@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); } }
/** * Parses the JSON input file containing the list of features. */ public static List<Feature> parseFeatures(URL file) throws IOException { InputStream input = file.openStream(); try { Reader reader = new InputStreamReader(input); try { FeatureDatabase.Builder database = FeatureDatabase.newBuilder(); JsonFormat.parser().merge(reader, database); return database.getFeatureList(); } finally { reader.close(); } } finally { input.close(); } }
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);
/** * Parses the JSON input file containing the list of features. */ public static List<Feature> parseFeatures(URL file) throws IOException { InputStream input = file.openStream(); try { Reader reader = new InputStreamReader(input); try { FeatureDatabase.Builder database = FeatureDatabase.newBuilder(); JsonFormat.parser().merge(reader, database); return database.getFeatureList(); } finally { reader.close(); } } finally { input.close(); } }
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); } }
private void run() throws Exception { stream = new SpannerImpl.GrpcStreamIterator(10); stream.setCall( new SpannerRpc.StreamingCall() { @Override public void cancel(@Nullable String message) {} @Override public void request(int numMessages) {} }); consumer = stream.consumer(); resultSet = new SpannerImpl.GrpcResultSet(stream, new NoOpListener(), QueryMode.NORMAL); JSONArray chunks = testCase.getJSONArray("chunks"); JSONObject expectedResult = testCase.getJSONObject("result"); for (int i = 0; i < chunks.length(); i++) { PartialResultSet.Builder builder = PartialResultSet.newBuilder(); JsonFormat.parser().merge(chunks.getString(i), builder); consumer.onPartialResultSet(builder.build()); } consumer.onCompleted(); assertResultSet(resultSet, expectedResult.getJSONArray("value")); }
/** * 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; }
@Test public void writeJsonWithJavaFormat() throws IOException { this.converter = new ProtobufHttpMessageConverter( new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(), this.extensionRegistry); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = MediaType.APPLICATION_JSON_UTF8; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(contentType, outputMessage.getHeaders().getContentType()); final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertFalse("body is empty", body.isEmpty()); Msg.Builder builder = Msg.newBuilder(); JsonFormat.parser().merge(body, builder); assertEquals(this.testMsg, builder.build()); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER)); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)); }
/** * 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; }
@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(); }
@Test public void writeJsonWithGoogleProtobuf() throws IOException { this.converter = new ProtobufHttpMessageConverter( new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null), this.extensionRegistry); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = MediaType.APPLICATION_JSON_UTF8; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(contentType, outputMessage.getHeaders().getContentType()); final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); assertFalse("body is empty", body.isEmpty()); Msg.Builder builder = Msg.newBuilder(); JsonFormat.parser().merge(body, builder); assertEquals(this.testMsg, builder.build()); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER)); assertNull(outputMessage.getHeaders().getFirst( ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)); }
/** * 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; }
/** * Parses the JSON input file containing the list of features. */ public static List<Feature> parseFeatures(URL file) throws IOException { InputStream input = file.openStream(); try { Reader reader = new InputStreamReader(input); try { FeatureDatabase.Builder database = FeatureDatabase.newBuilder(); JsonFormat.parser().merge(reader, database); return database.getFeatureList(); } finally { reader.close(); } } finally { input.close(); } }
public static Path createDeviceSpecFile(DeviceSpec deviceSpec, Path destinationFile) throws Exception { Files.write(destinationFile, JsonFormat.printer().print(deviceSpec).getBytes(UTF_8)); return destinationFile; }
/** * Converts a JSON object to a protobuf message. * <p> * Note: Ignores unknown fields * * @param builder the proto message type builder * @param input the JSON object to convert * @return the converted protobuf message */ public static Message fromJson(Message.Builder builder, JsonObject input) throws Exception { JsonFormat.parser().ignoringUnknownFields().merge(input.toString(), builder); return builder.build(); }