com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase Java Examples

The following examples show how to use com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase. 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: PropertyNamingStrategyWrapper.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
public PropertyNamingStrategyWrapper(PropertyNamingStrategy delegate) {
  if (delegate instanceof PropertyNamingStrategyBase) {
    this.delegate = (PropertyNamingStrategyBase) delegate;
  } else if (delegate == PropertyNamingStrategy.LOWER_CAMEL_CASE) {
    this.delegate = NO_OP;
  } else {
    this.delegate = SNAKE_TO_CAMEL;
  }
}
 
Example #2
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 #3
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();
}