Search in sources :

Example 31 with Json

use of siena.Json in project siena by mandubian.

the class User method testMultiple.

@SuppressWarnings("unchecked")
public void testMultiple() throws Exception {
    Date date = createDate();
    Map<String, Contact> contacts = new HashMap<String, Contact>();
    contacts.put("id1", new Contact("Alberto", "Gimeno", Gender.MALE, date, date));
    Json data = JsonSerializer.serialize(contacts);
    contacts = (Map<String, Contact>) JsonSerializer.deserialize(Util.getField(User.class, "contacts"), data);
    Contact contact = contacts.get("id1");
    assertEquals("Alberto", contact.firstName);
    assertNull(contact.foo);
    assertEquals("Gimeno", contact.lastName);
    assertEquals(Gender.MALE, contact.gender);
    assertEquals(date, contact.birthday);
    assertEquals(date, contact.rebirthday);
}
Also used : HashMap(java.util.HashMap) Json(siena.Json) Date(java.util.Date)

Example 32 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testFormatAndParseNumbers.

public void testFormatAndParseNumbers() {
    Json json = Json.loads("[1.0, 2.0, 3, 4, 5, 6.1, 1.0e12, 2e12]");
    String s = json.toString();
    assertTrue(s.contains("1.0"));
    assertTrue(s.contains("2.0"));
    assertFalse(s.contains("3.0"));
    assertFalse(s.contains("4.0"));
    assertFalse(s.contains("5.0"));
    assertTrue(s.contains("6.1"));
    assertTrue(s.contains("1.0E12"));
    assertTrue(s.contains("2.0E12"));
}
Also used : Json(siena.Json)

Example 33 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testContains.

public void testContains() {
    Json json = Json.list(1, 2, 3);
    assertTrue(json.contains(1));
    assertTrue(json.contains(2));
    assertTrue(json.contains(3));
    assertFalse(json.contains(4));
}
Also used : Json(siena.Json)

Example 34 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testParseComplex.

public void testParseComplex() {
    Json json = null;
    json = Json.loads("{}");
    assertEquals("{}", json.toString());
    json = Json.loads("[{}]");
    assertEquals("[{}]", json.toString());
    json = Json.loads("{}");
    assertEquals("{}", json.toString());
    json = Json.loads("[{}, {}, {}]");
    assertEquals("[{}, {}, {}]", json.toString());
    json = Json.loads("{\"foo\": 1234, \"bar\": [1, 2, 3, [{}], true, false, null, {}]}");
    assertTrue(json.isMap());
    assertEquals(2, json.size());
    assertEquals(1234, json.get("foo").asInt());
    Json list = json.get("bar");
    assertTrue(list.isList());
    assertEquals(8, list.size());
    assertEquals(1, list.at(0).asInt());
    assertEquals(2, list.at(1).asInt());
    assertEquals(3, list.at(2).asInt());
    assertTrue(list.at(3).isList());
    assertEquals(1, list.at(3).size());
    assertTrue(list.at(3).at(0).isMap());
    assertTrue(list.at(3).at(0).isEmpty());
    assertTrue(list.at(4).asBoolean());
    assertFalse(list.at(5).asBoolean());
    assertTrue(list.at(6).isNull());
    assertTrue(list.at(7).isMap());
    assertTrue(list.at(7).isEmpty());
}
Also used : Json(siena.Json)

Example 35 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testMap.

public void testMap() {
    Map<String, Object> m = new HashMap<String, Object>();
    m.put("foo", 1);
    Json map = new Json(m);
    assertTrue(map.isMap());
    assertEquals(1, map.size());
    assertEquals(1, map.get("foo").asInt());
    assertEquals("{\"foo\": 1}", map.toString());
}
Also used : HashMap(java.util.HashMap) 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