Skip to content

Commit

Permalink
Merge pull request #227 from ctrimble/feature_unique-items-order
Browse files Browse the repository at this point in the history
Use LinkedHashSet when Deserializing Set.
  • Loading branch information
joelittlejohn committed Jul 25, 2014
2 parents 8ac4b15 + 249c873 commit c1ca17d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.jsonschema2pojo;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter;
Expand All @@ -25,6 +27,7 @@
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.annotate.JsonValue;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -57,6 +60,9 @@ public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
@Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
field.annotate(JsonProperty.class).param("value", propertyName);
if (field.type().erasure().equals(field.type().owner().ref(Set.class))) {
field.annotate(JsonDeserialize.class).param("as", LinkedHashSet.class);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.jsonschema2pojo;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
Expand All @@ -27,6 +29,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.sun.codemodel.JAnnotationArrayMember;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
Expand Down Expand Up @@ -57,6 +60,9 @@ public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
@Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
field.annotate(JsonProperty.class).param("value", propertyName);
if (field.type().erasure().equals(field.type().owner().ref(Set.class))) {
field.annotate(JsonDeserialize.class).param("as", LinkedHashSet.class);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@

package org.jsonschema2pojo.integration;

import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.generateAndCompile;
import static org.junit.Assert.assertThat;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.junit.BeforeClass;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ArrayIT {

private static Class<?> classWithArrayProperties;
Expand Down Expand Up @@ -169,4 +175,65 @@ public void propertiesThatReferenceAnArraySchemaAlwaysHaveCorrectCollectionType(
assertThat(array2GenericType.getName(), is("com.example.RootArrayItem"));
}

@Test
public void uniqueArrayPreservesOrderJackson2() throws Exception {
new PreserveOrder("jackson2") {
@Override
protected Object roundTrip(Object original) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(mapper.writeValueAsString(original), original.getClass());
}
}.test();

}

@Test
public void uniqueArrayPreservesOrderJackson1() throws Exception {
new PreserveOrder("jackson1") {
@Override
protected Object roundTrip(Object original) throws Exception {
org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
return mapper.readValue(mapper.writeValueAsString(original), original.getClass());
}
}.test();

}

static abstract class PreserveOrder {
String annotationStyle;

public PreserveOrder(String annotationStyle) {
this.annotationStyle = annotationStyle;
}

public void test() throws Exception {
ClassLoader resultsClassLoader = generateAndCompile(
"/schema/array/typeWithArrayProperties.json",
"com.example",
config("annotationStyle", annotationStyle));

Class<?> jackson1Class = resultsClassLoader.loadClass("com.example.TypeWithArrayProperties");

Object original = jackson1Class.newInstance();

@SuppressWarnings("unchecked")
Set<Integer> expected = (Set<Integer>) jackson1Class.getMethod("getUniqueIntegerArray").invoke(original);
expected.addAll(java.util.Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10));

Object roundTrip = roundTrip(original);
@SuppressWarnings("unchecked")
Set<Integer> actual = (Set<Integer>) jackson1Class.getMethod("getUniqueIntegerArray").invoke(roundTrip);

Iterator<Integer> expectedItr = expected.iterator();
Iterator<Integer> actualItr = actual.iterator();

while (expectedItr.hasNext()) {
assertThat("The collection order is stable", actualItr.next(), equalTo(expectedItr.next()));
}
assertThat("All of the values were examined", actualItr.hasNext(), equalTo(false));

}

protected abstract Object roundTrip(Object original) throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"type" : "boolean"
}
},
"uniqueIntegerArray" : {
"type" : "array",
"uniqueItems" : true,
"items" : {
"type" : "integer"
}
},
"nonUniqueArrayByDefault" : {
"type" : "array",
"items" : {
Expand Down

0 comments on commit c1ca17d

Please sign in to comment.