com.google.protobuf.util.JsonFormat.TypeRegistry源码实例Demo

类com.google.protobuf.util.JsonFormat.TypeRegistry源码实例Demo

下面列出了com.google.protobuf.util.JsonFormat.TypeRegistry 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: seldon-server   文件: ClientRpcStore.java
private JsonNode getDefaultRequestJSON(Message msg) throws JsonParseException, IOException
{
	Message.Builder o2 = DefaultCustomPredictRequest.newBuilder();
	TypeRegistry registry = TypeRegistry.newBuilder().add(o2.getDescriptorForType()).build();
	JsonFormat.Printer jPrinter = JsonFormat.printer();
	String result = jPrinter.usingTypeRegistry(registry).print(msg);
	ObjectMapper mapper = new ObjectMapper();
	JsonFactory factory = mapper.getFactory();
	JsonParser parser = factory.createParser(result);
	JsonNode jNode = mapper.readTree(parser);
	if (jNode.has(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD))
	{
		JsonNode values = jNode.get(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD).get("values");
		((ObjectNode) jNode).set(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD, values);
	}
	return jNode;
}
 
源代码2 项目: seldon-server   文件: ClientRpcStore.java
public ClassificationReply getPredictReplyFromJson(String client,JsonNode json)
{
	RPCConfig config = services.get(client);
	try
	{
		TypeRegistry registry = null;
		if (config != null && config.replyClass != null && json.has(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD))
		{
			if (!json.get(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD).has("@type"))
				((ObjectNode) json.get(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD)).put("@type", "type.googleapis.com/" + config.replyClass.getName());
			Method m = config.replyBuilder;
			Message.Builder o = (Message.Builder) m.invoke(null);
			registry = TypeRegistry.newBuilder().add(o.getDescriptorForType()).build();
		}
		ClassificationReply.Builder builder = ClassificationReply.newBuilder();
		JsonFormat.Parser jFormatter = JsonFormat.parser();
		if (registry != null)
			jFormatter = jFormatter.usingTypeRegistry(registry);
		jFormatter.merge(json.toString(), builder);
		ClassificationReply reply = builder.build();
		return reply;
	} catch (Exception e) {
		logger.error("Failed to convert json "+json.toString()+" to PredictReply",e);
		return null;
	}
}
 
源代码3 项目: seldon-server   文件: ClientRpcStore.java
private ClassificationRequest getPredictRequestWithCustomDefaultFromJSON(JsonNode json) throws InvalidProtocolBufferException
  {
  	ObjectMapper mapper = new ObjectMapper();
  	ObjectNode data = mapper.createObjectNode();
  	data.put("@type", "type.googleapis.com/" + DefaultCustomPredictRequest.class.getName());
  	data.put("values", json.get(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD));
  	((ObjectNode) json).put(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD, data);
Message.Builder o = DefaultCustomPredictRequest.newBuilder();
TypeRegistry registry = TypeRegistry.newBuilder().add(o.getDescriptorForType()).build();
ClassificationRequest.Builder builder = ClassificationRequest.newBuilder();
JsonFormat.Parser jFormatter = JsonFormat.parser();
if (registry != null)
	jFormatter = jFormatter.usingTypeRegistry(registry);
jFormatter.merge(json.toString(), builder);
ClassificationRequest request = builder.build();
return request;
  }
 
源代码4 项目: grpc-swagger   文件: GrpcReflectionUtils.java
public static List<DynamicMessage> parseToMessages(TypeRegistry registry, Descriptor descriptor,
        List<String> jsonTexts) {
    Parser parser = JsonFormat.parser().usingTypeRegistry(registry);
    List<DynamicMessage> messages = new ArrayList<>();
    try {
        for (String jsonText : jsonTexts) {
            DynamicMessage.Builder messageBuilder = DynamicMessage.newBuilder(descriptor);
            parser.merge(jsonText, messageBuilder);
            messages.add(messageBuilder.build());
        }
        return messages;
    } catch (InvalidProtocolBufferException e) {
        throw new IllegalArgumentException("Unable to parse json text", e);
    }
}
 
源代码5 项目: grpc-swagger   文件: GrpcProxyService.java
public CallResults invokeMethod(GrpcMethodDefinition definition, Channel channel, CallOptions callOptions,
        List<String> requestJsonTexts) {
    FileDescriptorSet fileDescriptorSet = GrpcReflectionUtils.resolveService(channel, definition.getFullServiceName());
    if (fileDescriptorSet == null) {
        return null;
    }
    ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
    MethodDescriptor methodDescriptor = serviceResolver.resolveServiceMethod(definition);
    TypeRegistry registry = TypeRegistry.newBuilder().add(serviceResolver.listMessageTypes()).build();
    List<DynamicMessage> requestMessages = GrpcReflectionUtils.parseToMessages(registry, methodDescriptor.getInputType(),
            requestJsonTexts);
    CallResults results = new CallResults();
    StreamObserver<DynamicMessage> streamObserver = MessageWriter.newInstance(registry, results);
    CallParams callParams = CallParams.builder()
            .methodDescriptor(methodDescriptor)
            .channel(channel)
            .callOptions(callOptions)
            .requests(requestMessages)
            .responseObserver(streamObserver)
            .build();
    try {
        grpcClientService.call(callParams).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException("Caught exception while waiting for rpc", e);
    }
    return results;
}
 
源代码6 项目: milkman   文件: MessageReader.java
/** Creates a {@link MessageReader} which reads messages from a stream. */
public static MessageReader forStream(InputStream stream, Descriptor descriptor, TypeRegistry registry) {
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  return new MessageReader(
      JsonFormat.parser().usingTypeRegistry(registry),
      descriptor,
      reader,
      "STDIN");
}
 
源代码7 项目: milkman   文件: MessageReader.java
/** Creates a {@link MessageReader} which reads the messages from a file. */
public static MessageReader forFile(Path path, Descriptor descriptor, TypeRegistry registry) {
  try {
    return new MessageReader(
        JsonFormat.parser().usingTypeRegistry(registry),
        descriptor,
        Files.newBufferedReader(path),
        path.toString());
  } catch (IOException e) {
    throw new IllegalArgumentException("Unable to read file: " + path.toString(), e);
  }
}
 
源代码8 项目: milkman   文件: DynamicMessageDeEncoder.java
public DynamicMessageDeEncoder(ProtoMethodName protoMethod, FileDescriptorSet fileDescriptor) {
	var resolver = ServiceResolver.fromFileDescriptorSet(fileDescriptor);
	methodDefinition = resolver.resolveServiceMethod(protoMethod);

	registry = TypeRegistry.newBuilder().add(resolver.listMessageTypes()).build();

	writer = MessageWriter.create(registry);
}
 
源代码9 项目: grpc-swagger   文件: GrpcReflectionUtils.java
public static List<DynamicMessage> parseToMessages(TypeRegistry registry, Descriptor descriptor,
        List<String> jsonTexts) {
    Parser parser = JsonFormat.parser().usingTypeRegistry(registry);
    List<DynamicMessage> messages = new ArrayList<>();
    try {
        for (String jsonText : jsonTexts) {
            DynamicMessage.Builder messageBuilder = DynamicMessage.newBuilder(descriptor);
            parser.merge(jsonText, messageBuilder);
            messages.add(messageBuilder.build());
        }
        return messages;
    } catch (InvalidProtocolBufferException e) {
        throw new IllegalArgumentException("Unable to parse json text", e);
    }
}
 
源代码10 项目: grpc-swagger   文件: GrpcProxyService.java
public CallResults invokeMethod(GrpcMethodDefinition definition, Channel channel, CallOptions callOptions,
        List<String> requestJsonTexts) {
    FileDescriptorSet fileDescriptorSet = GrpcReflectionUtils.resolveService(channel, definition.getFullServiceName());
    if (fileDescriptorSet == null) {
        return null;
    }
    ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
    MethodDescriptor methodDescriptor = serviceResolver.resolveServiceMethod(definition);
    TypeRegistry registry = TypeRegistry.newBuilder().add(serviceResolver.listMessageTypes()).build();
    List<DynamicMessage> requestMessages = GrpcReflectionUtils.parseToMessages(registry, methodDescriptor.getInputType(),
            requestJsonTexts);
    CallResults results = new CallResults();
    StreamObserver<DynamicMessage> streamObserver = MessageWriter.newInstance(registry, results);
    CallParams callParams = CallParams.builder()
            .methodDescriptor(methodDescriptor)
            .channel(channel)
            .callOptions(callOptions)
            .requests(requestMessages)
            .responseObserver(streamObserver)
            .build();
    try {
        grpcClientService.call(callParams).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException("Caught exception while waiting for rpc", e);
    }
    return results;
}
 
源代码11 项目: DataflowTemplates   文件: DatastoreConverters.java
public EntityJsonPrinter() {
  TypeRegistry typeRegistry = TypeRegistry.newBuilder()
      .add(Entity.getDescriptor())
      .build();
  jsonPrinter = JsonFormat.printer()
      .usingTypeRegistry(typeRegistry)
      .omittingInsignificantWhitespace();
}
 
源代码12 项目: DataflowTemplates   文件: DatastoreConverters.java
public EntityJsonParser() {
  TypeRegistry typeRegistry = TypeRegistry.newBuilder()
      .add(Entity.getDescriptor())
      .build();

  jsonParser = JsonFormat.parser()
      .usingTypeRegistry(typeRegistry);
}
 
源代码13 项目: api-compiler   文件: ConfigGeneratorDriver.java
private TypeRegistry.Builder addPlatformExtensions(TypeRegistry.Builder registryBuilder) {
  ExtensionRegistry extensions = ToolProtoUtil.getStandardPlatformExtensions();
  for (String extendedType : EXTENDED_ELEMENTS) {
    for (ExtensionRegistry.ExtensionInfo info :
        extensions.getAllImmutableExtensionsByExtendedType(extendedType)) {

      if (null != info.defaultInstance) {
        registryBuilder.add(info.defaultInstance.getDescriptorForType());
      }
    }
  }
  return registryBuilder;
}
 
源代码14 项目: seldon-server   文件: ClientRpcStore.java
private JsonNode getJSONFromMethod(Method m,Message msg,String fieldname) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonParseException, IOException
{
	Message.Builder o2 = (Message.Builder) m.invoke(null);
	TypeRegistry registry = TypeRegistry.newBuilder().add(o2.getDescriptorForType()).build();
	
	JsonFormat.Printer jPrinter = JsonFormat.printer();
	String result = jPrinter.usingTypeRegistry(registry).print(msg);
	ObjectMapper mapper = new ObjectMapper();
	JsonFactory factory = mapper.getFactory();
	JsonParser parser = factory.createParser(result);
	JsonNode jNode = mapper.readTree(parser);
	if (jNode.has(fieldname) && jNode.get(fieldname).has("@type"))
		((ObjectNode) jNode.get(fieldname)).remove("@type");
	return jNode;
}
 
源代码15 项目: seldon-server   文件: CustomClassTest.java
@Test
public void testParseFromJSON() throws InvalidProtocolBufferException
{
	String json = "{\"meta\":{\"modelName\":\"some-name\"},\"custom\":{\"@type\":\"type.googleapis.com/io.seldon.api.rpc.example.CustomPredictReply\",\"data\":\"some custom data\"}}";
	ClassificationReply.Builder builder = ClassificationReply.newBuilder();
	CustomPredictReply.Builder customBuilder = CustomPredictReply.newBuilder();
	TypeRegistry registry = TypeRegistry.newBuilder().add(customBuilder.getDescriptorForType()).build();
	JsonFormat.Parser jFormatter = JsonFormat.parser().usingTypeRegistry(registry);
	jFormatter.merge(json, builder);
	ClassificationReply reply = builder.build();
	System.out.println(reply);
}
 
源代码16 项目: seldon-server   文件: CustomClassTest.java
@Test
public void testParseFromJSONDefault() throws InvalidProtocolBufferException
{
	String json = "{\"data\":{\"@type\":\"type.googleapis.com/io.seldon.api.rpc.DefaultCustomPredictRequest\",\"values\":[1.2,2.1]}}";
	ClassificationRequest.Builder builder = ClassificationRequest.newBuilder();
	DefaultCustomPredictRequest.Builder customBuilder = DefaultCustomPredictRequest.newBuilder();
	TypeRegistry registry = TypeRegistry.newBuilder().add(customBuilder.getDescriptorForType()).build();
	JsonFormat.Parser jFormatter = JsonFormat.parser().usingTypeRegistry(registry);
	jFormatter.merge(json, builder);
	ClassificationRequest request = builder.build();
	System.out.println(request);
}
 
源代码17 项目: milkman   文件: MessageReader.java
/** Creates a {@link MessageReader} which reads the messages from a file. */
public static MessageReader forFile(Path path, Descriptor descriptor) {
  return forFile(path, descriptor, TypeRegistry.getEmptyTypeRegistry());
}
 
源代码18 项目: milkman   文件: MessageWriter.java
/**
 * Creates a new {@link MessageWriter} which writes the messages it sees to the supplied
 */
public static <T extends Message> MessageWriter<T> create(TypeRegistry registry) {
  return new MessageWriter<>(JsonFormat.printer().usingTypeRegistry(registry));
}
 
源代码19 项目: api-compiler   文件: ConfigGeneratorDriver.java
private void generateOutputFiles(Service serviceConfig)
    throws FileNotFoundException, IOException {
  // Create normalized service proto, in binary form.
  if (!Strings.isNullOrEmpty(options.get(BIN_OUT))) {
    File outFileBinaryServiceConfig = new File(options.get(BIN_OUT));
    OutputStream normalizedOut = new FileOutputStream(outFileBinaryServiceConfig);
    serviceConfig.writeTo(normalizedOut);
    normalizedOut.close();
  }

  // Create normalized service proto, in text form.
  if (!Strings.isNullOrEmpty(options.get(TXT_OUT))) {
    File outFileTxtServiceConfig = new File(options.get(TXT_OUT));
    try (PrintWriter textPrintWriter =
        new PrintWriter(outFileTxtServiceConfig, StandardCharsets.UTF_8.name())) {
      TextFormat.print(serviceConfig, textPrintWriter);
    }
  }

  // Create normalized service proto, in json form.
  if (!Strings.isNullOrEmpty(options.get(JSON_OUT))) {
    File outFileJsonServiceConfig = new File(options.get(JSON_OUT));
    TypeRegistry registry =
        addPlatformExtensions(TypeRegistry.newBuilder())
            .add(Service.getDescriptor())
            .add(com.google.protobuf.BoolValue.getDescriptor())
            .add(com.google.protobuf.BytesValue.getDescriptor())
            .add(com.google.protobuf.DoubleValue.getDescriptor())
            .add(com.google.protobuf.FloatValue.getDescriptor())
            .add(com.google.protobuf.Int32Value.getDescriptor())
            .add(com.google.protobuf.Int64Value.getDescriptor())
            .add(com.google.protobuf.StringValue.getDescriptor())
            .add(com.google.protobuf.UInt32Value.getDescriptor())
            .add(com.google.protobuf.UInt64Value.getDescriptor())
            .build();
    JsonFormat.Printer jsonPrinter = JsonFormat.printer().usingTypeRegistry(registry);
    try (PrintWriter jsonPrintWriter =
        new PrintWriter(outFileJsonServiceConfig, StandardCharsets.UTF_8.name())) {
      jsonPrinter.appendTo(serviceConfig, jsonPrintWriter);
    }
  }
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法