|
Java example source code file (JsonTreeTest.java)
This example Java source code file (JsonTreeTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Learn more about this Java project at its project page.
The JsonTreeTest.java Java example source code
package com.google.gson.functional;
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;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import junit.framework.TestCase;
/**
* Functional tests for {@link Gson#toJsonTree(Object)} and
* {@link Gson#toJsonTree(Object, java.lang.reflect.Type)}
*
* @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 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 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 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);
}
public void testJsonTreeNull() {
BagOfPrimitives bag = new BagOfPrimitives(10L, 5, false, null);
JsonObject jsonElement = (JsonObject) gson.toJsonTree(bag, BagOfPrimitives.class);
assertFalse(jsonElement.has("stringValue"));
}
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 {
@SuppressWarnings("unused")
float f = 1.2F;
public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) {
super(l, i, b, string);
this.f = f;
}
}
}
Other Java examples (source code examples)
Here is a short list of links related to this Java JsonTreeTest.java source code file:
|