Search in sources :

Example 31 with ParserConfig

use of com.alibaba.fastjson.parser.ParserConfig in project fastjson by alibaba.

the class Issue96 method xx_testCast.

public void xx_testCast() {
    Page<Sub> page = new Page<Sub>(new Sub(1));
    Type type = new TypeReference<Page<Sub>>() {
    }.getType();
    ParserConfig parserconfig = ParserConfig.getGlobalInstance();
    // !!!! this will fail:
    // !!!! com.alibaba.fastjson.JSONException: can not cast to : Page<Sub> TypeUtils.java:719
    Page<Sub> page1 = TypeUtils.cast(page, type, parserconfig);
    System.out.println(page1.sub.getClass());
}
Also used : Type(java.lang.reflect.Type) ParserConfig(com.alibaba.fastjson.parser.ParserConfig)

Example 32 with ParserConfig

use of com.alibaba.fastjson.parser.ParserConfig in project fastjson by alibaba.

the class NamingSerTest method test_camel.

public void test_camel() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
    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.CamelCase;
    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);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ParserConfig(com.alibaba.fastjson.parser.ParserConfig)

Example 33 with ParserConfig

use of com.alibaba.fastjson.parser.ParserConfig in project fastjson by alibaba.

the class NamingSerTest method test_snake.

public void test_snake() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
    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.SnakeCase;
    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);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ParserConfig(com.alibaba.fastjson.parser.ParserConfig)

Example 34 with ParserConfig

use of com.alibaba.fastjson.parser.ParserConfig in project fastjson by alibaba.

the class NamingSerTest method test_kebab.

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);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ParserConfig(com.alibaba.fastjson.parser.ParserConfig)

Example 35 with ParserConfig

use of com.alibaba.fastjson.parser.ParserConfig in project fastjson by alibaba.

the class NamingSerTest method test_pascal.

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);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ParserConfig(com.alibaba.fastjson.parser.ParserConfig)

Aggregations

ParserConfig (com.alibaba.fastjson.parser.ParserConfig)50 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)13 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)10 JSONException (com.alibaba.fastjson.JSONException)7 Properties (java.util.Properties)5 JSONField (com.alibaba.fastjson.annotation.JSONField)2 Feature (com.alibaba.fastjson.parser.Feature)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 ArrayList (java.util.ArrayList)2 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ResolveTask (com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)1 JSONLexerBase (com.alibaba.fastjson.parser.JSONLexerBase)1 ParseContext (com.alibaba.fastjson.parser.ParseContext)1 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)1 FieldInfo (com.alibaba.fastjson.util.FieldInfo)1 Type (java.lang.reflect.Type)1 LinkedHashSet (java.util.LinkedHashSet)1