void merge(String json, Message.Builder builder) throws InvalidProtocolBufferException { try { JsonReader reader = new JsonReader(new StringReader(json)); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
merge(json, subBuilder); --currentDepth; return subBuilder.build();
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); } }
/** * Parses from JSON into a protobuf message. * * @throws InvalidProtocolBufferException if the input is not valid JSON * format or there are unknown fields in the input. */ public void merge(String json, Message.Builder builder) throws InvalidProtocolBufferException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. new ParserImpl(registry, ignoringUnknownFields, recursionLimit).merge(json, builder); }
/** * Parses from JSON into a protobuf message. * * @throws InvalidProtocolBufferException if the input is not valid JSON * format or there are unknown fields in the input. * @throws IOException if reading from the input throws. */ public void merge(Reader json, Message.Builder builder) throws IOException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. new ParserImpl(registry, ignoringUnknownFields, recursionLimit).merge(json, builder); }