Java 类com.alibaba.fastjson.serializer.SerializeConfig 实例源码

项目:GitHub    文件:PropertyPreFilterClassLevelTest_private.java   
public void test_0() throws Exception {
    Object[] array = {new ModelA(), new ModelB() };

    SerializeConfig config = new SerializeConfig();
    config.addFilter(ModelA.class, // 
                     new SimplePropertyPreFilter("name"));
    config.addFilter(ModelB.class, // 
                     new SimplePropertyPreFilter("id"));
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{},{\"id\":1002}]", text2);

    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);


}
项目:uavstack    文件:JSON.java   
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters,
                                        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, filters);

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}
项目:GitHub    文件:PropertyPreFilterClassLevelTest.java   
public void test_0() throws Exception {
    Object[] array = {new ModelA(), new ModelB() };

    SerializeConfig config = new SerializeConfig();
    config.addFilter(ModelA.class, // 
                     new SimplePropertyPreFilter("name"));
    config.addFilter(ModelB.class, // 
                     new SimplePropertyPreFilter("id"));
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{},{\"id\":1002}]", text2);

    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);


}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_bean_2() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    PO vo = TypeUtils.castToJavaBean(map, PO.class);
    Assert.assertEquals(123, vo.id);

    SerializeWriter out = new SerializeWriter();

    try {
        SerializeConfig config = new SerializeConfig();
        JSONSerializer serializer = new JSONSerializer(out, config);
        config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", "ID")));

        serializer.write(vo);

        Assert.assertEquals("{\"ID\":123}", out.toString());
    } finally {
        out.close();
    }


}
项目:GitHub    文件:ListFieldTest.java   
public void test_codec_null() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"value\":null}", text);

    ParserConfig config = new ParserConfig();
    config.setAutoTypeSupport(true);
    config.setAsmEnable(false);

    V0 v1 = JSON.parseObject(text, V0.class, config, JSON.DEFAULT_PARSER_FEATURE);

    Assert.assertEquals(v1.getValue(), v.getValue());
}
项目:GitHub    文件:NamingSerTest.java   
public void test_kebab() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.KebabCase;

    Model model = new Model();
    model.personId = 1001;
    String text = JSON.toJSONString(model, config);
    Assert.assertEquals("{\"person-id\":1001}", text);

    ParserConfig parserConfig = new ParserConfig();
    parserConfig.propertyNamingStrategy = PropertyNamingStrategy.KebabCase;
    Model model2 = JSON.parseObject(text, Model.class, parserConfig);
    Assert.assertEquals(model.personId, model2.personId);

    Model model3 = JSON.parseObject(text, Model.class);
    Assert.assertEquals(model.personId, model3.personId);
}
项目:GitHub    文件:NamingSerTest.java   
public void test_pascal() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;

    Model model = new Model();
    model.personId = 1001;
    String text = JSON.toJSONString(model, config);
    Assert.assertEquals("{\"PersonId\":1001}", text);

    ParserConfig parserConfig = new ParserConfig();
    parserConfig.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;
    Model model2 = JSON.parseObject(text, Model.class, parserConfig);
    Assert.assertEquals(model.personId, model2.personId);

    Model model3 = JSON.parseObject(text, Model.class);
    Assert.assertEquals(model.personId, model3.personId);
}
项目:GitHub    文件:LongFieldTest_primitive.java   
public void test_codec_null_asm() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);

    String text = JSON.toJSONString(v, mapping,
            SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"value\":123}", text);

    ParserConfig config = new ParserConfig();
    config.setAsmEnable(false);

    V0 v1 = JSON.parseObject(text, V0.class, config,
            JSON.DEFAULT_PARSER_FEATURE);

    Assert.assertEquals(v1.getValue(), v.getValue());
}
项目:uavstack    文件:JSON.java   
public static final byte[] toJSONBytes(Object object, SerializeConfig config, 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);
        }

        serializer.write(object);

        return out.toBytes("UTF-8");
    } finally {
        out.close();
    }
}
项目:GitHub    文件:FileFieldTest.java   
public void test_codec_null_1() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    Assert.assertEquals("{\"value\":null}", JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals("{value:null}", JSON.toJSONStringZ(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals("{value:null}", JSON.toJSONStringZ(v, mapping, SerializerFeature.UseSingleQuotes, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals("{'value':null}", JSON.toJSONStringZ(v, mapping, SerializerFeature.UseSingleQuotes, SerializerFeature.QuoteFieldNames, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
}
项目:GitHub    文件:Retrofit2ConverterFactory.java   
public RequestBody convert(T value) throws IOException {
    byte[] content = JSON.toJSONBytes(value
                , serializeConfig == null
                    ? SerializeConfig.globalInstance
                    : serializeConfig
                , serializerFeatures == null
                    ? SerializerFeature.EMPTY
                    : serializerFeatures
    );

    return RequestBody.create(MEDIA_TYPE, content);
}
项目:GitHub    文件:FastJsonConfig.java   
/**
 * init param.
 */
public FastJsonConfig() {

    this.charset = Charset.forName("UTF-8");

    this.serializeConfig = SerializeConfig.getGlobalInstance();
    this.parserConfig = new ParserConfig();

    this.serializerFeatures = new SerializerFeature[] {
            SerializerFeature.BrowserSecure
    };

    this.serializeFilters = new SerializeFilter[0];
    this.features = new Feature[0];
}
项目:GitHub    文件:JSONPath.java   
public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig){
    if (path == null || path.length() == 0) {
        throw new JSONPathException("json-path can not be null or empty");
    }

    this.path = path;
    this.serializeConfig = serializeConfig;
    this.parserConfig = parserConfig;
}
项目:GitHub    文件:ByteArrayTest.java   
public void test_bytes_1() throws Exception {
    VO vo = new VO();
    vo.setValue(new byte[] {1, 2, 3});

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
    String text1 = JSON.toJSONString(vo, mapping, features);

    Assert.assertEquals("{\"value\":\"AQID\"}", text1);
    String text2 = JSON.toJSONString(vo, features);

    Assert.assertEquals("{\"value\":\"AQID\"}", text2);
}
项目:GitHub    文件:JSONTest_overflow.java   
public void test_overflow_1() throws Exception {
    Entity entity = new Entity();
    entity.setSelf(entity);

    String text = JSON.toJSONStringZ(entity, SerializeConfig.getGlobalInstance());
    Entity entity2 = JSON.parseObject(text, Entity.class);
    Assert.assertTrue(entity2 == entity2.getSelf());
}
项目:GitHub    文件:InetAddressFieldTest.java   
public void test_codec_null() throws Exception {
    User user = new User();
    user.setValue(null);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(user, mapping, SerializerFeature.WriteMapNullValue);

    User user1 = JSON.parseObject(text, User.class);

    Assert.assertEquals(user1.getValue(), user.getValue());
}
项目:GitHub    文件:CharsetFieldTest.java   
public void test_codec() throws Exception {
    User user = new User();
    user.setValue(Charset.forName("UTF-8"));

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(user, mapping, SerializerFeature.WriteMapNullValue);

    User user1 = JSON.parseObject(text, User.class);

    Assert.assertEquals(user1.getValue(), user.getValue());
}
项目:GitHub    文件:Issue763.java   
public void test_0() throws Exception {
    Map<String, Object> reqDto = new HashMap<String, Object>();
    reqDto.put("name", "aaaa");
    reqDto.put("age", 50);
    reqDto.put("address", "深圳南山");

    SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
    filter.getExcludes().add("name");

    // SerializeConfig.getGlobalInstance().addFilter(Map.class, filter);
    SerializeConfig config = new SerializeConfig();
    config.addFilter(HashMap.class, filter);
    System.out.println(JSON.toJSONString(reqDto, config));
}
项目:GitHub    文件:FeaturesTest7.java   
public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":\"SECONDS\"}", text);
}
项目:GitHub    文件:FeaturesTest.java   
public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":null}", text);
}
项目:GitHub    文件:ObjectSerializerTest.java   
public void test_serialize() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(ResultCode.class, new ResultCodeSerilaizer());

    Result result = new Result();
    result.code = ResultCode.SIGN_ERROR;
    String json = JSON.toJSONString(result, config);
    Assert.assertEquals("{\"code\":17}", json);
}
项目:GitHub    文件:FeaturesTest5_1.java   
public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(true);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":false}", text);
}
项目:GitHub    文件:FeaturesTest6.java   
public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":[]}", text);
}
项目:GitHub    文件:StringFieldTest.java   
public void test_codec_null_1() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
    Assert.assertEquals("{\"value\":\"\"}", text);
}
项目:GitHub    文件:FeaturesTest3.java   
public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(true);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":0}", text);
}
项目:GitHub    文件:FeaturesTest4.java   
public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":\"\"}", text);
}
项目:GitHub    文件:SerializeConfigTest2.java   
public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setTypeKey("%type");
    Assert.assertEquals("%type", config.getTypeKey());

    Model model = new Model();
    model.value = 1001;

    Assert.assertEquals("{\"%type\":\"com.alibaba.json.bvt.serializer.SerializeConfigTest2$Model\",\"value\":1001}",
                        JSON.toJSONString(model, config, SerializerFeature.WriteClassName));
}
项目:GitHub    文件:URIFieldTest.java   
public void test_codec() throws Exception {
    User user = new User();
    user.setValue(URI.create("http://www.alibaba.com/abc"));

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(user, mapping, SerializerFeature.WriteMapNullValue);

    User user1 = JSON.parseObject(text, User.class);

    Assert.assertEquals(user1.getValue(), user.getValue());
}
项目:GitHub    文件:IntegerArrayFieldTest.java   
public void test_codec_null() throws Exception {
    User user = new User();
    user.setValue(null);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(user, mapping, SerializerFeature.WriteMapNullValue);

    User user1 = JSON.parseObject(text, User.class);

    Assert.assertEquals(user1.getValue(), user.getValue());
}
项目:GitHub    文件:WriteClassNameTest.java   
public void test_writeClassName() throws Exception {
    Entity object = new Entity();
    object.setId(123);
    object.setName("jobs");
    object.setAverage(3.21F);

    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);
    String text = JSON.toJSONString(object, config, SerializerFeature.WriteClassName);
    System.out.println(text);
}
项目:GitHub    文件:DateFieldTest2.java   
public void test_codec_no_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
项目:GitHub    文件:DoubleFieldTest_A.java   
public void test_codec_null() throws Exception {
    User user = new User();
    user.setValue(null);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(user, mapping, SerializerFeature.WriteMapNullValue);
    System.out.println(text);

    User user1 = JSON.parseObject(text, User.class);

    Assert.assertEquals(user1.getValue(), user.getValue());
}
项目:GitHub    文件:Issue204.java   
public void test_for_issue() throws Exception {
    VO vo = new VO();

    SerializeFilter filter = null;
    JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), filter);
    JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), new SerializeFilter[0]);
}
项目:GitHub    文件:Bug_for_issue_184.java   
public void test_for_issue() throws Exception {
    TUser user = new TUser();
    user.id = 1001;
    // 禁用asm(在android下使用),启用asm则没问题。
    SerializeConfig.getGlobalInstance().setAsmEnable(false);
    String json = JSON.toJSONString(user, SerializerFeature.WriteClassName);
    // 输出{"@type":"xx.TUser","id":0L}
    System.out.println(json);
    // 下面反系列化错误:com.alibaba.fastjson.JSONException: unclosed.str
    // 原因:id带L后缀
    user = (TUser) JSON.parse(json);
}
项目:GitHub    文件:NumberFieldTest.java   
public void test_codec_2_asm() throws Exception {
    V0 v = new V0();
    v.setValue(Long.MAX_VALUE);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"value\":" + Long.MAX_VALUE + "}", text);

    V0 v1 = JSON.parseObject(text, V0.class);

    Assert.assertEquals(new Long(Long.MAX_VALUE), v1.getValue());
}
项目:GitHub    文件:DateFieldTest3.java   
public void test_codec_no_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());

    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
项目:GitHub    文件:NumberFieldTest.java   
public void test_codec_3_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new BigDecimal("3.2"));

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"value\":3.2}", text);

    V0 v1 = JSON.parseObject(text, V0.class);

    Assert.assertEquals(new BigDecimal("3.2"), v1.getValue());
}
项目:GitHub    文件:DateFieldTest2.java   
public void test_codec_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
项目:GitHub    文件:BooleanArrayFieldTest_primitive.java   
public void test_codec_null_1() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    Assert.assertEquals("{\"value\":[]}", text);
}
项目:GitHub    文件:StringBuilderFieldTest.java   
public void test_codec_null_1() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue,
                                    SerializerFeature.WriteNullStringAsEmpty);
    Assert.assertEquals("{\"value\":\"\"}", text);
}