Skip to content

Commit

Permalink
Merge pull request #92 from dbarfield/jsonOrdering
Browse files Browse the repository at this point in the history
[BUG] Changes to DataModelSerializer for ordering of JSON
  • Loading branch information
idlewis committed Feb 2, 2016
2 parents 88c5bf5 + 0c517ae commit 490a7b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.ibm.ws.repository.common.enums.DisplayPolicy;
import com.ibm.ws.repository.common.enums.Visibility;
import com.ibm.ws.repository.transport.client.DataModelSerializer;
import com.ibm.ws.repository.transport.client.JSONIgnore;
import com.ibm.ws.repository.transport.exceptions.BadVersionException;
import com.ibm.ws.repository.transport.model.Asset;
import com.ibm.ws.repository.transport.model.Provider;
import com.ibm.ws.repository.transport.model.WlpInformation;

public class DataModelSerializerTest {
Expand Down Expand Up @@ -422,7 +424,7 @@ public void setStringList(List<String> stringList) {
}

@Test
public void deserializePrimitives() throws Exception {
public void testDeserializePrimitives() throws Exception {

DeserialisationHelperClass dhc = DataModelSerializer.deserializeObject(new ByteArrayInputStream("{ \"intField\": 25 }".getBytes()),
DeserialisationHelperClass.class);
Expand Down Expand Up @@ -511,6 +513,59 @@ public void testDeserializeBooleans() throws Exception {
}
}

public static class JsonOrderingHelperClass {
String stringAlpha;
String stringGamma;
String stringBeta;

List<String> stringList;

public String getStringAlpha() {
return "alphaValue";
}

public String getStringGamma() {
return "gammaValue";
}

public String getStringBeta() {
return "betaValue";
}
}

@Test
public void testJsonOrdering() throws Exception {
String actual;
JsonOrderingHelperClass johc = new JsonOrderingHelperClass();
actual = DataModelSerializer.serializeAsString(johc);
String expected = "{\"stringAlpha\":\"alphaValue\",\"stringBeta\":\"betaValue\",\"stringGamma\":\"gammaValue\"}";
assertEquals("JSON was not alphabetically ordered or otherwise incorrect", expected, actual);
}

@Test
public void testJsonOrderingOfAnAsset() throws Exception {
Asset asset = new Asset();

Provider prov = new Provider();
prov.setName("IBM");
asset.setProvider(prov);

WlpInformation wlpInformation = new WlpInformation();
wlpInformation.setVisibility(Visibility.INSTALL);
wlpInformation.setDisplayPolicy(DisplayPolicy.HIDDEN);
Collection<String> providesFeature = new ArrayList<String>();
providesFeature.add("my.feature-1.0");
wlpInformation.setProvideFeature(providesFeature);

asset.setWlpInformation(wlpInformation);

String actual = DataModelSerializer.serializeAsString(asset);
String expected = "{\"provider\":{\"name\":\"IBM\"},\"wlpInformation\":{\"displayPolicy\":\"HIDDEN\",\"mainAttachmentSize\":0,\"provideFeature\":[\"my.feature-1.0\"]},\"wlpInformation2\":{\"visibility\":\"INSTALL\"}}";

assertEquals("JSON created from an asset was not in alphabetical order or otherwise incorrect",
expected, actual);
}

public static class LocaleTest {
Locale locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TimeZone;
import java.util.TreeMap;

import javax.json.Json;
import javax.json.JsonArray;
Expand Down Expand Up @@ -222,13 +224,14 @@ private static JSONArtrifactPair findFieldsToSerialize(Object o) {
throw new IllegalStateException("Data Model Error: serialization only supported for Collections of String, or other Data Model elements");
}
}

JsonArray result = arrayBuilder.build();
return new JSONArtrifactPair(result, null);
}

// object wasn't a collection.. better see what we can do with it.
JsonObjectBuilder mainObjectBuilder = Json.createObjectBuilder();
Map<String, Method> gettersFromO = new HashMap<String, Method>();
SortedMap<String, Method> gettersFromO = new TreeMap<String, Method>();
Class<? extends Object> classOfO = o.getClass();

// See if we have any breaking changes that need to go into a separate object
Expand Down

0 comments on commit 490a7b2

Please sign in to comment.