Search in sources :

Example 6 with MapSerializer

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

the class MapSerializerTest method test_int2_s.

public void test_int2_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    MapSerializer mapSerializer = new MapSerializer();
    Map<String, Integer> map = new LinkedHashMap<String, Integer>();
    map.put("A", 1);
    map.put("B", 2);
    mapSerializer.write(new JSONSerializer(out), map, null, null, 0);
    Assert.assertEquals("{\"A\":1,\"B\":2}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) LinkedHashMap(java.util.LinkedHashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 7 with MapSerializer

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

the class MapSerializerTest method test_4.

public void test_4() throws Exception {
    SerializeWriter out = new SerializeWriter();
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("TOP", "value");
    map.put("bytes", new byte[] { 1, 2 });
    MapSerializer mapSerializer = new MapSerializer();
    mapSerializer.write(new JSONSerializer(out), map, null, null, 0);
    String text = out.toString();
    Assert.assertEquals("{\"TOP\":\"value\",\"bytes\":\"AQI=\"}", text);
    JSONObject json = JSON.parseObject(text);
    byte[] bytes = json.getBytes("bytes");
    Assert.assertEquals(1, bytes[0]);
    Assert.assertEquals(2, bytes[1]);
    Assert.assertEquals(2, bytes.length);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) LinkedHashMap(java.util.LinkedHashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 8 with MapSerializer

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

the class MapSerializerTest method test_special_s.

public void test_special_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    MapSerializer mapSerializer = new MapSerializer();
    mapSerializer.write(new JSONSerializer(out), Collections.singletonMap("A\nB", 1), null, null, 0);
    Assert.assertEquals("{\"A\\nB\":1}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 9 with MapSerializer

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

the class MapSerializerTest method test_string3_s.

public void test_string3_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.config(SerializerFeature.UseSingleQuotes, true);
    MapSerializer mapSerializer = new MapSerializer();
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("A", "1");
    map.put("B", "2");
    mapSerializer.write(serializer, map, null, null, 0);
    Assert.assertEquals("{'A':'1','B':'2'}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with MapSerializer

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

the class MapSerializerTest method test_long2_s.

public void test_long2_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    MapSerializer mapSerializer = new MapSerializer();
    Map<String, Long> map = new LinkedHashMap<String, Long>();
    map.put("A", 1L);
    map.put("B", 2L);
    mapSerializer.write(new JSONSerializer(out), map, null, null, 0);
    Assert.assertEquals("{\"A\":1,\"B\":2}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) LinkedHashMap(java.util.LinkedHashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)10 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)10 LinkedHashMap (java.util.LinkedHashMap)5 JSONObject (com.alibaba.fastjson.JSONObject)1