Search in sources :

Example 21 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class JSONSerializerTest1 method test_0.

public void test_0() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    Assert.assertEquals(0, serializer.getNameFilters().size());
    Assert.assertEquals(0, serializer.getNameFilters().size());
    Assert.assertEquals(0, serializer.getValueFilters().size());
    Assert.assertEquals(0, serializer.getValueFilters().size());
    Assert.assertEquals(0, serializer.getPropertyFilters().size());
    Assert.assertEquals(0, serializer.getPropertyFilters().size());
    serializer.writeWithFormat("123", null);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 22 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class JSONSerializerTest2 method test_3.

public void test_3() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.config(SerializerFeature.WriteEnumUsingToString, true);
    serializer.write(new A(Type.B));
    Assert.assertEquals("{\"type\":\"B\"}", serializer.getWriter().toString());
    A a = JSON.parseObject(serializer.getWriter().toString(), A.class);
    Assert.assertEquals(a.getType(), Type.B);
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 23 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class JSONSerializerTest2 method test_0.

public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    int size = JSONSerializerMapTest.size(serializer.getMapping());
    serializer.config(SerializerFeature.WriteEnumUsingToString, false);
    serializer.config(SerializerFeature.WriteEnumUsingName, false);
    serializer.write(Type.A);
    Assert.assertTrue(size < JSONSerializerMapTest.size(serializer.getMapping()));
    Assert.assertEquals(Integer.toString(Type.A.ordinal()), serializer.getWriter().toString());
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 24 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class JavaBeanSerializerTest method test_0_s.

public void test_0_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    A a = new A();
    a.getL0().add("A");
    a.getL0().add("B");
    JavaBeanSerializer serializer = new JavaBeanSerializer(A.class);
    serializer.write(new JSONSerializer(out), a, null, null, 0);
    Assert.assertEquals("{\"l0\":[\"A\",\"B\"]}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 25 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class JavaBeanSerializerTest method test_error_1_s.

public void test_error_1_s() throws Exception {
    JSONException error = null;
    try {
        SerializeWriter out = new SerializeWriter();
        JavaBeanSerializer serializer = new JavaBeanSerializer(D.class);
        serializer.write(new JSONSerializer(out), new D(), null, null, 0);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONException(com.alibaba.fastjson.JSONException) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)262 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)205 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)48 NameFilter (com.alibaba.fastjson.serializer.NameFilter)36 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)19 ArrayList (java.util.ArrayList)12 ListSerializer (com.alibaba.fastjson.serializer.ListSerializer)11 StringWriter (java.io.StringWriter)11 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)10 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)10 Map (java.util.Map)10 Date (java.util.Date)9 LinkedHashMap (java.util.LinkedHashMap)8 JSONException (com.alibaba.fastjson.JSONException)7 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 IOException (java.io.IOException)4 Color (java.awt.Color)3