Search in sources :

Example 6 with JSONSerializer

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

the class FastjsonSockJsMessageCodec method applyJsonQuoting.

@Override
protected char[] applyJsonQuoting(String content) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.write(content);
        return out.toCharArrayForSpringWebSocket();
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 7 with JSONSerializer

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

the class JSON_toJSONStringTest method test_1.

public void test_1() throws Exception {
    User user = new User();
    user.setId(123);
    user.setName("毛头");
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(User.class, new JavaBeanSerializer(User.class, Collections.singletonMap("id", "uid")));
    JSONSerializer serializer = new JSONSerializer(mapping);
    serializer.write(user);
    String jsonString = serializer.toString();
    Assert.assertEquals("{\"uid\":123}", jsonString);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 8 with JSONSerializer

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

the class RectangleTest method test_color.

public void test_color() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Rectangle.class).getClass());
    Rectangle v = new Rectangle(3, 4, 100, 200);
    String text = JSON.toJSONString(v, SerializerFeature.WriteClassName);
    System.out.println(text);
    Rectangle v2 = (Rectangle) JSON.parse(text);
    Assert.assertEquals(v, v2);
}
Also used : Rectangle(java.awt.Rectangle) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 9 with JSONSerializer

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

the class ColorTest2 method test_color.

public void test_color() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Color.class).getClass());
    Color color = Color.RED;
    String text = JSON.toJSONString(color, SerializerFeature.WriteClassName);
    System.out.println(text);
    Color color2 = (Color) JSON.parse(text);
    Assert.assertEquals(color, color2);
}
Also used : Color(java.awt.Color) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 10 with JSONSerializer

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

the class Bug_for_JSONObject method test_0.

public void test_0() throws Exception {
    JSONSerializer ser = new JSONSerializer();
    ser.config(SerializerFeature.WriteClassName, true);
    ser.write(new JSONObject());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) 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