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);
}
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();
}
Aggregations