Package jodd.json.mock

Examples of jodd.json.mock.Person


    assertEquals(pedro.getFirstname(), list.get(2).getFirstname());
  }

  @Test
  public void testListDeserializationNoClass2() {
    Person modesty = creator.createModesty();
    Person igor = creator.createJodder();
    Person pedro = creator.createPedro();
    List<Person> list = new ArrayList<Person>(3);
    list.add(modesty);
    list.add(igor);
    list.add(pedro);

    String json = new JsonSerializer().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());
  }
View Full Code Here


  }

  @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().use("birthdate", new DateJsonSerializer() {
      @Override
      public void serialize(JsonContext jsonContext, Date date) {
        jsonContext.writeString(df.format(date));
      }
    }).serialize(foo);

    Person newUser = new JsonParser()
        .use("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()));
  }
View Full Code Here

  }


  @Test
  public void testArrayType() {
    Person igor = creator.createJodder();
    Person modesty = creator.createModesty();

    Group group = new Group("brothers", igor, modesty);
    String json = new JsonSerializer().include("people").exclude("*.class").serialize(group);
    Group bro = new JsonParser().map(Group.class).parse(json);

View Full Code Here

    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());
  }
View Full Code Here

    assertEquals("173", target.get("height"));
  }

  @Test
  public void testConvertPersonToMap() {
    Person jodder = new DataCreator().createJodder();

    final Map<String, Object> target = new HashMap<String, Object>();

    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"));
  }
View Full Code Here

    assertSame(jodder.getWork(), target.get("work"));
  }

  @Test
  public void testConvertPersonToMap2() {
    Person jodder = new DataCreator().createJodder();

    final Map<String, Object> target = new HashMap<String, Object>();

    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"));
  }
View Full Code Here

TOP

Related Classes of jodd.json.mock.Person

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.