@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); } }
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);
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); } }
public static <T extends Message> String encodeProtoJson(T message) throws InvalidProtocolBufferException { return JsonFormat.printer().omittingInsignificantWhitespace().print(message); }
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 InputStream stream(T value) { try { return new ByteArrayInputStream(printer.print(value).getBytes(charset)); } catch (InvalidProtocolBufferException e) { throw Status.INTERNAL .withCause(e) .withDescription("Unable to print json proto") .asRuntimeException(); } }
@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 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); } }
private static void structAsMessage(Struct struct, Message.Builder messageBuilder) throws InvalidProtocolBufferException { String json = JsonFormat.printer() .preservingProtoFieldNames() .print(struct); JsonFormat.parser().merge(json, messageBuilder); }
@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 onNext(V value) { if (closed) { throw new IllegalStateException("JerseyStreamingObserver has already been closed"); } addHeadersIfNotSent(); try { write(JsonHandler.streamPrinter().print(value)); } catch (IOException e) { onError(e); } }
@Override public Optional<String> handleStreamingError(Throwable t) throws InvalidProtocolBufferException { Status grpcError = GrpcErrorUtil.throwableToStatus(t).getPayload(); // JsonFormat doesn't support serializing Any. if (!grpcError.getDetailsList().isEmpty()) { grpcError = grpcError.toBuilder().clearDetails().build(); } return Optional.of(JsonHandler.streamPrinter().print(grpcError)); } }
@Override public void onNext(T message) { try { output.add(jsonPrinter.print(message)); } catch (InvalidProtocolBufferException e) { logger.warning(e.getMessage()); } }
@Override public ExampleMessage.GetResponse get(ExampleMessage.GetRequest request) { ExampleMessage.GetResponse response = stateMachine.get(request); try { LOG.info("get request, request={}, response={}", printer.print(request), printer.print(response)); } catch (InvalidProtocolBufferException ex) { ex.printStackTrace(); } return response; }
/** * Prints an Entity as a JSON String. * @param entity a Datastore Protobuf Entity. * @return Datastore Entity encoded as a JSON String. * @throws InvalidProtocolBufferException */ public String print(Entity entity) throws InvalidProtocolBufferException { return jsonPrinter.print(entity); } }
@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(); }
/** * 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; }
/** * 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: 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; }
public static Path createDeviceSpecFile(DeviceSpec deviceSpec, Path destinationFile) throws Exception { Files.write(destinationFile, JsonFormat.printer().print(deviceSpec).getBytes(UTF_8)); return destinationFile; }