Search in sources :

Example 1 with ObjectJsonSerializer

use of jodd.json.impl.ObjectJsonSerializer in project jodd by oblac.

the class JSONSerializationTest method testSerializeWithCustomBeanSerializer.

@Test
public void testSerializeWithCustomBeanSerializer() {
    JsonSerializer jsonSerializer = new JsonSerializer();
    Lucy lucy = new Lucy();
    String json = jsonSerializer.serialize(lucy);
    assertAttribute("address", json);
    assertAttribute("name", json);
    jsonSerializer.withSerializer(Object.class, new ObjectJsonSerializer() {

        public void serializeValue(final JsonContext jsonContext, Object value) {
            jsonContext.writeOpenObject();
            BeanSerializer beanVisitor = new BeanSerializer(jsonContext, value) {

                @Override
                protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
                    if (value == null) {
                        return;
                    }
                    super.onSerializableProperty(propertyName, propertyType, value);
                }
            };
            beanVisitor.serialize();
            jsonContext.writeCloseObject();
        }
    });
    json = jsonSerializer.serialize(lucy);
    assertEquals("{\"name\":\"Lucy\"}", json);
}
Also used : ObjectJsonSerializer(jodd.json.impl.ObjectJsonSerializer) ObjectJsonSerializer(jodd.json.impl.ObjectJsonSerializer) Test(org.junit.Test)

Example 2 with ObjectJsonSerializer

use of jodd.json.impl.ObjectJsonSerializer 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

ObjectJsonSerializer (jodd.json.impl.ObjectJsonSerializer)2 BigInteger (java.math.BigInteger)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 DateJsonSerializer (jodd.json.impl.DateJsonSerializer)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 LongArrayJsonSerializer (jodd.json.impl.LongArrayJsonSerializer)1 MapJsonSerializer (jodd.json.impl.MapJsonSerializer)1