类com.google.protobuf.AbstractMessage.Builder源码实例Demo

下面列出了怎么用com.google.protobuf.AbstractMessage.Builder的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: grain   文件: WSCodeUtil.java
public static WsPacket decodeJson(String stringResult) {
	try {

		JSONObject jsObj = JSONObject.fromObject(stringResult);
		String wsOpCode = jsObj.getString(WSOPCODE);
		if (wsOpCode == null) {
			if (WSManager.log != null) {
				WSManager.log.warn("数据为:" + stringResult + ",无wsOpCode");
			}
			return null;
		}
		if (!WSManager.wsOpCodeMap.containsKey(wsOpCode)) {
			if (WSManager.log != null) {
				WSManager.log.warn("wsOpCode为:" + wsOpCode + "无对应解析,请及时解决");
			}
			return null;
		}
		Class<?> className = WSManager.wsOpCodeMap.get(wsOpCode);

		Method buildM = className.getDeclaredMethod("newBuilder");
		AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null);
		Message data = PacketUtils.jsonToProtoBuf(stringResult, builder);
		WsPacket wsPacket = new WsPacket(wsOpCode, data);
		return wsPacket;
	} catch (Exception e) {
		if (WSManager.log != null) {
			WSManager.log.error("json转换成protobuf异常", e);
		}
		return null;
	}
}
 
源代码2 项目: PeonyFramwork   文件: HttpPBPacket.java
public HttpPBPacket(int opcode, Builder<?> builder) {
		this.opcode = opcode;
		Message msg = builder.build();
//		log.info("[HttpPBPacket][new] \nopode:{}, message:\n[\n{}]" , opcode,  msg);
		this.data = msg.toByteArray();
	}
 
源代码3 项目: saluki   文件: ProtobufSerializer.java
/**
 * @see io.github.saluki.serializer.IProtobufSerializer#toProtobuf(java.lang.Object)
 */
@Override
@SuppressWarnings({"unchecked", "rawtypes", "unused"})
public Message toProtobuf(Object pojo) throws ProtobufException {
  try {
    final Class<?> fromClazz = (Class<?>) pojo.getClass();
    final Class<? extends GeneratedMessageV3> protoClazz =
        ProtobufSerializerUtils.getProtobufClassFromPojoAnno(fromClazz);
    if (protoClazz == null) {
      throw new ProtobufAnnotationException(
          "Doesn't seem like " + fromClazz + " is ProtobufEntity");
    }
    final Map<Field, ProtobufAttribute> protoBufFields =
        ProtobufSerializerUtils.getAllProtbufFields(fromClazz);
    if (protoBufFields.isEmpty()) {
      return null;
    }
    final Method newBuilderMethod = protoClazz.getMethod("newBuilder");
    final Builder protoObjBuilder = (Builder) newBuilderMethod.invoke(null);
    for (Entry<Field, ProtobufAttribute> entry : protoBufFields.entrySet()) {
      final Field field = entry.getKey();
      final ProtobufAttribute gpbAnnotation = entry.getValue();
      final String fieldName = field.getName();
      // 1. Determine validity of value
      Object value = Pojo2ProtobufHelp.getPojoFieldValue(pojo, gpbAnnotation, field);
      // If value is null and it is not required, skip, as the default for Protobuf values is null
      if (value == null) {
        continue;
      }
      // 2. Call recursively if this is a ProtobufEntity
      value = Pojo2ProtobufHelp.serializeToProtobufEntity(value);
      // 3. Special recursively if this is a ProtobufEntity
      if (value instanceof Collection) {
        value = Pojo2ProtobufHelp.convertCollectionToProtobufs((Collection<Object>) value);
        if (((Collection) value).isEmpty()) {
          continue;
        }
      }
      if (value instanceof Map) {
        value = Pojo2ProtobufHelp.convertMapToProtobufs((Map) value);
        if (((Map) value).isEmpty()) {
          continue;
        }
      }
      String setter = ProtobufSerializerUtils.getProtobufSetter(gpbAnnotation, field, value);
      if (value instanceof Enum) {
        value = JReflectionUtils.runMethod(value, "getNumber");
        setter = setter + "Value";
      }
      Pojo2ProtobufHelp.setProtobufFieldValue(gpbAnnotation, protoObjBuilder, setter, value);
    }
    return protoObjBuilder.build();
  } catch (Exception e) {
    throw new ProtobufException(
        "Could not generate Protobuf object for " + pojo.getClass() + ": " + e, e);
  }
}
 
源代码4 项目: grain   文件: CodeUtils.java
/**
 * 将字符串转换成HttpPacket
 * 
 * @param stringResult
 *            字符串
 * @param isServer
 *            是不是服务器一般传true
 * @param httpPacket
 *            消息包
 * @return
 */
public static boolean decodeJson(String stringResult, boolean isServer, HttpPacket httpPacket) {
	try {
		// 转换成json获取hOpCode,如果没有看看头消息有没有
		JSONObject jsObj = JSONObject.fromObject(stringResult);
		String hOpCode;
		if (jsObj.containsKey(AllowParam.HOPCODE)) {
			hOpCode = jsObj.getString(AllowParam.HOPCODE);
		} else if (httpPacket.hSession.headParam.hOpCode != null && !httpPacket.hSession.headParam.hOpCode.equals("")) {
			hOpCode = httpPacket.hSession.headParam.hOpCode;
		} else {
			return false;
		}
		// 是否设定相应解析
		if (!HttpManager.hOpCodeMap.containsKey(hOpCode)) {
			if (HttpConfig.log != null) {
				HttpConfig.log.warn("hOpCode为:" + hOpCode + "无对应解析,请及时解决");
			}
			return false;
		}
		// 解析
		Class<?>[] classNames = HttpManager.hOpCodeMap.get(hOpCode);
		Class<?> className;
		if (isServer) {
			className = classNames[0];
		} else {
			className = classNames[1];
		}
		Method buildM = className.getDeclaredMethod("newBuilder");
		AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null);
		Message data = PacketUtils.jsonToProtoBuf(stringResult, builder);
		if (data == null) {
			return false;
		}
		// 设置hOpCode和消息体
		httpPacket.sethOpCode(hOpCode);
		httpPacket.setData(data);
		return true;
	} catch (Exception e) {
		if (HttpConfig.log != null) {
			HttpConfig.log.error("json转换成protobuf异常", e);
		}
		return false;
	}
}
 
源代码5 项目: pulsar   文件: FunctionCommon.java
public static void mergeJson(String json, Builder builder) throws IOException {
    JsonFormat.parser().merge(json, builder);
}
 
 类所在包
 类方法
 同包方法