Search in sources :

Example 41 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testParseMap.

public void testParseMap() {
    Json json = Json.loads("{\"foo\": 1234, \"bar\": true}");
    assertTrue(json.isMap());
    assertEquals(2, json.size());
    Json foo = json.get("foo");
    assertTrue(foo.isNumber());
    assertEquals(1234, foo.asInt());
    Json bar = json.get("bar");
    assertTrue(bar.isBoolean());
    assertEquals(true, bar.asBoolean());
}
Also used : Json(siena.Json)

Example 42 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testBuildComplex.

public void testBuildComplex() {
    Json json = Json.list(1, 2, 3, 4, true, false, null, Json.map().put("foo", 1).put("bar", 2));
    assertEquals(1, json.at(0).asInt());
    assertEquals(2, json.at(1).asInt());
    assertEquals(3, json.at(2).asInt());
    assertEquals(4, json.at(3).asInt());
    assertTrue(json.at(4).asBoolean());
    assertFalse(json.at(5).asBoolean());
    assertTrue(json.at(6).isNull());
    assertTrue(json.at(7).isMap());
    assertEquals(2, json.at(7).size());
    assertEquals(1, json.at(7).get("foo").asInt());
    assertEquals(2, json.at(7).get("bar").asInt());
}
Also used : Json(siena.Json)

Example 43 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method sumReal.

public void sumReal() {
    Json map1 = Json.map().put("a", 1.5);
    map1.sumReal("a", 2.5);
    assertEquals(4, map1.get("a").asInt());
    map1 = Json.map();
    map1.sumReal("a", 2.5);
    assertEquals(2.5, map1.get("a").asInt());
}
Also used : Json(siena.Json)

Example 44 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testPutComplex.

public void testPutComplex() {
    List<Integer> list1 = Arrays.asList(new Integer[] { 1, 2, 3 });
    List<String> list2 = Arrays.asList(new String[] { "foo", "bar", "baz" });
    List<List<?>> objects = new ArrayList<List<?>>();
    ;
    objects.add(list1);
    objects.add(list2);
    Json map = Json.map().put("foo", objects);
    Json list = map.get("foo");
    assertEquals(2, list.size());
    assertEquals(3, list.at(0).size());
    assertEquals(1, list.at(0).at(0).asInt());
    assertEquals(2, list.at(0).at(1).asInt());
    assertEquals(3, list.at(0).at(2).asInt());
    assertEquals(3, list.at(1).size());
    assertEquals("foo", list.at(1).at(0).str());
    assertEquals("bar", list.at(1).at(1).str());
    assertEquals("baz", list.at(1).at(2).str());
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Json(siena.Json)

Example 45 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testSortedMap.

public void testSortedMap() {
    Json json = Json.sortedMap().put("1", 1).put("400", 400).put("3", 3).put("2", 2);
    Iterator<String> keys = json.keys().iterator();
    assertEquals("1", keys.next());
    assertEquals("2", keys.next());
    assertEquals("3", keys.next());
    assertEquals("400", keys.next());
}
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