com.google.protobuf.ExtensionRegistry.ExtensionInfo Java Examples

The following examples show how to use com.google.protobuf.ExtensionRegistry.ExtensionInfo. 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: ExtensionRegistryWrapper.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
private ExtensionRegistryWrapper(final ExtensionRegistry extensionRegistry) {
  this.extensionFunction = new Function<Descriptor, Set<ExtensionInfo>>() {
    private final Map<Descriptor, Set<ExtensionInfo>> extensionCache = new ConcurrentHashMap<>();

    @Override
    public Set<ExtensionInfo> apply(Descriptor descriptor) {
      Set<ExtensionInfo> cached = extensionCache.get(descriptor);
      if (cached != null) {
        return cached;
      }

      Set<ExtensionInfo> extensions =
              extensionRegistry.getAllImmutableExtensionsByExtendedType(descriptor.getFullName());
      extensionCache.put(descriptor, extensions);
      return extensions;
    }
  };
}
 
Example #2
Source File: JsonInclusionTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() {
  allFields = new HashSet<>();
  arrayFields = new HashSet<>();

  Descriptor descriptor = AllFields.getDescriptor();
  for (FieldDescriptor field : descriptor.getFields()) {
    allFields.add(translate(field.getName()));
    if (field.isRepeated()) {
      arrayFields.add(translate(field.getName()));
    }
  }

  allExtensionFields = new HashSet<>();
  arrayExtensionFields = new HashSet<>();

  ExtensionRegistryWrapper extensionRegistry = ExtensionRegistryWrapper.wrap(EXTENSION_REGISTRY);
  for (ExtensionInfo extensionInfo : extensionRegistry.getExtensionsByDescriptor(descriptor)) {
    allExtensionFields.add(translate(extensionInfo.descriptor.getName()));
    if (extensionInfo.descriptor.isRepeated()) {
      arrayExtensionFields.add(translate(extensionInfo.descriptor.getName()));
    }
  }
}
 
Example #3
Source File: ExtensionRegistryWrapper.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
private ExtensionRegistryWrapper() {
  this.extensionFunction = new Function<Descriptor, Set<ExtensionInfo>>() {

    @Override
    public Set<ExtensionInfo> apply(Descriptor descriptor) {
      return Collections.emptySet();
    }
  };
}
 
Example #4
Source File: MessageDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
private Map<String, ExtensionInfo> buildExtensionLookup(Descriptor descriptor, DeserializationContext context) {
  PropertyNamingStrategyBase namingStrategy =
          new PropertyNamingStrategyWrapper(context.getConfig().getPropertyNamingStrategy());

  Map<String, ExtensionInfo> extensionLookup = new HashMap<>();
  for (ExtensionInfo extensionInfo : config.extensionRegistry().getExtensionsByDescriptor(descriptor)) {
    extensionLookup.put(namingStrategy.translate(extensionInfo.descriptor.getName()), extensionInfo);
  }

  return extensionLookup;
}
 
Example #5
Source File: DataDstPbHelper.java    From xresloader with MIT License 4 votes vote down vote up
private static <T extends com.google.protobuf.GeneratedMessageV3.ExtendableMessage<T>> HashMap<String, Object> dumpMessageExtensions(
        T msg, Map<Descriptors.FieldDescriptor, Object> options,
        com.google.protobuf.ExtensionRegistry custom_extensions) {
    HashMap<String, Object> ret = null;
    if (null == custom_extensions) {
        return ret;
    }

    if (options != null && !options.isEmpty()) {
        ret = new HashMap<String, Object>();
        for (Map.Entry<Descriptors.FieldDescriptor, Object> kv : options.entrySet()) {
            ret.put(kv.getKey().getName(), convertMessageFieldIntoObject(kv.getKey(), kv.getValue(), true));
        }
    }

    Map<Integer, com.google.protobuf.UnknownFieldSet.Field> all_unknown_fields = msg.getUnknownFields().asMap();
    if (all_unknown_fields.isEmpty()) {
        return ret;
    }

    Set<ExtensionInfo> all_ext_types = custom_extensions
            .getAllImmutableExtensionsByExtendedType(msg.getDescriptorForType().getFullName());

    if (all_ext_types.isEmpty()) {
        return ret;
    }

    for (ExtensionInfo ext_type : all_ext_types) {
        com.google.protobuf.UnknownFieldSet.Field field_data = all_unknown_fields
                .getOrDefault(ext_type.descriptor.getNumber(), null);
        if (field_data == null) {
            continue;
        }

        if (ret != null && ret.containsKey(ext_type.descriptor.getName())) {
            continue;
        }

        try {
            Object val;
            val = pickMessageField(field_data, ext_type.descriptor, ext_type.defaultInstance, true);
            if (null != val) {
                if (ret == null) {
                    ret = new HashMap<String, Object>();
                }
                ret.put(ext_type.descriptor.getName(), val);
            }
        } catch (InvalidProtocolBufferException | UnsupportedEncodingException e) {
            // Ignore error
        }
    }

    return ret;
}
 
Example #6
Source File: ExtensionRegistryWrapper.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
/**
 * @deprecated use {@link #getExtensionsByDescriptor(Descriptor)}
 */
@Deprecated
public List<ExtensionInfo> findExtensionsByDescriptor(Descriptor descriptor) {
  return new ArrayList<>(getExtensionsByDescriptor(descriptor));
}
 
Example #7
Source File: ExtensionRegistryWrapper.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
public Set<ExtensionInfo> getExtensionsByDescriptor(Descriptor descriptor) {
  return extensionFunction.apply(descriptor);
}
 
Example #8
Source File: MessageDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Override
protected void populate(
        V builder,
        JsonParser parser,
        DeserializationContext context
) throws IOException {
  JsonToken token = parser.getCurrentToken();
  if (token == JsonToken.START_ARRAY) {
    token = parser.nextToken();
  }

  switch (token) {
    case END_OBJECT:
      return;
    case START_OBJECT:
      token = parser.nextToken();
      if (token == JsonToken.END_OBJECT) {
        return;
      }
      break;
    default:
      break; // make findbugs happy
  }

  final Descriptor descriptor = builder.getDescriptorForType();
  final Map<String, FieldDescriptor> fieldLookup = buildFieldLookup(descriptor, context);
  final Map<String, ExtensionInfo> extensionLookup;
  if (builder instanceof ExtendableMessageOrBuilder<?>) {
    extensionLookup = buildExtensionLookup(descriptor, context);
  } else {
    extensionLookup = Collections.emptyMap();
  }

  do {
    if (!token.equals(JsonToken.FIELD_NAME)) {
      throw reportWrongToken(JsonToken.FIELD_NAME, context, "");
    }

    String name = parser.getCurrentName();
    FieldDescriptor field = fieldLookup.get(name);
    Message defaultInstance = null;
    if (field == null) {
      ExtensionInfo extensionInfo = extensionLookup.get(name);
      if (extensionInfo != null) {
        field = extensionInfo.descriptor;
        defaultInstance = extensionInfo.defaultInstance;
      }
    }

    if (field == null) {
      context.handleUnknownProperty(parser, this, builder, name);
      parser.nextToken();
      parser.skipChildren();
      continue;
    }

    parser.nextToken();
    setField(builder, field, defaultInstance, parser, context);
  } while ((token = parser.nextToken()) != JsonToken.END_OBJECT);
}
 
Example #9
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();
}