Permalink
Browse files

avoid repeated calls to build() during deserialization by obtaining f…

…ield types from the Message's defaultInstance (#1215)
  • Loading branch information...
orischwartz authored and inder123 committed Dec 28, 2017
1 parent 3bf1967 commit c744ccd51cdea2e92b3e06abc44336943281cddd
Showing with 4 additions and 2 deletions.
  1. +4 −2 proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java
@@ -263,6 +263,9 @@ public Message deserialize(JsonElement json, Type typeOfT,
Message.Builder protoBuilder =
(Message.Builder) getCachedMethod(protoClass, "newBuilder").invoke(null);
Message defaultInstance =
(Message) getCachedMethod(protoClass, "getDefaultInstance").invoke(null);
Descriptor protoDescriptor =
(Descriptor) getCachedMethod(protoClass, "getDescriptor").invoke(null);
// Call setters on all of the available fields
@@ -300,8 +303,7 @@ public Message deserialize(JsonElement json, Type typeOfT,
fieldValue = context.deserialize(jsonElement, protoArrayFieldType);
protoBuilder.setField(fieldDescriptor, fieldValue);
} else {
Message prototype = protoBuilder.build();
Object field = prototype.getField(fieldDescriptor);
Object field = defaultInstance.getField(fieldDescriptor);
fieldValue = context.deserialize(jsonElement, field.getClass());
protoBuilder.setField(fieldDescriptor, fieldValue);
}

0 comments on commit c744ccd

Please sign in to comment.