Java Code Examples for com.google.protobuf.Descriptors.FieldDescriptor#isOptional()

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#isOptional() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtensionRegistry.java    From travelguide with Apache License 2.0 8 votes vote down vote up
private void add(final ExtensionInfo extension) {
  if (!extension.descriptor.isExtension()) {
    throw new IllegalArgumentException(
      "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
      "(non-extension) field.");
  }

  extensionsByName.put(extension.descriptor.getFullName(), extension);
  extensionsByNumber.put(
    new DescriptorIntPair(extension.descriptor.getContainingType(),
                          extension.descriptor.getNumber()),
    extension);

  final FieldDescriptor field = extension.descriptor;
  if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
      field.getType() == FieldDescriptor.Type.MESSAGE &&
      field.isOptional() &&
      field.getExtensionScope() == field.getMessageType()) {
    // This is an extension of a MessageSet type defined within the extension
    // type's own scope.  For backwards-compatibility, allow it to be looked
    // up by type name.
    extensionsByName.put(field.getMessageType().getFullName(), extension);
  }
}
 
Example 2
Source File: MessageToMap.java    From krpc with Apache License 2.0 6 votes vote down vote up
static public Map<FieldDescriptor, Object> getFields(MessageOrBuilder message, boolean withDefaultValue) {
    if (!withDefaultValue) {
        return message.getAllFields();
    }
    Map<FieldDescriptor, Object> fieldsToPrint = new LinkedHashMap<>();
    for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
        if (field.isOptional()) {
            if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE && !message.hasField(field)) {
                continue;
            }
            if (field.getJavaType() == FieldDescriptor.JavaType.STRING && !message.hasField(field)) {
                continue;
            }
        }
        fieldsToPrint.put(field, message.getField(field));
    }
    return fieldsToPrint;
}
 
Example 3
Source File: ExtensionRegistry.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void add(final ExtensionInfo extension) {
  if (!extension.descriptor.isExtension()) {
    throw new IllegalArgumentException(
      "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
      "(non-extension) field.");
  }

  extensionsByName.put(extension.descriptor.getFullName(), extension);
  extensionsByNumber.put(
    new DescriptorIntPair(extension.descriptor.getContainingType(),
                          extension.descriptor.getNumber()),
    extension);

  final FieldDescriptor field = extension.descriptor;
  if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
      field.getType() == FieldDescriptor.Type.MESSAGE &&
      field.isOptional() &&
      field.getExtensionScope() == field.getMessageType()) {
    // This is an extension of a MessageSet type defined within the extension
    // type's own scope.  For backwards-compatibility, allow it to be looked
    // up by type name.
    extensionsByName.put(field.getMessageType().getFullName(), extension);
  }
}
 
Example 4
Source File: XmlJavaxFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private void printSingleField(FieldDescriptor field, Object value, 
        XMLStreamWriter generator) throws XMLStreamException, IOException {
    
    if (field.isExtension()) {
        generator.writeStartElement(EXTENSION_ELEMENT);
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
                && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
                // object equality
                && (field.getExtensionScope() == field.getMessageType())) {

            generator.writeAttribute(EXTENSION_TYPE, field.getMessageType().getFullName());
        } else {
        	// extensions will have '.' in them, while normal fields wont..
        	generator.writeAttribute(EXTENSION_TYPE, field.getFullName());
        }
    } else {
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.writeStartElement(field.getMessageType().getName());
        } else {
            generator.writeStartElement(field.getName());
        }
    }

    
    // Done with the name, on to the value
    printFieldValue(field, value, generator);
    
    generator.writeEndElement();
}
 
Example 5
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                                     Object value,
                                     JsonGenerator generator) throws IOException {
    if (field.isExtension()) {
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
            && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
            // object equality
            && (field.getExtensionScope() == field.getMessageType())) {
            generator.writeFieldName(field.getMessageType().getFullName());
        } else {
        	// extensions will have '.' in them, while normal fields wont..
        	generator.writeFieldName(field.getFullName());
        }
    } else {
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.writeFieldName(field.getMessageType().getName());
        } else {
            generator.writeFieldName(field.getName());
        }
    }

    // Done with the name, on to the value
    if (field.isRepeated()) {
        // Repeated field. Print each element.
        generator.writeStartArray();
        for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
            printFieldValue(field, iter.next(), generator);
        }
        generator.writeEndArray();
    } else {
        printFieldValue(field, value, generator);
    }
}
 
Example 6
Source File: MetaDataEvolutionValidator.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private void validateField(@Nonnull FieldDescriptor oldFieldDescriptor, @Nonnull FieldDescriptor newFieldDescriptor,
                           @Nonnull Set<Pair<Descriptor, Descriptor>> seenDescriptors) {
    if (!oldFieldDescriptor.getName().equals(newFieldDescriptor.getName())) {
        // TODO: Field renaming should be allowed with some caveats about if the field is indexed or not
        throw new MetaDataException("field renamed",
                LogMessageKeys.OLD_FIELD_NAME, oldFieldDescriptor.getName(),
                LogMessageKeys.NEW_FIELD_NAME, newFieldDescriptor.getName());
    }
    if (!oldFieldDescriptor.getType().equals(newFieldDescriptor.getType())) {
        validateTypeChange(oldFieldDescriptor, newFieldDescriptor);
    }
    if (oldFieldDescriptor.isRequired() && !newFieldDescriptor.isRequired()) {
        throw new MetaDataException("required field is no longer required",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    } else if (oldFieldDescriptor.isOptional() && !newFieldDescriptor.isOptional()) {
        // TODO: In theory, optional -> repeated is okay, but only if the field is not indexed
        throw new MetaDataException("optional field is no longer optional",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    } else if (oldFieldDescriptor.isRepeated() && !newFieldDescriptor.isRepeated()) {
        throw new MetaDataException("repeated field is no longer repeated",
                LogMessageKeys.FIELD_NAME, oldFieldDescriptor.getName());
    }
    if (oldFieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) {
        validateEnum(newFieldDescriptor.getName(), oldFieldDescriptor.getEnumType(), newFieldDescriptor.getEnumType());
    }
    if (oldFieldDescriptor.getType().equals(FieldDescriptor.Type.GROUP) || oldFieldDescriptor.getType().equals(FieldDescriptor.Type.MESSAGE)) {
        // Message types need to be validated against each other as well.
        final Descriptor oldMessageType = oldFieldDescriptor.getMessageType();
        final Descriptor newMessageType = newFieldDescriptor.getMessageType();
        validateMessage(oldMessageType, newMessageType, seenDescriptors);
    }
}
 
Example 7
Source File: TextFormat.java    From travelguide with Apache License 2.0 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 8
Source File: ProtoGenerator.java    From fuchsia with Apache License 2.0 4 votes vote down vote up
private void generateProtoFromDescriptor(FieldDescriptor descriptor,
                                         Appendable out, String indent, Map<Descriptor, Boolean> descriptors)
        throws IOException {
    out.append(indent);
    if (descriptor.isRequired()) {
        out.append("required ");
    }

    if (descriptor.isOptional()) {
        out.append("optional ");
    }

    if (descriptor.isRepeated()) {
        out.append("repeated ");
    }

    if (descriptor.getType().equals(Type.MESSAGE)) {
        out.append(descriptor.getMessageType().getFullName() + " ");
        Descriptor messageType = descriptor.getMessageType();
        if (descriptors.get(messageType) == null) {
            descriptors.put(messageType, false);
        }
    } else if (descriptor.getType().equals(Type.ENUM)) {
        out.append(descriptor.getEnumType().getFullName() + " ");
    } else {
        out.append(descriptor.getType().toString().toLowerCase() + " ");
    }

    out.append(descriptor.getName() + " = " + descriptor.getNumber());

    if (descriptor.hasDefaultValue()) {
        out.append(" [default = ");
        Object defaultValue = descriptor.getDefaultValue();

        if (defaultValue instanceof EnumValueDescriptor) {
            out.append(((EnumValueDescriptor) defaultValue).getName());
        }

        out.append("]");
    }

    out.append(";\n");
}
 
Example 9
Source File: TextFormat.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 10
Source File: TextFormat.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 11
Source File: ProtobufJsonFormat.java    From incubator-tajo with Apache License 2.0 4 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                              Object value,
                              JsonGenerator generator) throws IOException {
  if (field.isExtension()) {
    generator.print("\"");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("\"");
  } else {
    generator.print("\"");
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
    generator.print("\"");
  }

  // Done with the name, on to the value

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    generator.print(": ");
    generator.indent();
  } else {
    generator.print(": ");
  }


  if (field.isRepeated()) {
    // Repeated field. Print each element.
    generator.print("[");
    for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
      printFieldValue(field, iter.next(), generator);
      if (iter.hasNext()) {
        generator.print(",");
      }
    }
    generator.print("]");
  } else {
    printFieldValue(field, value, generator);
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      generator.outdent();
    }
  }
}
 
Example 12
Source File: ProtobufJsonFormat.java    From tajo with Apache License 2.0 4 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                              Object value,
                              JsonGenerator generator) throws IOException {
  if (field.isExtension()) {
    generator.print("\"");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("\"");
  } else {
    generator.print("\"");
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
    generator.print("\"");
  }

  // Done with the name, on to the value

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    generator.print(": ");
    generator.indent();
  } else {
    generator.print(": ");
  }


  if (field.isRepeated()) {
    // Repeated field. Print each element.
    generator.print("[");
    for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
      printFieldValue(field, iter.next(), generator);
      if (iter.hasNext()) {
        generator.print(",");
      }
    }
    generator.print("]");
  } else {
    printFieldValue(field, value, generator);
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      generator.outdent();
    }
  }
}
 
Example 13
Source File: JsonFormat.java    From compiler with Apache License 2.0 4 votes vote down vote up
private static void printSingleField(FieldDescriptor field,
                                       Object value,
                                       JsonGenerator generator) throws IOException {
      if (field.isExtension()) {
          generator.print("\"");
          // We special-case MessageSet elements for compatibility with proto1.
          if (field.getContainingType().getOptions().getMessageSetWireFormat()
              && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
              // object equality
              && (field.getExtensionScope() == field.getMessageType())) {
              generator.print(field.getMessageType().getFullName());
          } else {
              generator.print(field.getFullName());
          }
          generator.print("\"");
      } else {
          generator.print("\"");
          if (field.getType() == FieldDescriptor.Type.GROUP) {
              // Groups must be serialized with their original capitalization.
              generator.print(field.getMessageType().getName());
          } else {
              generator.print(field.getName());
          }
          generator.print("\"");
      }

      // Done with the name, on to the value

generator.print(": ");

      if (field.isRepeated()) {
          // Repeated field. Print each element.
          generator.print("[\n");
	generator.indent();
          for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
              printFieldValue(field, iter.next(), generator);
              if (iter.hasNext()) {
                  generator.print(",\n");
              }
          }
	generator.outdent();
          generator.print("\n");
          generator.print("]");
      } else {
          printFieldValue(field, value, generator);
      }
  }
 
Example 14
Source File: ExtensionRegistry.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void add(
    final ExtensionInfo extension,
    final Extension.ExtensionType extensionType) {
  if (!extension.descriptor.isExtension()) {
    throw new IllegalArgumentException(
        "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
            "(non-extension) field.");
  }

  Map<String, ExtensionInfo> extensionsByName;
  Map<DescriptorIntPair, ExtensionInfo> extensionsByNumber;
  switch (extensionType) {
    case IMMUTABLE:
      extensionsByName = immutableExtensionsByName;
      extensionsByNumber = immutableExtensionsByNumber;
      break;
    case MUTABLE:
      extensionsByName = mutableExtensionsByName;
      extensionsByNumber = mutableExtensionsByNumber;
      break;
    default:
      // Ignore the unknown supported type.
      return;
  }

  extensionsByName.put(extension.descriptor.getFullName(), extension);
  extensionsByNumber.put(
    new DescriptorIntPair(extension.descriptor.getContainingType(),
                          extension.descriptor.getNumber()),
    extension);

  final FieldDescriptor field = extension.descriptor;
  if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
      field.getType() == FieldDescriptor.Type.MESSAGE &&
      field.isOptional() &&
      field.getExtensionScope() == field.getMessageType()) {
    // This is an extension of a MessageSet type defined within the extension
    // type's own scope.  For backwards-compatibility, allow it to be looked
    // up by type name.
    extensionsByName.put(field.getMessageType().getFullName(), extension);
  }
}
 
Example 15
Source File: TextFormat.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 16
Source File: ExtensionRegistry.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
private void add(
    final ExtensionInfo extension,
    final Extension.ExtensionType extensionType) {
  if (!extension.descriptor.isExtension()) {
    throw new IllegalArgumentException(
        "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
            "(non-extension) field.");
  }

  Map<String, ExtensionInfo> extensionsByName;
  Map<DescriptorIntPair, ExtensionInfo> extensionsByNumber;
  switch (extensionType) {
    case IMMUTABLE:
      extensionsByName = immutableExtensionsByName;
      extensionsByNumber = immutableExtensionsByNumber;
      break;
    case MUTABLE:
      extensionsByName = mutableExtensionsByName;
      extensionsByNumber = mutableExtensionsByNumber;
      break;
    default:
      // Ignore the unknown supported type.
      return;
  }

  extensionsByName.put(extension.descriptor.getFullName(), extension);
  extensionsByNumber.put(
    new DescriptorIntPair(extension.descriptor.getContainingType(),
                          extension.descriptor.getNumber()),
    extension);

  final FieldDescriptor field = extension.descriptor;
  if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
      field.getType() == FieldDescriptor.Type.MESSAGE &&
      field.isOptional() &&
      field.getExtensionScope() == field.getMessageType()) {
    // This is an extension of a MessageSet type defined within the extension
    // type's own scope.  For backwards-compatibility, allow it to be looked
    // up by type name.
    extensionsByName.put(field.getMessageType().getFullName(), extension);
  }
}
 
Example 17
Source File: TextFormat.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
private void printSingleField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isExtension()) {
    generator.print("[");
    // We special-case MessageSet elements for compatibility with proto1.
    if (field.getContainingType().getOptions().getMessageSetWireFormat()
        && (field.getType() == FieldDescriptor.Type.MESSAGE)
        && (field.isOptional())
        // object equality
        && (field.getExtensionScope() == field.getMessageType())) {
      generator.print(field.getMessageType().getFullName());
    } else {
      generator.print(field.getFullName());
    }
    generator.print("]");
  } else {
    if (field.getType() == FieldDescriptor.Type.GROUP) {
      // Groups must be serialized with their original capitalization.
      generator.print(field.getMessageType().getName());
    } else {
      generator.print(field.getName());
    }
  }

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print(" { ");
    } else {
      generator.print(" {\n");
      generator.indent();
    }
  } else {
    generator.print(": ");
  }

  printFieldValue(field, value, generator);

  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    if (singleLineMode) {
      generator.print("} ");
    } else {
      generator.outdent();
      generator.print("}\n");
    }
  } else {
    if (singleLineMode) {
      generator.print(" ");
    } else {
      generator.print("\n");
    }
  }
}
 
Example 18
Source File: JsonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                                     Object value,
                                     JsonGenerator generator) throws IOException {
    if (field.isExtension()) {
        generator.print("\"");
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
            && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
            // object equality
            && (field.getExtensionScope() == field.getMessageType())) {
            generator.print(field.getMessageType().getFullName());
        } else {
            generator.print(field.getFullName());
        }
        generator.print("\"");
    } else {
        generator.print("\"");
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.print(field.getMessageType().getName());
        } else {
            generator.print(field.getName());
        }
        generator.print("\"");
    }

    // Done with the name, on to the value

    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        generator.print(": ");
        generator.indent();
    } else {
        generator.print(": ");
    }


    if (field.isRepeated()) {
        // Repeated field. Print each element.
        generator.print("[");
        for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
            printFieldValue(field, iter.next(), generator);
            if (iter.hasNext()) {
                generator.print(",");
            }
        }
        generator.print("]");
    } else {
        printFieldValue(field, value, generator);
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
            generator.outdent();
        }
    }
}
 
Example 19
Source File: HtmlFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private void printSingleField(FieldDescriptor field,
                                     Object value,
                                     HtmlGenerator generator) throws IOException {
    if (field.isExtension()) {
        generator.print("[<span style=\"");
        generator.print(FIELD_NAME_STYLE);
        generator.print("\">");
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
                        && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
                        // object equality
                        && (field.getExtensionScope() == field.getMessageType())) {
            generator.print(field.getMessageType().getFullName());
        } else {
            generator.print(field.getFullName());
        }
        generator.print("</span>]");
    } else {
        generator.print("<span style=\"");
        generator.print(FIELD_NAME_STYLE);
        generator.print("\">");
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.print(field.getMessageType().getName());
        } else {
            generator.print(field.getName());
        }
        generator.print("</span>");
    }

    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        generator.print(" <span style=\"color: red;\">{</span><br/>");
        generator.indent();
    } else {
        generator.print(": ");
    }

    printFieldValue(field, value, generator);

    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        generator.outdent();
        generator.print("<span style=\"color: red;\">}</span>");
    }
    generator.print("<br/>");
}
 
Example 20
Source File: JsonFormat.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void printSingleField(FieldDescriptor field,
                                     Object value, JsonGenerator generator, boolean selfType) throws IOException {
    if (field.isExtension()) {
        generator.print("\"");
        // We special-case MessageSet elements for compatibility with proto1.
        if (field.getContainingType().getOptions().getMessageSetWireFormat()
                && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
                // object equality
                && (field.getExtensionScope() == field.getMessageType())) {
            generator.print(field.getMessageType().getFullName());
        } else {
            generator.print(field.getFullName());
        }
        generator.print("\"");
    } else {
        generator.print("\"");
        if (field.getType() == FieldDescriptor.Type.GROUP) {
            // Groups must be serialized with their original capitalization.
            generator.print(field.getMessageType().getName());
        } else {
            generator.print(field.getName());
        }
        generator.print("\"");
    }

    // Done with the name, on to the value

    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        generator.print(": ");
        generator.indent();
    } else {
        generator.print(": ");
    }

    if (field.isRepeated()) {
        // Repeated field. Print each element.
        generator.print("[");
        for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext(); ) {
            printFieldValue(field, iter.next(), generator, selfType);
            if (iter.hasNext()) {
                generator.print(",");
            }
        }
        generator.print("]");
    } else {
        printFieldValue(field, value, generator, selfType);
        if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
            generator.outdent();
        }
    }
}