Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Unwrapped Serialization. #79

Merged
merged 6 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 @@ public MessageSerializer(ExtensionRegistryWrapper extensionRegistry) {
}

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

public MessageSerializer(ProtobufJacksonConfig config, boolean unwrappingSerializer) {
this(config, null, unwrappingSerializer);
}

public MessageSerializer(ProtobufJacksonConfig config, NameTransformer nameTransformer, boolean unwrappingSerializer) {
jhaber marked this conversation as resolved.
Show resolved Hide resolved
super(MessageOrBuilder.class);
this.config = config;
this.unwrappingSerializer = unwrappingSerializer;
this.propertyNamingCache = new ConcurrentHashMap<>();
}

Expand All @@ -51,7 +61,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 +111,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, nameTransformer, 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;
}
}
}