當前位置: 首頁>>代碼示例>>Java>>正文


Java JSONSerializer類代碼示例

本文整理匯總了Java中com.alibaba.fastjson.serializer.JSONSerializer的典型用法代碼示例。如果您正苦於以下問題:Java JSONSerializer類的具體用法?Java JSONSerializer怎麽用?Java JSONSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JSONSerializer類屬於com.alibaba.fastjson.serializer包,在下文中一共展示了JSONSerializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: toJSONString

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        for (SerializerFeature feature : features) {
            serializer.config(feature, true);
        }
        serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
        setFilter(serializer, filter);
        serializer.write(object);
        String serializeWriter = out.toString();
        return serializeWriter;
    } finally {
        out.close();
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:17,代碼來源:JSON.java

示例2: test_valuefilter_3

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_valuefilter_3() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("name")) {
                return null;
            }

            return value;
        }

    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getValueFilters().add(filter);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "AA");
    serializer.write(map);

    String text = out.toString();
    Assert.assertEquals("{}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:ValueFilterTest_field_long.java

示例3: test_3

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_3() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("name".equals(name)) {
                return true;
            }
            return false;
        }
    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "chennp2008");
    serializer.write(map);

    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:PropertyFilter_float.java

示例4: test_0

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_0() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            return false;
        }
    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);

    A a = new A();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:19,代碼來源:PropertyFilter_short.java

示例5: toJSONString

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter filter,
                                        SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out, config);
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }

        setFilter(serializer, filter);

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:20,代碼來源:JSON.java

示例6: test_direct

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_direct() throws Exception {
    SerializeWriter out = new SerializeWriter(6);
    out.config(SerializerFeature.QuoteFieldNames, true);

    try {
        JSONSerializer serializer = new JSONSerializer(out);

        VO vo = new VO();
        vo.setValue(123456789);
        serializer.write(vo);
        
        Assert.assertEquals("{\"value\":123456789}", out.toString());
    } finally {
        out.close();
    }
    
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:SerializeWriterTest_17.java

示例7: test_point

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_point() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Point.class).getClass());
    
    Point point = new Point(3, 4);
    String text = JSON.toJSONString(point, SerializerFeature.WriteClassName);

    System.out.println(text);
    Object obj = JSON.parse(text);
    Point point2 = (Point) obj;

    Assert.assertEquals(point, point2);

    Point point3 = (Point) JSON.parseObject(text, Point.class);

    Assert.assertEquals(point, point3);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:PointTest2.java

示例8: test_2

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_2() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("name".equals(name)) {
                return true;
            }
            return false;
        }
    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);

    A a = new A();
    a.setName("chennp2008");
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:PropertyFilter_char.java

示例9: test_namefilter_1

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter_1() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }

            return name;
        }

    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 0);
    serializer.write(map);

    String text = out.toString();
    Assert.assertEquals("{\"ID\":0}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:NameFilterTest_double_field.java

示例10: test_namefilter_1

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter_1() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }

            return name;
        }

    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", true);
    serializer.write(map);

    String text = out.toString();
    Assert.assertEquals("{\"ID\":true}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:NameFilterTest_boolean_field.java

示例11: test_namefilter

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }

            return name;
        }

    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);

    Bean a = new Bean();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"ID\":0.0}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:24,代碼來源:NameFilterTest_double.java

示例12: test_namefilter

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_namefilter() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {

            if (name.equals("id")) {
                Assert.assertTrue(value instanceof Character);
                return "ID";
            }

            return name;
        }

    };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);

    Bean a = new Bean();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"ID\":\"0\"}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:NameFilterTest_char.java

示例13: test_writer_1

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_writer_1() throws Exception {
    SerializeWriter out = new SerializeWriter(14);
    out.config(SerializerFeature.QuoteFieldNames, true);

    try {
        JSONSerializer serializer = new JSONSerializer(out);

        VO vo = new VO();
        vo.setValue("#");
        serializer.write(vo);
        
        Assert.assertEquals("{\"value\":\"#\"}", out.toString());
    } finally {
        out.close();
    }
   
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:SerializeWriterTest_18.java

示例14: test_2

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_2() throws Exception {
    SerializeWriter out = new SerializeWriter();

    ListSerializer listSerializer = new ListSerializer();

    LinkedList<Object> list = new LinkedList<Object>();
    for (int i = 0; i < 100000; i++) {
        list.add(i);
    }

    long start = System.currentTimeMillis();
    listSerializer.write(new JSONSerializer(out), list, null, null, 0);
    long end = System.currentTimeMillis();

    System.out.println("linkedList time: " + (end - start));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:ListSerializerTest3.java

示例15: test_0

import com.alibaba.fastjson.serializer.JSONSerializer; //導入依賴的package包/類
public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer();

    serializer.setDateFormat("yyyy");
    Assert.assertEquals("yyyy", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
    Assert.assertEquals("yyyy",  serializer.getDateFormatPattern());
    
    serializer.setDateFormat("yyyy-MM");
    Assert.assertEquals("yyyy-MM", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
    
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    format.setTimeZone(JSON.defaultTimeZone);
    serializer.setDateFormat(format);
    Assert.assertEquals("yyyy-MM-dd",  serializer.getDateFormatPattern());
    
    serializer.close();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:JSONSerializerTest3.java


注:本文中的com.alibaba.fastjson.serializer.JSONSerializer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。