public static String toJson(Message message) { StringWriter json = new StringWriter(); try (JsonWriter jsonWriter = JsonWriter.of(json)) { write(message, jsonWriter); } return json.toString(); }
public static void writeProtobuf(Message msg, Request request, Response response) { OutputStream output = response.stream().output(); try { if (request.getMediaType().equals(PROTOBUF)) { response.stream().setMediaType(PROTOBUF); msg.writeTo(output); } else { response.stream().setMediaType(JSON); try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) { ProtobufJsonFormat.write(msg, writer); } } } catch (Exception e) { throw new IllegalStateException("Error while writing protobuf message", e); } finally { IOUtils.closeQuietly(output); } }
private void test(List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest, String expected) { StringWriter output = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(output); ProtobufJsonFormat.write(underTest.build(db.getSession(), blocks, branch, pullRequest), jsonWriter); JsonAssert.assertJson(output.toString()).isSimilarTo(expected); }
@Test public void bytes_field_can_not_be_converted() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("JSON format does not support type 'BYTE_STRING' of field 'bytesField'"); PrimitiveTypeMsg protobuf = PrimitiveTypeMsg.newBuilder() .setBytesField(ByteString.copyFrom(new byte[]{2, 4})) .build(); ProtobufJsonFormat.write(protobuf, JsonWriter.of(new StringWriter())); }
public static String toJson(Message message) { StringWriter json = new StringWriter(); try (JsonWriter jsonWriter = JsonWriter.of(json)) { write(message, jsonWriter); } return json.toString(); }
public static void writeProtobuf(Message msg, Request request, Response response) { OutputStream output = response.stream().output(); try { if (request.getMediaType().equals(PROTOBUF)) { response.stream().setMediaType(PROTOBUF); msg.writeTo(output); } else { response.stream().setMediaType(JSON); try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) { ProtobufJsonFormat.write(msg, writer); } } } catch (Exception e) { throw new IllegalStateException("Error while writing protobuf message", e); } finally { IOUtils.closeQuietly(output); } }