print((Message) value); break;
printedField = true; printField(field.getKey(), field.getValue());
/** Prints google.protobuf.Struct */ private void printStruct(MessageOrBuilder message) throws IOException { Descriptor descriptor = message.getDescriptorForType(); FieldDescriptor field = descriptor.findFieldByName("fields"); if (field == null) { throw new InvalidProtocolBufferException("Invalid Struct type."); } // Struct is formatted as a map object. printMapFieldValue(field, message.getField(field)); }
} else { print(contentMessage, typeUrl);
printSingleFieldValue(keyField, entryKey, true); generator.print(":" + blankOrSpace); printSingleFieldValue(valueField, entryValue);
/** * Converts a protobuf message to JSON format. * * @throws InvalidProtocolBufferException if the message contains Any types that can't be * resolved. * @throws IOException if writing to the output fails. */ public void appendTo(MessageOrBuilder message, Appendable output) throws IOException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. new PrinterImpl( registry, alwaysOutputDefaultValueFields, includingDefaultValueFields, preservingProtoFieldNames, output, omittingInsignificantWhitespace, printingEnumsAsInts) .print(message); }
/** Prints google.protobuf.Value */ private void printValue(MessageOrBuilder message) throws IOException { // For a Value message, only the value of the field is formatted. Map<FieldDescriptor, Object> fields = message.getAllFields(); if (fields.isEmpty()) { // No value set. generator.print("null"); return; } // A Value message can only have at most one field set (it only contains // an oneof). if (fields.size() != 1) { throw new InvalidProtocolBufferException("Invalid Value type."); } for (Map.Entry<FieldDescriptor, Object> entry : fields.entrySet()) { printSingleFieldValue(entry.getKey(), entry.getValue()); } }
private void printSingleFieldValue(FieldDescriptor field, Object value) throws IOException { printSingleFieldValue(field, value, false); }
@SuppressWarnings("rawtypes") private void printRepeatedFieldValue(FieldDescriptor field, Object value) throws IOException { generator.print("["); boolean printedElement = false; for (Object element : (List) value) { if (printedElement) { generator.print("," + blankOrSpace); } else { printedElement = true; } printSingleFieldValue(field, element); } generator.print("]"); }
private void printField(FieldDescriptor field, Object value) throws IOException { if (preservingProtoFieldNames) { generator.print("\"" + field.getName() + "\":" + blankOrSpace); } else { generator.print("\"" + field.getJsonName() + "\":" + blankOrSpace); } if (field.isMapField()) { printMapFieldValue(field, value); } else if (field.isRepeated()) { printRepeatedFieldValue(field, value); } else { printSingleFieldValue(field, value); } }
/** Prints google.protobuf.FieldMask */ private void printFieldMask(MessageOrBuilder message) throws IOException { FieldMask value = FieldMask.parseFrom(toByteString(message)); generator.print("\"" + FieldMaskUtil.toJsonString(value) + "\""); }
/** Prints google.protobuf.Timestamp */ private void printTimestamp(MessageOrBuilder message) throws IOException { Timestamp value = Timestamp.parseFrom(toByteString(message)); generator.print("\"" + Timestamps.toString(value) + "\""); }
/** Prints google.protobuf.ListValue */ private void printListValue(MessageOrBuilder message) throws IOException { Descriptor descriptor = message.getDescriptorForType(); FieldDescriptor field = descriptor.findFieldByName("values"); if (field == null) { throw new InvalidProtocolBufferException("Invalid ListValue type."); } printRepeatedFieldValue(field, message.getField(field)); }
void print(MessageOrBuilder message) throws IOException { WellKnownTypePrinter specialPrinter = wellKnownTypePrinters.get(message.getDescriptorForType().getFullName()); if (specialPrinter != null) { specialPrinter.print(this, message); return; } print(message, null); }
/** Prints google.protobuf.Duration */ private void printDuration(MessageOrBuilder message) throws IOException { Duration value = Duration.parseFrom(toByteString(message)); generator.print("\"" + Durations.toString(value) + "\""); }
/** Prints wrapper types (e.g., google.protobuf.Int32Value) */ private void printWrapper(MessageOrBuilder message) throws IOException { Descriptor descriptor = message.getDescriptorForType(); FieldDescriptor valueField = descriptor.findFieldByName("value"); if (valueField == null) { throw new InvalidProtocolBufferException("Invalid Wrapper type."); } // When formatting wrapper types, we just print its value field instead of // the whole message. printSingleFieldValue(valueField, message.getField(valueField)); }