Search in sources :

Example 16 with Person

use of jodd.json.mock.Person in project jodd by oblac.

the class JSONDeserializerTest method testArray.

@Test
public void testArray() {
    Person[] p = new Person[3];
    p[0] = creator.createJodder();
    p[1] = creator.createDilbert();
    p[2] = creator.createModesty();
    String json = new JsonSerializer().serialize(p);
    Person[] jsonP = new JsonParser().parse(json, Person[].class);
    assertEquals(3, jsonP.length);
    assertEquals("Igor", jsonP[0].getFirstname());
    assertEquals("Dilbert", jsonP[1].getFirstname());
    assertEquals("Modesty", jsonP[2].getFirstname());
}
Also used : DateJsonSerializer(jodd.json.impl.DateJsonSerializer) Person(jodd.json.mock.Person) Test(org.junit.Test)

Example 17 with Person

use of jodd.json.mock.Person in project jodd by oblac.

the class JSONSerializationTest method testCopyOnWriteList.

@Test
public void testCopyOnWriteList() {
    CopyOnWriteArrayList<Person> people = new CopyOnWriteArrayList<>();
    people.add(jodder);
    people.add(modesty);
    String json = new JsonSerializer().serialize(people);
    assertAttribute("firstname", json);
    assertStringValue("Igor", json);
    assertStringValue("Modesty", json);
}
Also used : ObjectJsonSerializer(jodd.json.impl.ObjectJsonSerializer) Person(jodd.json.mock.Person) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 18 with Person

use of jodd.json.mock.Person in project jodd by oblac.

the class JSONSerializationTest method testObject.

@Test
public void testObject() {
    JoddJson.classMetadataName = "class";
    JsonSerializer serializer = new JsonSerializer();
    String jodderJson = serializer.serialize(jodder);
    assertStringValue(Person.class.getName(), jodderJson);
    assertAttribute("firstname", jodderJson);
    assertStringValue("Igor", jodderJson);
    assertAttribute("lastname", jodderJson);
    assertStringValue("Spasic", jodderJson);
    assertAttribute("work", jodderJson);
    assertAttribute("home", jodderJson);
    assertAttribute("street", jodderJson);
    assertStringValue(Address.class.getName(), jodderJson);
    assertAttribute("zipcode", jodderJson);
    assertStringValue(Zipcode.class.getName(), jodderJson);
    assertAttributeMissing("person", jodderJson);
    assertAttributeMissing("phones", jodderJson);
    assertStringValueMissing(Phone.class.getName(), jodderJson);
    assertAttributeMissing("hobbies", jodderJson);
    JsonSerializer jdrSerializer = new JsonSerializer();
    jdrSerializer.exclude("home", "work");
    String modestyJson = jdrSerializer.serialize(modesty);
    assertStringValue(Person.class.getName(), modestyJson);
    assertAttribute("firstname", modestyJson);
    assertStringValue("Modesty", modestyJson);
    assertAttribute("lastname", modestyJson);
    assertStringValue("Blase", modestyJson);
    assertAttribute("birthdate", modestyJson);
    assertStringValueMissing(Address.class.getName(), modestyJson);
    assertAttributeMissing("work", modestyJson);
    assertAttributeMissing("home", modestyJson);
    assertAttributeMissing("street", modestyJson);
    assertAttributeMissing("city", modestyJson);
    assertAttributeMissing("state", modestyJson);
    assertStringValueMissing(Zipcode.class.getName(), modestyJson);
    assertAttributeMissing("zipcode", modestyJson);
    assertStringValueMissing(Phone.class.getName(), modestyJson);
    assertAttributeMissing("hobbies", modestyJson);
    assertAttributeMissing("person", modestyJson);
    serializer.exclude("home.zipcode", "work.zipcode");
    String json2 = serializer.serialize(jodder);
    assertStringValue(Person.class.getName(), json2);
    assertAttribute("work", json2);
    assertAttribute("home", json2);
    assertAttribute("street", json2);
    assertStringValue(Address.class.getName(), json2);
    assertAttributeMissing("zipcode", json2);
    assertAttributeMissing("phones", json2);
    assertStringValueMissing(Zipcode.class.getName(), json2);
    assertStringValueMissing(Phone.class.getName(), json2);
    assertAttributeMissing("hobbies", json2);
    assertAttributeMissing("type", json2);
    assertStringValueMissing("PAGER", json2);
    serializer.include("hobbies").exclude(true, "phones.areaCode", "phones.exchange", "phones.number");
    String json3 = serializer.serialize(jodder);
    assertStringValue(Person.class.getName(), json3);
    assertAttribute("work", json3);
    assertAttribute("home", json3);
    assertAttribute("street", json3);
    assertStringValue(Address.class.getName(), json3);
    assertAttribute("phones", json3);
    assertAttribute("phoneNumber", json3);
    assertStringValue(Phone.class.getName(), json3);
    assertAttribute("hobbies", json3);
    assertAttributeMissing("zipcode", json3);
    assertAttributeMissing(Zipcode.class.getName(), json3);
    assertAttributeMissing("areaCode", json3);
    assertAttributeMissing("exchange", json3);
    assertAttributeMissing("number", json3);
    assertAttribute("type", json3);
    assertStringValue("PAGER", json3);
    assertTrue(json3.startsWith("{"));
    assertTrue(json3.endsWith("}"));
}
Also used : Address(jodd.json.mock.Address) Phone(jodd.json.mock.Phone) Zipcode(jodd.json.mock.Zipcode) ObjectJsonSerializer(jodd.json.impl.ObjectJsonSerializer) Person(jodd.json.mock.Person) Test(org.junit.Test)

Example 19 with Person

use of jodd.json.mock.Person in project jodd by oblac.

the class JSONDeserializerTest method testListDeserializationNoClass.

@Test
public void testListDeserializationNoClass() {
    JoddJson.classMetadataName = "class";
    Person modesty = creator.createModesty();
    Person igor = creator.createJodder();
    Person pedro = creator.createPedro();
    List<Person> list = new ArrayList<>(3);
    list.add(modesty);
    list.add(igor);
    list.add(pedro);
    String json = new JsonSerializer().exclude("*.class").serialize(list);
    List<Person> people = new JsonParser().map("values", Person.class).parse(json);
    assertEquals(ArrayList.class, people.getClass());
    assertEquals(3, list.size());
    assertEquals(modesty.getFirstname(), list.get(0).getFirstname());
    assertEquals(igor.getFirstname(), list.get(1).getFirstname());
    assertEquals(pedro.getFirstname(), list.get(2).getFirstname());
}
Also used : ArrayList(java.util.ArrayList) DateJsonSerializer(jodd.json.impl.DateJsonSerializer) Person(jodd.json.mock.Person) Test(org.junit.Test)

Aggregations

Person (jodd.json.mock.Person)19 Test (org.junit.Test)15 DateJsonSerializer (jodd.json.impl.DateJsonSerializer)11 Address (jodd.json.mock.Address)5 Zipcode (jodd.json.mock.Zipcode)5 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 HashMap (java.util.HashMap)4 Phone (jodd.json.mock.Phone)3 Timestamp (java.sql.Timestamp)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ObjectJsonSerializer (jodd.json.impl.ObjectJsonSerializer)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Employee (jodd.json.mock.Employee)1 Group (jodd.json.mock.Group)1 Before (org.junit.Before)1