Skip to content
Merged
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
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2019.
* (C) Copyright IBM Corp. 2019, 2020.
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
Expand Down Expand Up @@ -67,6 +67,9 @@
*
* <p>This class includes code that was adapted from the internal <code>ReflectiveTypeAdapterFactory</code> and
* <code>MapTypeAdapterFactory</code> classes from Gson.
*
* <p>This class will explicitly serialize null values found within dynamic (additional) properties, regardless of
* the global "serialize nulls" setting in Gson.
*/
public class DynamicModelTypeAdapterFactory implements TypeAdapterFactory {
private static final Logger LOGGER = Logger.getLogger(DynamicModelTypeAdapterFactory.class.getName());
Expand Down Expand Up @@ -331,9 +334,17 @@ public void write(JsonWriter out, T value) throws IOException {
}

// Next, serialize each of the map entries.
for (String key : ((DynamicModel<?>) value).getPropertyNames()) {
out.name(String.valueOf(key));
mapValueTypeAdapter.write(out, ((DynamicModel<?>) value).get(key));
// When serializing the map entries (i.e. additional/dynamic properties) we want
// to explicitly serialize null values regardless of the global Gson "serialize nulls" setting.
boolean serializeNulls = out.getSerializeNulls();
out.setSerializeNulls(true);
try {
for (String key : ((DynamicModel<?>) value).getPropertyNames()) {
out.name(String.valueOf(key));
mapValueTypeAdapter.write(out, ((DynamicModel<?>) value).get(key));
}
} finally {
out.setSerializeNulls(serializeNulls);
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2015, 2019.
* (C) Copyright IBM Corp. 2015, 2020.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -54,15 +54,18 @@ private <T> T deserialize(String json, Class<T> clazz) {
return GsonSingleton.getGson().fromJson(json, clazz);
}

private <T> void testSerDeser(DynamicModel<?> model, Class<T> clazz) {
String jsonString = serialize(model);
private void display(String msg) {
if (displayOutput) {
System.out.println("serialized " + model.getClass().getSimpleName() + ": " + jsonString);
System.out.println(msg);
}
}

private <T> void testSerDeser(DynamicModel<?> model, Class<T> clazz) {
String jsonString = serialize(model);

display("serialized " + model.getClass().getSimpleName() + ": " + jsonString);
T newModel = deserialize(jsonString, clazz);
if (displayOutput) {
System.out.println("de-serialized " + model.getClass().getSimpleName() + ": " + newModel.toString());
}
display("de-serialized " + model.getClass().getSimpleName() + ": " + newModel.toString());
assertEquals(newModel, model);
}

Expand Down Expand Up @@ -203,19 +206,31 @@ public void testNoCtor() {
public void testNullValues() {
ModelAPFoo model = createModelAPFoo();
model.setProp1(null);
// model.put("basketball", "foo");
testSerDeser(model, ModelAPFoo.class);
}

@Test
public void testAddlPropsNull() {
ModelAPString model = createModelAPString();
model.put("basketball", null);

String json = serialize(model);
display("Serialized: " + json);
assertTrue(json.contains("\"basketball\": null"));

ModelAPString newModel = deserialize(json, ModelAPString.class);
assertEquals(newModel, model);
}

@Test(expectedExceptions = {JsonSyntaxException.class})
public void testBadDeser() {

// Obtain the json string and then render it incorrect to trigger a deserialization error.
ModelAPFoo model = createModelAPFoo();
String goodJson = serialize(model);
if (displayOutput) System.out.println("Serialized ModelAPFoo: " + goodJson);
display("Serialized ModelAPFoo: " + goodJson);
String badJson = goodJson.replaceAll("foo", "FOO").replaceAll("prop2", "var2");
if (displayOutput) System.out.println("Incorrect JSON: " + badJson);
display("Incorrect JSON: " + badJson);

// We just need to try to deserialize the bad JSON string to trigger the exception.
deserialize(badJson, ModelAPFoo.class);
Expand Down