use of jodd.json.impl.DateJsonSerializer 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()));
}
use of jodd.json.impl.DateJsonSerializer in project jodd by oblac.
the class TypeJsonSerializerMap method registerDefaults.
/**
* Registers default set of {@link jodd.json.TypeJsonSerializer serializers}.
*/
public void registerDefaults() {
// main
map.put(Object.class, new ObjectJsonSerializer());
map.put(Map.class, new MapJsonSerializer());
map.put(Iterable.class, new IterableJsonSerializer());
// arrays
map.put(int[].class, new IntArrayJsonSerializer());
map.put(long[].class, new LongArrayJsonSerializer());
map.put(double[].class, new DoubleArrayJsonSerializer());
map.put(float[].class, new FloatArrayJsonSerializer());
map.put(boolean[].class, new BooleanArrayJsonSerializer());
map.put(byte[].class, new ByteArrayJsonSerializer());
map.put(Integer[].class, new ArraysJsonSerializer<Integer>() {
@Override
protected int getLength(Integer[] array) {
return array.length;
}
@Override
protected Integer get(Integer[] array, int index) {
return array[index];
}
});
map.put(Long[].class, new ArraysJsonSerializer<Long>() {
@Override
protected int getLength(Long[] array) {
return array.length;
}
@Override
protected Long get(Long[] array, int index) {
return array[index];
}
});
map.put(Arrays.class, new ArraysJsonSerializer());
// strings
TypeJsonSerializer jsonSerializer = new CharSequenceJsonSerializer();
map.put(String.class, jsonSerializer);
map.put(StringBuilder.class, jsonSerializer);
map.put(CharSequence.class, jsonSerializer);
// number
jsonSerializer = new NumberJsonSerializer();
map.put(Number.class, jsonSerializer);
map.put(Integer.class, jsonSerializer);
map.put(int.class, jsonSerializer);
map.put(Long.class, jsonSerializer);
map.put(long.class, jsonSerializer);
map.put(Double.class, jsonSerializer);
map.put(double.class, jsonSerializer);
map.put(Float.class, jsonSerializer);
map.put(float.class, jsonSerializer);
map.put(BigInteger.class, jsonSerializer);
map.put(BigDecimal.class, jsonSerializer);
// other
map.put(Boolean.class, new BooleanJsonSerializer());
map.put(Date.class, new DateJsonSerializer());
map.put(Calendar.class, new CalendarJsonSerializer());
map.put(JDateTime.class, new JDateTimeSerializer());
map.put(Enum.class, new EnumJsonSerializer());
map.put(File.class, new FileJsonSerializer(FileJsonSerializer.Type.PATH));
//map.put(Collection.class, new CollectionJsonSerializer());
jsonSerializer = new CharacterJsonSerializer();
map.put(Character.class, jsonSerializer);
map.put(char.class, jsonSerializer);
map.put(Class.class, new ClassJsonSerializer());
// clear cache
cache.clear();
}
Aggregations