2009-04-03 23:33:01 +02:00
|
|
|
package com.google.gson.functional;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
|
|
|
|
|
|
|
/**
|
2009-08-07 22:58:41 +02:00
|
|
|
* Functional tests for {@link Gson#toJsonTree(Object)} and
|
|
|
|
* {@link Gson#toJsonTree(Object, java.lang.reflect.Type)}
|
2009-04-03 23:33:01 +02:00
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class JsonTreeTest extends TestCase {
|
|
|
|
private Gson gson;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
gson = new Gson();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testToJsonTree() {
|
|
|
|
BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, "foo");
|
|
|
|
JsonElement json = gson.toJsonTree(bag);
|
|
|
|
assertTrue(json.isJsonObject());
|
|
|
|
JsonObject obj = json.getAsJsonObject();
|
|
|
|
Set<Entry<String, JsonElement>> children = obj.entrySet();
|
|
|
|
assertEquals(4, children.size());
|
|
|
|
assertContains(obj, new JsonPrimitive(10L));
|
|
|
|
assertContains(obj, new JsonPrimitive(5));
|
|
|
|
assertContains(obj, new JsonPrimitive(false));
|
|
|
|
assertContains(obj, new JsonPrimitive("foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testToJsonTreeObjectType() {
|
|
|
|
SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
|
|
|
|
JsonElement json = gson.toJsonTree(bag, BagOfPrimitives.class);
|
|
|
|
assertTrue(json.isJsonObject());
|
|
|
|
JsonObject obj = json.getAsJsonObject();
|
|
|
|
Set<Entry<String, JsonElement>> children = obj.entrySet();
|
|
|
|
assertEquals(4, children.size());
|
|
|
|
assertContains(obj, new JsonPrimitive(10L));
|
|
|
|
assertContains(obj, new JsonPrimitive(5));
|
|
|
|
assertContains(obj, new JsonPrimitive(false));
|
|
|
|
assertContains(obj, new JsonPrimitive("foo"));
|
|
|
|
}
|
|
|
|
|
2009-08-07 22:58:41 +02:00
|
|
|
public void testJsonTreeToString() {
|
|
|
|
SubTypeOfBagOfPrimitives bag = new SubTypeOfBagOfPrimitives(10L, 5, false, "foo", 1.4F);
|
|
|
|
String json1 = gson.toJson(bag);
|
|
|
|
JsonElement jsonElement = gson.toJsonTree(bag, SubTypeOfBagOfPrimitives.class);
|
|
|
|
String json2 = gson.toJson(jsonElement);
|
|
|
|
assertEquals(json1, json2);
|
|
|
|
}
|
|
|
|
|
2009-04-03 23:33:01 +02:00
|
|
|
private void assertContains(JsonObject json, JsonPrimitive child) {
|
|
|
|
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
|
|
|
|
JsonElement node = entry.getValue();
|
|
|
|
if (node.isJsonPrimitive()) {
|
|
|
|
if (node.getAsJsonPrimitive().equals(child)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SubTypeOfBagOfPrimitives extends BagOfPrimitives {
|
2009-09-23 19:45:16 +02:00
|
|
|
@SuppressWarnings("unused")
|
2009-09-23 21:09:01 +02:00
|
|
|
private float f = 1.2F;
|
2009-04-03 23:33:01 +02:00
|
|
|
public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) {
|
|
|
|
super(l, i, b, string);
|
|
|
|
this.f = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|