public ProtobufJavaUtilSupport(@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) { this.parser = (parser != null ? parser : JsonFormat.parser()); this.printer = (printer != null ? printer : JsonFormat.printer()); }
public ProtobufJavaUtilSupport(@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) { this.parser = (parser != null ? parser : JsonFormat.parser()); this.printer = (printer != null ? printer : JsonFormat.printer()); }
@Override public Optional<String> serializeExampleRequest(String serviceName, String methodName, Object exampleRequest) { try { return Optional.of(JsonFormat.printer().print((MessageOrBuilder) exampleRequest)); } catch (InvalidProtocolBufferException e) { throw new UncheckedIOException( "Invalid example request protobuf. Is it missing required fields?", e); } }
public static <T extends Message> MethodDescriptor.Marshaller<T> jsonMarshaller(final T defaultInstance) { final JsonFormat.Printer printer = JsonFormat.printer().preservingProtoFieldNames(); final JsonFormat.Parser parser = JsonFormat.parser(); final Charset charset = Charset.forName("UTF-8");
GraphQlResponse.getDefaultInstance(), executionResult.toSpecification()); try { logger.info("Response in Json format: " + JsonFormat.printer().print(graphQlResponse)); } catch (InvalidProtocolBufferException e) { logger.log(Level.WARNING, "Response proto is invalid", e);
@Before public void setup() { this.registryInitializer = mock(ExtensionRegistryInitializer.class); this.extensionRegistry = mock(ExtensionRegistry.class); this.converter = new ProtobufJsonFormatHttpMessageConverter( JsonFormat.parser(), JsonFormat.printer(), this.registryInitializer); this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build(); }
private void writeDeviceSpecToFile(DeviceSpec deviceSpec, Path outputFile) { try { if (getOverwriteOutput()) { Files.deleteIfExists(getOutputPath()); } Files.write(outputFile, JsonFormat.printer().print(deviceSpec).getBytes(UTF_8)); } catch (IOException e) { throw new UncheckedIOException( String.format("Error while writing the output file '%s'.", outputFile), e); } }
/** * Converts a protobuf message to a JSON object * <p> * Note: Camel-cases the field names as defined in the proto definition * * @param input the protobuf message to convert * @return the converted JSON object */ public static JsonObject protobufToJsonCamelCase(Message input) { JsonObject object = new JsonObject(); if (input == null) { logger.warn("Protobuf message was null"); } else { try { String jsonString = JsonFormat.printer() .print(input); object = new JsonParser().parse(jsonString).getAsJsonObject(); } catch (Exception e) { throw new RuntimeException("Error deserializing protobuf to json", e); } } return object; }
/** * Converts a protobuf message to a JSON object * <p> * Note: Preserves the field names as defined in the proto definition * * @param input the protobuf message to convert * @return the converted JSON object */ public static JsonObject protobufToJson(Message input) { JsonObject object = new JsonObject(); if (input == null) { logger.warn("Protobuf message was null"); } else { try { String jsonString = JsonFormat.printer() .preservingProtoFieldNames() .print(input); object = new JsonParser().parse(jsonString).getAsJsonObject(); } catch (Exception e) { throw new RuntimeException("Error deserializing protobuf to json", e); } } return object; }
/** * Converts a protobuf message to a JSON object * <p> * Note: Preserves the field names as defined in the proto definition * Note: * * @param input the protobuf message to convert * @return the converted JSON object */ public static JsonObject protobufToJsonWithDefaultValues(Message input) { JsonObject object = new JsonObject(); if (input == null) { logger.warn("Protobuf message was null"); } else { try { String jsonString = JsonFormat.printer() .preservingProtoFieldNames() .includingDefaultValueFields() .print(input); object = new JsonParser().parse(jsonString).getAsJsonObject(); } catch (Exception e) { throw new RuntimeException("Error deserializing protobuf to json", e); } } return object; }
public static Path createDeviceSpecFile(DeviceSpec deviceSpec, Path destinationFile) throws Exception { Files.write(destinationFile, JsonFormat.printer().print(deviceSpec).getBytes(UTF_8)); return destinationFile; }
@Override public Optional<String> serializeExampleRequest(String serviceName, String methodName, Object exampleRequest) { try { return Optional.of(JsonFormat.printer().print((MessageOrBuilder) exampleRequest)); } catch (InvalidProtocolBufferException e) { throw new UncheckedIOException( "Invalid example request protobuf. Is it missing required fields?", e); } }
@Override public void save(OutputStream output, boolean saveValues, Map<String, String> metadata) throws IOException { KeanuSavedBayesNet.Model model = protobufSaver.getModel(saveValues, metadata); Writer outputWriter = new OutputStreamWriter(output); String jsonOutput = JsonFormat.printer().print(model); outputWriter.write(jsonOutput); outputWriter.close(); }
/** * Creates a new Writer which writes the messages it sees to the supplied Output. */ public static <T extends Message> Writer<T> create(List<Object> output, JsonFormat.TypeRegistry registry) { return new Writer<>(JsonFormat.printer().usingTypeRegistry(registry), output); }
private static String toJsonString(ChaincodeMessage message) { try { return JsonFormat.printer().print(message); } catch (InvalidProtocolBufferException e) { return String.format("{ Type: %s, TxId: %s }", message.getType(), message.getTxid()); } }
@Override public String extractOutput(List<Export.Table> accumulator) { ExportProtos.Export.Builder exportManifest = ExportProtos.Export.newBuilder(); exportManifest.addAllTables(accumulator); try { return JsonFormat.printer().print(exportManifest.build()); } catch (InvalidProtocolBufferException e) { throw new RuntimeException(e); } } }
private static void structAsMessage(Struct struct, Message.Builder messageBuilder) throws InvalidProtocolBufferException { String json = JsonFormat.printer() .preservingProtoFieldNames() .print(struct); JsonFormat.parser().merge(json, messageBuilder); }
/** * 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() .ignoringUnknownFields(); final Printer printer = JsonFormat.printer().preservingProtoFieldNames(); return jsonMarshaller(defaultInstance, parser, printer); }
private static Struct messageAsStruct(MessageOrBuilder message) { try { String json = JsonFormat.printer() .preservingProtoFieldNames() .print(message); Struct.Builder structBuilder = Struct.newBuilder(); JsonFormat.parser().merge(json, structBuilder); return structBuilder.build(); } catch (InvalidProtocolBufferException e) { throw new RuntimeException("Failed to convert protobuf message to struct", e); } }
public EntityJsonPrinter() { TypeRegistry typeRegistry = TypeRegistry.newBuilder() .add(Entity.getDescriptor()) .build(); jsonPrinter = JsonFormat.printer() .usingTypeRegistry(typeRegistry) .omittingInsignificantWhitespace(); }