com.google.protobuf.util.JsonFormat.TypeRegistry Java Examples

The following examples show how to use com.google.protobuf.util.JsonFormat.TypeRegistry. 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: ClientRpcStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
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;
  }
 
Example #2
Source File: ClientRpcStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #3
Source File: ClientRpcStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: DatastoreConverters.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
public EntityJsonPrinter() {
  TypeRegistry typeRegistry = TypeRegistry.newBuilder()
      .add(Entity.getDescriptor())
      .build();
  jsonPrinter = JsonFormat.printer()
      .usingTypeRegistry(typeRegistry)
      .omittingInsignificantWhitespace();
}
 
Example #5
Source File: CustomClassTest.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: CustomClassTest.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: ClientRpcStore.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: ConfigGeneratorDriver.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: DatastoreConverters.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
public EntityJsonParser() {
  TypeRegistry typeRegistry = TypeRegistry.newBuilder()
      .add(Entity.getDescriptor())
      .build();

  jsonParser = JsonFormat.parser()
      .usingTypeRegistry(typeRegistry);
}
 
Example #10
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #11
Source File: GrpcProxyService.java    From grpc-swagger with MIT License 5 votes vote down vote up
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;
}
 
Example #12
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #13
Source File: DynamicMessageDeEncoder.java    From milkman with MIT License 5 votes vote down vote up
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);
}
 
Example #14
Source File: MessageReader.java    From milkman with MIT License 5 votes vote down vote up
/** 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);
  }
}
 
Example #15
Source File: MessageReader.java    From milkman with MIT License 5 votes vote down vote up
/** 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");
}
 
Example #16
Source File: GrpcProxyService.java    From grpc-swagger with MIT License 5 votes vote down vote up
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;
}
 
Example #17
Source File: ConfigGeneratorDriver.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
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);
    }
  }
}
 
Example #18
Source File: MessageWriter.java    From milkman with MIT License 4 votes vote down vote up
/**
 * 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));
}
 
Example #19
Source File: MessageReader.java    From milkman with MIT License 4 votes vote down vote up
/** 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());
}