com.google.protobuf.AbstractMessage.Builder Java Examples

The following examples show how to use com.google.protobuf.AbstractMessage.Builder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WSCodeUtil.java    From grain with MIT License 5 votes vote down vote up
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;
	}
}
 
Example #2
Source File: HttpPBPacket.java    From PeonyFramwork with Apache License 2.0 4 votes vote down vote up
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();
	}
 
Example #3
Source File: ProtobufSerializer.java    From saluki with Apache License 2.0 4 votes vote down vote up
/**
 * @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);
  }
}
 
Example #4
Source File: CodeUtils.java    From grain with MIT License 4 votes vote down vote up
/**
 * 将字符串转换成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;
	}
}
 
Example #5
Source File: FunctionCommon.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static void mergeJson(String json, Builder builder) throws IOException {
    JsonFormat.parser().merge(json, builder);
}