Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Convert Protocol Buffers to JSON

Twister pairs nicely with Jackson’s ObjectMapper since they both use POJOs.

Encode Protocol Buffer Messages to a JSON String

Person person = Person.newBuilder()
        .setId(1)
        .setName("John Doe")
        .setAge(30)
        .setCity("New York")
        .build();

Map<String, Object> personMap = new ProtoMapper().wrap(person);
String json = new ObjectMapper().writeValueAsString(personMap);

Convert Protocol Buffer Bytes to a JSON String

Descriptor personDescriptor = Person.getDescriptor();
ByteBuffer personProtoBytes = ...;
Map<String, Object> personMap = ProtoReader.read(personProtoBytes, personDescriptor);
String json = new ObjectMapper().writeValueAsString(personMap);