Skip to content

configurable wrapping of single links/resources #115

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -93,18 +93,29 @@ public Jackson1HalModule() {
public static class HalLinkListSerializer extends ContainerSerializerBase<List<Link>> implements
ContextualSerializer<List<Link>> {

private boolean wrapSingleElemLists;
private final BeanProperty property;

/**
* Creates a new {@link HalLinkListSerializer}.
*/
public HalLinkListSerializer() {
this(null);
this(null, false);
}

@Deprecated
public HalLinkListSerializer(BeanProperty property) {
this(property, false);
}

public HalLinkListSerializer(boolean wrapSingleElemLists) {
this(null, wrapSingleElemLists);
}

protected HalLinkListSerializer(BeanProperty property, boolean wrapSingleElemLists) {
super(List.class, false);
this.property = property;
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand Down Expand Up @@ -133,7 +144,7 @@ public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider p
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);

MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null, null,
provider.findKeySerializer(keyType, null), new OptionalListSerializer(property));
provider.findKeySerializer(keyType, null), new OptionalListSerializer(property, wrapSingleElemLists));

serializer.serialize(sortedLinks, jgen, provider);
}
Expand All @@ -145,7 +156,7 @@ public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider p
@Override
public JsonSerializer<List<Link>> createContextual(SerializationConfig config, BeanProperty property)
throws JsonMappingException {
return new HalLinkListSerializer(property);
return new HalLinkListSerializer(property, wrapSingleElemLists);
}

/*
Expand All @@ -169,19 +180,27 @@ public static class HalResourcesSerializer extends ContainerSerializerBase<Colle

private final BeanProperty property;
private final RelProvider relProvider;
private boolean wrapSingleElemLists;

/**
* Creates a new {@link HalLinkListSerializer}.
*/
public HalResourcesSerializer(RelProvider relProvider) {
this(null, relProvider);
public HalResourcesSerializer(RelProvider relPorvider) {
this(relPorvider, false);
}

public HalResourcesSerializer(RelProvider relPorvider, boolean wrapSingleElemLists) {
this(null, relPorvider, wrapSingleElemLists);
}

@Deprecated
public HalResourcesSerializer(BeanProperty property, RelProvider relProvider) {
this(property, relProvider, false);
}

protected HalResourcesSerializer(BeanProperty property, RelProvider relProvider, boolean wrapSingleElemLists) {
super(Collection.class, false);

this.property = property;
this.relProvider = relProvider;
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand All @@ -204,7 +223,7 @@ public void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvide
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);

MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null, null,
provider.findKeySerializer(keyType, null), new OptionalListSerializer(property));
provider.findKeySerializer(keyType, null), new OptionalListSerializer(property, wrapSingleElemLists));

serializer.serialize(builder.asMap(), jgen, provider);
}
Expand All @@ -216,7 +235,7 @@ public void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvide
@Override
public JsonSerializer<Collection<?>> createContextual(SerializationConfig config, BeanProperty property)
throws JsonMappingException {
return new HalResourcesSerializer(property, relProvider);
return new HalResourcesSerializer(property, relProvider, wrapSingleElemLists);
}

/*
Expand All @@ -239,6 +258,8 @@ public ContainerSerializerBase<?> _withValueTypeSerializer(TypeSerializer vts) {
*/
public static class OptionalListSerializer extends ContainerSerializerBase<Object> {

private boolean wrapSingleElemLists = false;

private final BeanProperty property;
private JsonSerializer<Object> serializer;

Expand All @@ -247,9 +268,17 @@ public OptionalListSerializer() {
}

public OptionalListSerializer(BeanProperty property) {
this(property, false);
}

public OptionalListSerializer(BeanProperty property, boolean wrapSingleElemLists) {
super(List.class, false);
this.property = property;
this.wrapSingleElemLists = wrapSingleElemLists;
}

public void setWrapSingleElemLists(boolean wrapSingleElemLists) {
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand All @@ -271,7 +300,7 @@ public void serialize(Object value, JsonGenerator jgen, SerializerProvider provi

List<?> list = (List<?>) value;

if (list.size() == 1) {
if (list.size() == 1 && !wrapSingleElemLists) {
serializeContents(list.iterator(), jgen, provider);
return;
}
Expand Down Expand Up @@ -452,9 +481,14 @@ public static class HalHandlerInstantiator extends HandlerInstantiator {

public HalHandlerInstantiator(RelProvider relProvider) {

Assert.notNull(relProvider, "RelProvider must not be null!");
this.instanceMap.put(HalResourcesSerializer.class, new HalResourcesSerializer(null, relProvider));
this(relProvider, false);
}

public HalHandlerInstantiator(RelProvider relProvider, boolean wrapSingleElemLists) {

Assert.notNull(relProvider, "RelProvider must not be null!");
this.instanceMap.put(HalResourcesSerializer.class, new HalResourcesSerializer(relProvider, wrapSingleElemLists));
this.instanceMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(wrapSingleElemLists));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,27 @@ public Jackson2HalModule() {
*/
public static class HalLinkListSerializer extends ContainerSerializer<List<Link>> implements ContextualSerializer {

private boolean wrapSingleElemLists;

private final BeanProperty property;

public HalLinkListSerializer() {
this(null);
this(null, false);
}

@Deprecated
public HalLinkListSerializer(BeanProperty property) {
this(property, false);
}

public HalLinkListSerializer(boolean wrapSingleElemLists) {
this(null, wrapSingleElemLists);
}

protected HalLinkListSerializer(BeanProperty property, boolean wrapSingleElemLists) {
super(List.class, false);
this.property = property;
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand Down Expand Up @@ -127,7 +138,7 @@ public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider p
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);

MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null,
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property));
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property, wrapSingleElemLists));

serializer.serialize(sortedLinks, jgen, provider);
}
Expand All @@ -141,7 +152,7 @@ public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider p
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
throws JsonMappingException {
return new HalLinkListSerializer(property);
return new HalLinkListSerializer(property, wrapSingleElemLists);
}

/*
Expand Down Expand Up @@ -206,17 +217,27 @@ public static class HalResourcesSerializer extends ContainerSerializer<Collectio

private final BeanProperty property;
private final RelProvider relProvider;
private boolean wrapSingleElemLists;

public HalResourcesSerializer(RelProvider relPorvider) {
this(null, relPorvider);
this(relPorvider, false);
}

public HalResourcesSerializer(RelProvider relPorvider, boolean wrapSingleElemLists) {
this(null, relPorvider, wrapSingleElemLists);
}

@Deprecated
public HalResourcesSerializer(BeanProperty property, RelProvider relProvider) {
this(property, relProvider, false);
}

protected HalResourcesSerializer(BeanProperty property, RelProvider relProvider, boolean wrapSingleElemLists) {
super(Collection.class, false);

this.property = property;
this.relProvider = relProvider;
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand All @@ -241,15 +262,15 @@ public void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvide
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);

MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null,
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property));
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property, wrapSingleElemLists));

serializer.serialize(builder.asMap(), jgen, provider);
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException {
return new HalResourcesSerializer(property, relProvider);
return new HalResourcesSerializer(property, relProvider, wrapSingleElemLists);
}

@Override
Expand Down Expand Up @@ -293,6 +314,8 @@ protected ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
public static class OptionalListJackson2Serializer extends ContainerSerializer<Object> implements
ContextualSerializer {

private boolean wrapSingleElemLists = false;

private final BeanProperty property;
private JsonSerializer<Object> serializer;

Expand All @@ -306,9 +329,18 @@ public OptionalListJackson2Serializer() {
* @param property
*/
public OptionalListJackson2Serializer(BeanProperty property) {
this(property, false);
}

/**
* Creates a new {@link OptionalListJackson2Serializer} using the given {@link BeanProperty}.
*
* @param property
*/
public OptionalListJackson2Serializer(BeanProperty property, boolean wrapSingleElemLists) {
super(List.class, false);
this.property = property;
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
Expand All @@ -322,6 +354,10 @@ public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
throw new UnsupportedOperationException("not implemented");
}

public void setWrapSingleElemLists(boolean wrapSingleElemLists) {
this.wrapSingleElemLists = wrapSingleElemLists;
}

/*
* (non-Javadoc)
*
Expand All @@ -334,7 +370,7 @@ public void serialize(Object value, JsonGenerator jgen, SerializerProvider provi

List<?> list = (List<?>) value;

if (list.size() == 1) {
if (list.size() == 1 && !wrapSingleElemLists) {
serializeContents(list.iterator(), jgen, provider);
return;
}
Expand Down Expand Up @@ -409,7 +445,7 @@ public boolean isEmpty(Object arg0) {
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
throws JsonMappingException {
return new OptionalListJackson2Serializer(property);
return new OptionalListJackson2Serializer(property, wrapSingleElemLists);
}
}

Expand Down Expand Up @@ -563,8 +599,14 @@ public static class HalHandlerInstantiator extends HandlerInstantiator {

public HalHandlerInstantiator(RelProvider resolver) {

this(resolver, false);
}

public HalHandlerInstantiator(RelProvider resolver, boolean wrapSingleElemLists) {

Assert.notNull(resolver, "RelProvider must not be null!");
this.instanceMap.put(HalResourcesSerializer.class, new HalResourcesSerializer(null, resolver));
this.instanceMap.put(HalResourcesSerializer.class, new HalResourcesSerializer(resolver, wrapSingleElemLists));
this.instanceMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(wrapSingleElemLists));
}

private Object findInstance(Class<?> type) {
Expand Down
Loading