Search in sources :

Example 11 with Person

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

the class JSONDeserializerTest method testDeserializeWithIncludes.

@Test
public void testDeserializeWithIncludes() {
    Person igor = creator.createJodder();
    String json = new JsonSerializer().include("phones", "hobbies").serialize(igor);
    Person jsonIgor = new JsonParser().parse(json, Person.class);
    assertEquals(2, jsonIgor.getPhones().size());
    assertEquals(0, jsonIgor.getHobbies().size());
}
Also used : DateJsonSerializer(jodd.json.impl.DateJsonSerializer) Person(jodd.json.mock.Person) Test(org.junit.Test)

Example 12 with Person

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

the class JSONDeserializerTest method testUnixEpoch.

@Test
public void testUnixEpoch() {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.set(Calendar.YEAR, 1970);
    cal.set(Calendar.MONTH, 0);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.AM_PM, Calendar.AM);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Person hank = new Person("Hank", "Paulsen", cal.getTime(), null, null);
    String json = new JsonSerializer().serialize(hank);
    Person deHank = new JsonParser().parse(json, Person.class);
    assertEquals(hank.getFirstname(), deHank.getFirstname());
    assertEquals(hank.getLastname(), deHank.getLastname());
    assertEquals(hank.getBirthdate(), deHank.getBirthdate());
}
Also used : Calendar(java.util.Calendar) DateJsonSerializer(jodd.json.impl.DateJsonSerializer) Person(jodd.json.mock.Person) Test(org.junit.Test)

Example 13 with Person

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

the class JSONDeserializerTest method testDateTransforming.

@Test
public void testDateTransforming() throws ParseException {
    final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    Person foo = new Person("Foo", "Bar", new Date(), null, null);
    foo.setBirthdate(df.parse("2009/01/02"));
    String json = new JsonSerializer().withSerializer("birthdate", new DateJsonSerializer() {

        @Override
        public boolean serialize(JsonContext jsonContext, Date date) {
            jsonContext.writeString(df.format(date));
            return true;
        }
    }).serialize(foo);
    Person newUser = new JsonParser().withValueConverter("birthdate", new ValueConverter<String, Date>() {

        public Date convert(String data) {
            try {
                return df.parse(data);
            } catch (ParseException pe) {
                throw new JsonException(pe);
            }
        }
    }).parse(json, Person.class);
    assertEquals(foo.getBirthdate(), newUser.getBirthdate());
    assertEquals("2009/01/02", df.format(newUser.getBirthdate()));
}
Also used : DateJsonSerializer(jodd.json.impl.DateJsonSerializer) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Person(jodd.json.mock.Person) Date(java.util.Date) DateJsonSerializer(jodd.json.impl.DateJsonSerializer) Test(org.junit.Test)

Example 14 with Person

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

the class ObjectToMapTest method testConvertPersonToMap.

@Test
public void testConvertPersonToMap() {
    Person jodder = new DataCreator().createJodder();
    final Map<String, Object> target = new HashMap<>();
    JsonContext jsonContext = new JsonSerializer().createJsonContext(null);
    BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {

        @Override
        protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
            target.put(propertyName, value);
        }
    };
    beanSerializer.serialize();
    assertEquals(6, target.size());
    assertSame(jodder.getBirthdate(), target.get("birthdate"));
    assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
    assertSame(jodder.getLastname(), target.get("lastname"));
    assertSame(jodder.getFirstname(), target.get("firstname"));
    assertSame(jodder.getHome(), target.get("home"));
    assertSame(jodder.getWork(), target.get("work"));
}
Also used : HashMap(java.util.HashMap) Person(jodd.json.mock.Person) Test(org.junit.Test)

Example 15 with Person

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

the class ObjectToMapTest method testConvertPersonToMap2.

@Test
public void testConvertPersonToMap2() {
    Person jodder = new DataCreator().createJodder();
    final Map<String, Object> target = new HashMap<>();
    JsonContext jsonContext = new JsonSerializer().include("phones").excludeTypes(Address.class).createJsonContext(null);
    BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {

        @Override
        protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
            target.put(propertyName, value);
        }
    };
    beanSerializer.serialize();
    assertEquals(5, target.size());
    assertSame(jodder.getBirthdate(), target.get("birthdate"));
    assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
    assertSame(jodder.getLastname(), target.get("lastname"));
    assertSame(jodder.getFirstname(), target.get("firstname"));
    assertSame(jodder.getPhones(), target.get("phones"));
}
Also used : Address(jodd.json.mock.Address) HashMap(java.util.HashMap) 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