Search in sources :

Example 1 with DateJsonSerializer

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()));
}
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 2 with DateJsonSerializer

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();
}
Also used : LongArrayJsonSerializer(jodd.json.impl.LongArrayJsonSerializer) ObjectJsonSerializer(jodd.json.impl.ObjectJsonSerializer) BooleanJsonSerializer(jodd.json.impl.BooleanJsonSerializer) EnumJsonSerializer(jodd.json.impl.EnumJsonSerializer) FileJsonSerializer(jodd.json.impl.FileJsonSerializer) IterableJsonSerializer(jodd.json.impl.IterableJsonSerializer) ClassJsonSerializer(jodd.json.impl.ClassJsonSerializer) FloatArrayJsonSerializer(jodd.json.impl.FloatArrayJsonSerializer) JDateTimeSerializer(jodd.json.impl.JDateTimeSerializer) NumberJsonSerializer(jodd.json.impl.NumberJsonSerializer) DoubleArrayJsonSerializer(jodd.json.impl.DoubleArrayJsonSerializer) ArraysJsonSerializer(jodd.json.impl.ArraysJsonSerializer) BigInteger(java.math.BigInteger) CalendarJsonSerializer(jodd.json.impl.CalendarJsonSerializer) BooleanArrayJsonSerializer(jodd.json.impl.BooleanArrayJsonSerializer) CharSequenceJsonSerializer(jodd.json.impl.CharSequenceJsonSerializer) CharacterJsonSerializer(jodd.json.impl.CharacterJsonSerializer) MapJsonSerializer(jodd.json.impl.MapJsonSerializer) ByteArrayJsonSerializer(jodd.json.impl.ByteArrayJsonSerializer) IntArrayJsonSerializer(jodd.json.impl.IntArrayJsonSerializer) DateJsonSerializer(jodd.json.impl.DateJsonSerializer)

Aggregations

DateJsonSerializer (jodd.json.impl.DateJsonSerializer)2 BigInteger (java.math.BigInteger)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ArraysJsonSerializer (jodd.json.impl.ArraysJsonSerializer)1 BooleanArrayJsonSerializer (jodd.json.impl.BooleanArrayJsonSerializer)1 BooleanJsonSerializer (jodd.json.impl.BooleanJsonSerializer)1 ByteArrayJsonSerializer (jodd.json.impl.ByteArrayJsonSerializer)1 CalendarJsonSerializer (jodd.json.impl.CalendarJsonSerializer)1 CharSequenceJsonSerializer (jodd.json.impl.CharSequenceJsonSerializer)1 CharacterJsonSerializer (jodd.json.impl.CharacterJsonSerializer)1 ClassJsonSerializer (jodd.json.impl.ClassJsonSerializer)1 DoubleArrayJsonSerializer (jodd.json.impl.DoubleArrayJsonSerializer)1 EnumJsonSerializer (jodd.json.impl.EnumJsonSerializer)1 FileJsonSerializer (jodd.json.impl.FileJsonSerializer)1 FloatArrayJsonSerializer (jodd.json.impl.FloatArrayJsonSerializer)1 IntArrayJsonSerializer (jodd.json.impl.IntArrayJsonSerializer)1 IterableJsonSerializer (jodd.json.impl.IterableJsonSerializer)1 JDateTimeSerializer (jodd.json.impl.JDateTimeSerializer)1