com.google.protobuf.Descriptors.FileDescriptor.Syntax Java Examples

The following examples show how to use com.google.protobuf.Descriptors.FileDescriptor.Syntax. 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: Descriptors.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/** Get the syntax of the .proto file. */
public Syntax getSyntax() {
  if (Syntax.PROTO3.name.equals(proto.getSyntax())) {
    return Syntax.PROTO3;
  }
  return Syntax.PROTO2;
}
 
Example #2
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/** For internal use only. */
public boolean needsUtf8Check() {
  if (type != Type.STRING) {
    return false;
  }
  if (getContainingType().getOptions().getMapEntry()) {
    // Always enforce strict UTF-8 checking for map fields.
    return true;
  }
  if (getFile().getSyntax() == Syntax.PROTO3) {
    return true;
  }
  return getFile().getOptions().getJavaStringCheckUtf8();
}
 
Example #3
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/** Does this field have the {@code [packed = true]} option or is this field
 *  packable in proto3 and not explicitly setted to unpacked?
 */
@Override
public boolean isPacked() {
  if (!isPackable()) {
    return false;
  }
  if (getFile().getSyntax() == FileDescriptor.Syntax.PROTO2) {
    return getOptions().getPacked();
  } else {
    return !getOptions().hasPacked() || getOptions().getPacked();
  }
}
 
Example #4
Source File: ProtobufUtil.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively walks the given {@link Message} class and verifies that every field or message
 * linked in uses the Protocol Buffers proto2 syntax.
 */
static void checkProto2Syntax(Class<? extends Message> clazz, ExtensionRegistry registry) {
  for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) {
    Syntax s = d.getFile().getSyntax();
    checkArgument(
        s == Syntax.PROTO2,
        "Message %s or one of its dependencies does not use proto2 syntax: %s in file %s",
        clazz.getName(),
        d.getFullName(),
        d.getFile().getName());
  }
}
 
Example #5
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
Syntax(String name) {
  this.name = name;
}
 
Example #6
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
boolean supportsUnknownEnumValue() {
  return getSyntax() == Syntax.PROTO3;
}
 
Example #7
Source File: MessageSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(
        MessageOrBuilder message,
        JsonGenerator generator,
        SerializerProvider serializerProvider
) throws IOException {
  generator.writeStartObject();

  boolean proto3 = message.getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3;
  Include include = serializerProvider.getConfig().getDefaultPropertyInclusion().getValueInclusion();
  boolean writeDefaultValues = proto3 && include != Include.NON_DEFAULT;
  boolean writeEmptyCollections = include != Include.NON_DEFAULT && include != Include.NON_EMPTY;
  PropertyNamingStrategyBase namingStrategy =
          new PropertyNamingStrategyWrapper(serializerProvider.getConfig().getPropertyNamingStrategy());

  Descriptor descriptor = message.getDescriptorForType();
  List<FieldDescriptor> fields = new ArrayList<>(descriptor.getFields());
  if (message instanceof ExtendableMessageOrBuilder<?>) {
    for (ExtensionInfo extensionInfo : config.extensionRegistry().getExtensionsByDescriptor(descriptor)) {
      fields.add(extensionInfo.descriptor);
    }
  }

  for (FieldDescriptor field : fields) {
    if (field.isRepeated()) {
      List<?> valueList = (List<?>) message.getField(field);

      if (!valueList.isEmpty() || writeEmptyCollections) {
        if (field.isMapField()) {
          generator.writeFieldName(namingStrategy.translate(field.getName()));
          writeMap(field, valueList, generator, serializerProvider);
        } else if (valueList.size() == 1 && writeSingleElementArraysUnwrapped(serializerProvider)) {
          generator.writeFieldName(namingStrategy.translate(field.getName()));
          writeValue(field, valueList.get(0), generator, serializerProvider);
        } else {
          generator.writeArrayFieldStart(namingStrategy.translate(field.getName()));
          for (Object subValue : valueList) {
            writeValue(field, subValue, generator, serializerProvider);
          }
          generator.writeEndArray();
        }
      }
    } else if (message.hasField(field) || (writeDefaultValues && !supportsFieldPresence(field) && field.getContainingOneof() == null)) {
      generator.writeFieldName(namingStrategy.translate(field.getName()));
      writeValue(field, message.getField(field), generator, serializerProvider);
    } else if (include == Include.ALWAYS && field.getContainingOneof() == null) {
      generator.writeFieldName(namingStrategy.translate(field.getName()));
      generator.writeNull();
    }
  }

  generator.writeEndObject();
}