/** * Merges all fields specified by this FieldMaskTree from {@code source} to {@code destination}. */ void merge(Message source, Message.Builder destination, FieldMaskUtil.MergeOptions options) { if (source.getDescriptorForType() != destination.getDescriptorForType()) { throw new IllegalArgumentException("Cannot merge messages of different types."); } if (root.children.isEmpty()) { return; } merge(root, "", source, destination, options); }
private void mergeMessage(JsonElement json, Message.Builder builder, boolean skipTypeUrl) throws InvalidProtocolBufferException { if (!(json instanceof JsonObject)) { throw new InvalidProtocolBufferException("Expect message object but got: " + json); } JsonObject object = (JsonObject) json; Map<String, FieldDescriptor> fieldNameMap = getFieldNameMap(builder.getDescriptorForType()); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { if (skipTypeUrl && entry.getKey().equals("@type")) { continue; } FieldDescriptor field = fieldNameMap.get(entry.getKey()); if (field == null) { if (ignoringUnknownFields) { continue; } throw new InvalidProtocolBufferException( "Cannot find field: " + entry.getKey() + " in message " + builder.getDescriptorForType().getFullName()); } mergeField(field, entry.getValue(), builder); } }
private void merge(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { WellKnownTypeParser specialParser = wellKnownTypeParsers.get(builder.getDescriptorForType().getFullName()); if (specialParser != null) { specialParser.merge(this, json, builder); return; } mergeMessage(json, builder, false); }
Message.Builder destination, FieldMaskUtil.MergeOptions options) { if (source.getDescriptorForType() != destination.getDescriptorForType()) { throw new IllegalArgumentException( String.format( "source (%s) and destination (%s) descriptor must be equal", source.getDescriptorForType(), destination.getDescriptorForType()));
private void mergeAny(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { Descriptor descriptor = builder.getDescriptorForType(); FieldDescriptor typeUrlField = descriptor.findFieldByName("type_url"); FieldDescriptor valueField = descriptor.findFieldByName("value");
private void mergeValue(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { Descriptor type = builder.getDescriptorForType(); if (json instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) json; if (primitive.isBoolean()) { builder.setField(type.findFieldByName("bool_value"), primitive.getAsBoolean()); } else if (primitive.isNumber()) { builder.setField(type.findFieldByName("number_value"), primitive.getAsDouble()); } else { builder.setField(type.findFieldByName("string_value"), primitive.getAsString()); } } else if (json instanceof JsonObject) { FieldDescriptor field = type.findFieldByName("struct_value"); Message.Builder structBuilder = builder.newBuilderForField(field); merge(json, structBuilder); builder.setField(field, structBuilder.build()); } else if (json instanceof JsonArray) { FieldDescriptor field = type.findFieldByName("list_value"); Message.Builder listBuilder = builder.newBuilderForField(field); merge(json, listBuilder); builder.setField(field, listBuilder.build()); } else if (json instanceof JsonNull) { builder.setField( type.findFieldByName("null_value"), NullValue.NULL_VALUE.getValueDescriptor()); } else { throw new IllegalStateException("Unexpected json data: " + json); } }
private void mergeWrapper(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { Descriptor type = builder.getDescriptorForType(); FieldDescriptor field = type.findFieldByName("value"); if (field == null) { throw new InvalidProtocolBufferException("Invalid wrapper type: " + type.getFullName()); } builder.setField(field, parseFieldValue(field, json, builder)); }
private void mergeStruct(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { Descriptor descriptor = builder.getDescriptorForType(); FieldDescriptor field = descriptor.findFieldByName("fields"); if (field == null) { throw new InvalidProtocolBufferException("Invalid Struct type."); } mergeMapField(field, json, builder); }
private void mergeListValue(JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { Descriptor descriptor = builder.getDescriptorForType(); FieldDescriptor field = descriptor.findFieldByName("values"); if (field == null) { throw new InvalidProtocolBufferException("Invalid ListValue type."); } mergeRepeatedField(field, json, builder); }