Search in sources :

Example 6 with Json

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());
        }
    }
}
Also used : Element(org.dom4j.Element) Json(siena.Json)

Example 7 with Json

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());
}
Also used : Json(siena.Json)

Example 8 with Json

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());
}
Also used : Json(siena.Json)

Example 9 with Json

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());
}
Also used : Json(siena.Json)

Example 10 with Json

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));
}
Also used : Json(siena.Json)

Aggregations

Json (siena.Json)48 SienaException (siena.SienaException)7 Field (java.lang.reflect.Field)6 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 Date (java.util.Date)3 Element (org.dom4j.Element)3 DecimalPrecision (siena.core.DecimalPrecision)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Polymorphic (siena.core.Polymorphic)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1