@Test public void handleBody() throws Exception { TestRequest.Builder request = TestRequest.newBuilder(); request.setS("string"); request.setBytearray(ByteString.copyFromUtf8("string")); request.setUint3(123); request.setUint6(123); request.setInt3(123); request.setInt6(123); request.setF(123.456f); request.setD(123.456d); request.setEnu(TestEnum.SECOND); request.setBoolean(true); request.setNt(NestedType.newBuilder().setF1("abc").build()); String json = JsonFormat.printer().print(request); TestRequest.Builder deserialized = TestRequest.newBuilder(); RequestParser.handleBody("*", deserialized, json); assertThat(deserialized.build()).isEqualTo(request.build()); }
public static <V extends Message> void handleBody( String fieldPath, V.Builder builder, String body) throws InvalidProtocolBufferException { Message.Builder toMerge; if ("*".equals(fieldPath)) { toMerge = builder; } else { ImmutableList<Descriptors.FieldDescriptor> fieldDescriptors = ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), fieldPath); if (fieldDescriptors.isEmpty()) { return; } toMerge = builder; for (Descriptors.FieldDescriptor fd : fieldDescriptors) { if (fd.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { toMerge = toMerge.getFieldBuilder(fd); } } } if (toMerge != null) { JsonHandler.parser().merge(body, toMerge); } }
RequestParser { public static <V extends Message> void handleBody( String fieldPath, V.Builder builder, String body) throws InvalidProtocolBufferException { Message.Builder toMerge; if ("*".equals(fieldPath)) { toMerge = builder; } else { ImmutableList<Descriptors.FieldDescriptor> fieldDescriptors = ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), fieldPath); if (fieldDescriptors.isEmpty()) { return; } toMerge = builder; for (Descriptors.FieldDescriptor fd : fieldDescriptors) { if (fd.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { toMerge = toMerge.getFieldBuilder(fd); } } } if (toMerge != null) { JsonHandler.parser().merge(body, toMerge); } } }
RequestParser { public static <V extends Message> void handleBody( String fieldPath, V.Builder builder, String body) throws InvalidProtocolBufferException { Message.Builder toMerge; if ("*".equals(fieldPath)) { toMerge = builder; } else { ImmutableList<Descriptors.FieldDescriptor> fieldDescriptors = ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), fieldPath); if (fieldDescriptors.isEmpty()) { return; } toMerge = builder; for (Descriptors.FieldDescriptor fd : fieldDescriptors) { if (fd.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { toMerge = toMerge.getFieldBuilder(fd); } } } if (toMerge != null) { JsonHandler.parser().merge(body, toMerge); } }  }
RequestParser { public static <V extends Message> void handleBody( String fieldPath, V.Builder builder, String body) throws InvalidProtocolBufferException { Message.Builder toMerge; if ("*".equals(fieldPath)) { toMerge = builder; } else { ImmutableList<Descriptors.FieldDescriptor> fieldDescriptors = ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), fieldPath); if (fieldDescriptors.isEmpty()) { return; } toMerge = builder; for (Descriptors.FieldDescriptor fd : fieldDescriptors) { if (fd.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { toMerge = toMerge.getFieldBuilder(fd); } } } if (toMerge != null) { JsonHandler.parser().merge(body, toMerge); } }  static void parseQueryParams(UriInfo uriInfo,
            V.Builder builder,
            DescriptorProtos.FieldDescriptorProto... pathParams); static void parseQueryParams(UriInfo uriInfo,
            V.Builder builder,
            List<DescriptorProtos.FieldDescriptorProto> pathParams); static T parseHeaders(HttpHeaders headers, T stub); static Metadata parseHeaders(HttpHeaders headers); static void setFieldSafely(Message.Builder builder, String path, String value); static void setFieldSafely(Message.Builder builder, String path, List<String> value); static void setFieldSafely(Message.Builder builder, Descriptors.FieldDescriptor fd, List<String> value); static void handleBody(
            String fieldPath,
            V.Builder builder,
            String body); }
RequestParser { public static <V extends Message> void handleBody( String fieldPath, V.Builder builder, String body) throws InvalidProtocolBufferException { Message.Builder toMerge; if ("*".equals(fieldPath)) { toMerge = builder; } else { ImmutableList<Descriptors.FieldDescriptor> fieldDescriptors = ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), fieldPath); if (fieldDescriptors.isEmpty()) { return; } toMerge = builder; for (Descriptors.FieldDescriptor fd : fieldDescriptors) { if (fd.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { toMerge = toMerge.getFieldBuilder(fd); } } } if (toMerge != null) { JsonHandler.parser().merge(body, toMerge); } }  static void parseQueryParams(UriInfo uriInfo,
            V.Builder builder,
            DescriptorProtos.FieldDescriptorProto... pathParams); static void parseQueryParams(UriInfo uriInfo,
            V.Builder builder,
            List<DescriptorProtos.FieldDescriptorProto> pathParams); static T parseHeaders(HttpHeaders headers, T stub); static Metadata parseHeaders(HttpHeaders headers); static void setFieldSafely(Message.Builder builder, String path, String value); static void setFieldSafely(Message.Builder builder, String path, List<String> value); static void setFieldSafely(Message.Builder builder, Descriptors.FieldDescriptor fd, List<String> value); static void handleBody(
            String fieldPath,
            V.Builder builder,
            String body);  }