当前位置: 首页>>代码示例>>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;未经允许,请勿转载。