use of siena.Json in project siena by mandubian.
the class JsonSerializer method fromJson.
private static void fromJson(Json json, Element element) {
Set<String> keys = json.keys();
for (String key : keys) {
if ("@".equals(key)) {
Json value = json.get(key);
if (value.isString()) {
element.setText(value.str());
} else {
Set<String> ks = value.keys();
for (String k : ks) {
Element child = element.addElement(k);
fromJson(value.get(k), child);
}
}
} else {
element.addAttribute(key, json.get(key).str());
}
}
}
use of siena.Json in project siena by mandubian.
the class JsonTest method testCollection.
public void testCollection() {
Json list = new Json(Arrays.asList(new Object[] { 1, 2, 3 }));
assertEquals(3, list.size());
assertEquals(1, list.at(0).asInt());
assertEquals(2, list.at(1).asInt());
assertEquals(3, list.at(2).asInt());
assertEquals("[1, 2, 3]", list.toString());
}
use of siena.Json in project siena by mandubian.
the class JsonTest method testSumIntegers.
public void testSumIntegers() {
Json map1 = Json.map().put("a", 1);
Json map2 = Json.map().put("a", 2).put("b", 5);
map1.sumIntegers(map2);
assertEquals(3, map1.get("a").asInt());
assertEquals(5, map1.get("b").asInt());
}
use of siena.Json in project siena by mandubian.
the class JsonTest method testParseList.
public void testParseList() {
Json json = Json.loads("[true, false, null, 1234, \"foobar\"]");
assertTrue(json.isList());
assertEquals(5, json.size());
assertTrue(json.at(0).isBoolean());
assertTrue(json.at(0).asBoolean());
assertTrue(json.at(1).isBoolean());
assertFalse(json.at(1).asBoolean());
assertTrue(json.at(2).isNull());
assertTrue(json.at(3).isNumber());
assertEquals(1234, json.at(3).asInt());
assertTrue(json.at(4).isString());
assertEquals("foobar", json.at(4).asString());
}
use of siena.Json in project siena by mandubian.
the class JsonTest method testEqualsTo.
public void testEqualsTo() {
Json a = map();
Json b = map();
assertTrue(a.equalsTo(b));
a = list();
b = list();
assertTrue(a.equalsTo(b));
a = list("foo", "bar");
b = list("foo", "bar");
assertTrue(a.equalsTo(b));
a = map().put("foo", "bar");
b = map().put("foo", "bar");
assertTrue(a.equalsTo(b));
a = map().put("foo", list("bar"));
b = map().put("foo", list("bar"));
assertTrue(a.equalsTo(b));
a = map();
b = sortedMap();
assertTrue(a.equalsTo(b));
a = new Json(true);
b = new Json(true);
assertTrue(a.equalsTo(b));
a = new Json(1);
b = new Json(1l);
assertTrue(a.equalsTo(b));
a = new Json(1.0);
b = new Json(1l);
assertTrue(a.equalsTo(b));
a = new Json(null);
b = new Json(null);
assertTrue(a.equalsTo(b));
a = map();
b = list();
assertFalse(a.equalsTo(b));
a = list();
b = map();
assertFalse(a.equalsTo(b));
a = list(1, 2, 3, 4);
b = list(1, 2, 3);
assertFalse(a.equalsTo(b));
a = map().put("foo", 1);
b = map().put("bar", 1);
assertFalse(a.equalsTo(b));
a = new Json(1);
b = new Json(null);
assertFalse(a.equalsTo(b));
a = list(1, 3);
b = list(1, 2);
assertFalse(a.equalsTo(b));
a = map();
b = map().put("1", 2);
assertFalse(a.equalsTo(b));
a = map();
b = a;
assertTrue(a.equalsTo(b));
}
Aggregations