Skip to content

Commit 0c517ae

Browse files
committed
[BUG] Changes to DataModelSerializer for ordering of JSON
Change to ensure that DataModelSerializer produces JSON with the fields listed alphabetically. Json4j used to do that automatically but jsonp doesn't.
1 parent 88c5bf5 commit 0c517ae

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

Diff for: client-lib-tests/src/test/java/com/ibm/ws/repository/transport/client/test/DataModelSerializerTest.java

+56-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
import org.junit.BeforeClass;
4040
import org.junit.Test;
4141

42+
import com.ibm.ws.repository.common.enums.DisplayPolicy;
4243
import com.ibm.ws.repository.common.enums.Visibility;
4344
import com.ibm.ws.repository.transport.client.DataModelSerializer;
4445
import com.ibm.ws.repository.transport.client.JSONIgnore;
4546
import com.ibm.ws.repository.transport.exceptions.BadVersionException;
4647
import com.ibm.ws.repository.transport.model.Asset;
48+
import com.ibm.ws.repository.transport.model.Provider;
4749
import com.ibm.ws.repository.transport.model.WlpInformation;
4850

4951
public class DataModelSerializerTest {
@@ -422,7 +424,7 @@ public void setStringList(List<String> stringList) {
422424
}
423425

424426
@Test
425-
public void deserializePrimitives() throws Exception {
427+
public void testDeserializePrimitives() throws Exception {
426428

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

516+
public static class JsonOrderingHelperClass {
517+
String stringAlpha;
518+
String stringGamma;
519+
String stringBeta;
520+
521+
List<String> stringList;
522+
523+
public String getStringAlpha() {
524+
return "alphaValue";
525+
}
526+
527+
public String getStringGamma() {
528+
return "gammaValue";
529+
}
530+
531+
public String getStringBeta() {
532+
return "betaValue";
533+
}
534+
}
535+
536+
@Test
537+
public void testJsonOrdering() throws Exception {
538+
String actual;
539+
JsonOrderingHelperClass johc = new JsonOrderingHelperClass();
540+
actual = DataModelSerializer.serializeAsString(johc);
541+
String expected = "{\"stringAlpha\":\"alphaValue\",\"stringBeta\":\"betaValue\",\"stringGamma\":\"gammaValue\"}";
542+
assertEquals("JSON was not alphabetically ordered or otherwise incorrect", expected, actual);
543+
}
544+
545+
@Test
546+
public void testJsonOrderingOfAnAsset() throws Exception {
547+
Asset asset = new Asset();
548+
549+
Provider prov = new Provider();
550+
prov.setName("IBM");
551+
asset.setProvider(prov);
552+
553+
WlpInformation wlpInformation = new WlpInformation();
554+
wlpInformation.setVisibility(Visibility.INSTALL);
555+
wlpInformation.setDisplayPolicy(DisplayPolicy.HIDDEN);
556+
Collection<String> providesFeature = new ArrayList<String>();
557+
providesFeature.add("my.feature-1.0");
558+
wlpInformation.setProvideFeature(providesFeature);
559+
560+
asset.setWlpInformation(wlpInformation);
561+
562+
String actual = DataModelSerializer.serializeAsString(asset);
563+
String expected = "{\"provider\":{\"name\":\"IBM\"},\"wlpInformation\":{\"displayPolicy\":\"HIDDEN\",\"mainAttachmentSize\":0,\"provideFeature\":[\"my.feature-1.0\"]},\"wlpInformation2\":{\"visibility\":\"INSTALL\"}}";
564+
565+
assertEquals("JSON created from an asset was not in alphabetical order or otherwise incorrect",
566+
expected, actual);
567+
}
568+
514569
public static class LocaleTest {
515570
Locale locale;
516571

Diff for: client-lib/src/main/java/com/ibm/ws/repository/transport/client/DataModelSerializer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import java.util.Locale;
4141
import java.util.Map;
4242
import java.util.Set;
43+
import java.util.SortedMap;
4344
import java.util.TimeZone;
45+
import java.util.TreeMap;
4446

4547
import javax.json.Json;
4648
import javax.json.JsonArray;
@@ -222,13 +224,14 @@ private static JSONArtrifactPair findFieldsToSerialize(Object o) {
222224
throw new IllegalStateException("Data Model Error: serialization only supported for Collections of String, or other Data Model elements");
223225
}
224226
}
227+
225228
JsonArray result = arrayBuilder.build();
226229
return new JSONArtrifactPair(result, null);
227230
}
228231

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

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

0 commit comments

Comments
 (0)