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

The following examples show how to use com.google.protobuf.util.JsonFormat.Parser. 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: RestConfig.java    From kbear with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private ProtobufHttpMessageConverter newCustomProtobufMessageConverter() throws Exception {
    Constructor[] constructors = ProtobufHttpMessageConverter.class.getDeclaredConstructors();
    Constructor requiredConstructor = null;
    for (Constructor constructor : constructors) {
        if (constructor.getParameterTypes().length == 2) {
            constructor.setAccessible(true);
            requiredConstructor = constructor;
            break;
        }
    }

    Class[] classes = ProtobufHttpMessageConverter.class.getDeclaredClasses();
    Class requiredClass = null;
    for (Class clazz : classes) {
        if (clazz.getSimpleName().equals("ProtobufJavaUtilSupport")) {
            requiredClass = clazz;
            break;
        }
    }

    Constructor pbUtilSupportConstructor = requiredClass.getConstructor(Parser.class, Printer.class);
    pbUtilSupportConstructor.setAccessible(true);

    Parser parser = JsonFormat.parser().ignoringUnknownFields();
    Printer printer = JsonFormat.printer().includingDefaultValueFields().preservingProtoFieldNames()
            .omittingInsignificantWhitespace();
    Object support = pbUtilSupportConstructor.newInstance(parser, printer);
    return (ProtobufHttpMessageConverter) requiredConstructor.newInstance(support, null);
}
 
Example #2
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 #3
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 #4
Source File: JsonMarshaller.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
  final Parser parser = JsonFormat.parser();
  final Printer printer = JsonFormat.printer();
  return jsonMarshaller(defaultInstance, parser, printer);
}
 
Example #5
Source File: JsonMarshaller.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
  final Parser parser = JsonFormat.parser();
  final Printer printer = JsonFormat.printer();
  return jsonMarshaller(defaultInstance, parser, printer);
}