Skip to content

Commit

Permalink
Merge pull request #79 from sankala-dremio/master
Browse files Browse the repository at this point in the history
Added support for Unwrapped Serialization.
  • Loading branch information
jhaber authored Aug 1, 2022
2 parents 8cff548 + cd44004 commit 11a4eea
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.util.NameTransformer;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
Expand All @@ -28,6 +29,7 @@
public class MessageSerializer extends ProtobufSerializer<MessageOrBuilder> {
@SuppressFBWarnings(value="SE_BAD_FIELD")
private final ProtobufJacksonConfig config;
private final boolean unwrappingSerializer;
private final Map<Descriptor, PropertyNamingCache> propertyNamingCache;

/**
Expand All @@ -39,9 +41,13 @@ public MessageSerializer(ExtensionRegistryWrapper extensionRegistry) {
}

public MessageSerializer(ProtobufJacksonConfig config) {
super(MessageOrBuilder.class);
this(config, false);
}

private MessageSerializer(ProtobufJacksonConfig config, boolean unwrappingSerializer) {
super(MessageOrBuilder.class);
this.config = config;
this.unwrappingSerializer = unwrappingSerializer;
this.propertyNamingCache = new ConcurrentHashMap<>();
}

Expand All @@ -51,7 +57,9 @@ public void serialize(
JsonGenerator generator,
SerializerProvider serializerProvider
) throws IOException {
generator.writeStartObject();
if (!isUnwrappingSerializer()) {
generator.writeStartObject();
}

boolean proto3 = message.getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3;
Include include = serializerProvider.getConfig().getDefaultPropertyInclusion().getValueInclusion();
Expand Down Expand Up @@ -99,7 +107,19 @@ public void serialize(
}
}

generator.writeEndObject();
if (!isUnwrappingSerializer()) {
generator.writeEndObject();
}
}

@Override
public boolean isUnwrappingSerializer() {
return unwrappingSerializer;
}

@Override
public MessageSerializer unwrappingSerializer(NameTransformer nameTransformer) {
return new MessageSerializer(config, true);
}

private Function<FieldDescriptor, String> getPropertyNaming(Descriptor descriptor, SerializerProvider serializerProvider) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.hubspot.jackson.datatype.protobuf.builtin;

import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.camelCase;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.protobuf.Value;
import com.hubspot.jackson.datatype.protobuf.util.BuiltInProtobufs;

public class UnwrappedSerializationTest {
@Test
public void itWritesUnwrappedNullValue() throws IOException {
BuiltInProtobufs.HasValue message = BuiltInProtobufs.HasValue
.newBuilder()
.setValue(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
.build();
ValueBean bean = new ValueBean();
bean.setHasValue(message);
String json = camelCase().writeValueAsString(bean);
assertThat(json).isEqualTo("{\"value\":null}");
}

@Test
public void itWritesUnwrappedListValue() throws IOException {
ListValue list = ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("test").build()).build();
BuiltInProtobufs.HasValue message = BuiltInProtobufs.HasValue
.newBuilder()
.setValue(Value.newBuilder().setListValue(list).build())
.build();
ValueBean bean = new ValueBean();
bean.setHasValue(message);
String json = camelCase().writeValueAsString(bean);
assertThat(json).isEqualTo("{\"value\":[\"test\"]}");
}

public static class ValueBean {
@JsonUnwrapped
private BuiltInProtobufs.HasValue hasValue;

public BuiltInProtobufs.HasValue getHasValue() {
return hasValue;
}
public void setHasValue(BuiltInProtobufs.HasValue hasValue) {
this.hasValue = hasValue;
}
}
}

0 comments on commit 11a4eea

Please sign in to comment.