本文整理汇总了Java中com.alibaba.fastjson.parser.Feature类的典型用法代码示例。如果您正苦于以下问题:Java Feature类的具体用法?Java Feature怎么用?Java Feature使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Feature类属于com.alibaba.fastjson.parser包,在下文中一共展示了Feature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_array_mapping
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_array_mapping() throws Exception {
String json = "[-0.012671709,0.6042485,0.13231707,0.80090785,0.6192943]";
String json2 = "[\"-0.012671709\",\"0.6042485\",\"0.13231707\",\"0.80090785\",\"0.6192943\"]";
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportArrayToBean);
Model m2 = JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean);
assertNotNull(m1);
assertNotNull(m2);
assertEquals(-0.012671709f, m1.v1);
assertEquals(0.6042485f, m1.v2);
assertEquals(0.13231707f, m1.v3);
assertEquals(0.80090785f, m1.v4);
assertEquals(0.6192943f, m1.v5);
assertEquals(-0.012671709f, m2.v1);
assertEquals(0.6042485f, m2.v2);
assertEquals(0.13231707f, m2.v3);
assertEquals(0.80090785f, m2.v4);
assertEquals(0.6192943f, m2.v5);
}
示例2: test_1
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_1() throws Exception {
JSONObject res = new JSONObject();
res.put("a", 1);
res.put("b", 2);
res.put("c", 3);
String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}",
"{,,'a':1,,,,'b':2,'c':3,,,,,}", };
for (String t : tests) {
DefaultJSONParser ext = new DefaultJSONParser(t);
ext.config(Feature.AllowArbitraryCommas, true);
JSONObject extRes = ext.parseObject();
Assert.assertEquals(res, extRes);
DefaultJSONParser basic = new DefaultJSONParser(t);
basic.config(Feature.AllowArbitraryCommas, true);
JSONObject basicRes = basic.parseObject();
Assert.assertEquals(res, basicRes);
}
}
示例3: parseExtra
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
void parseExtra(DefaultJSONParser parser, Object object, String key) {
final JSONLexer lexer = parser.getLexer(); // xxx
if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
}
lexer.nextTokenWithColon();
Type type = FilterUtils.getExtratype(parser, object, key);
Object value;
if (type == null) {
value = parser.parse(); // skip
} else {
value = parser.parseObject(type);
}
FilterUtils.processExtra(parser, object, key, value);
}
示例4: test_0
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_0() throws Exception {
List<?> res = Arrays.asList(1, 2, 3);
String[] tests = { "[1,2,3]", "[1,,2,3]", "[1,2,,,3]", "[1 2,,,3]", "[1 2 3]", "[1, 2, 3,,]", "[,,1, 2, 3,,]", };
for (String t : tests) {
DefaultJSONParser ext = new DefaultJSONParser(t);
ext.config(Feature.AllowArbitraryCommas, true);
List<Object> extRes = ext.parseArray(Object.class);
Assert.assertEquals(res, extRes);
DefaultJSONParser basic = new DefaultJSONParser(t);
basic.config(Feature.AllowArbitraryCommas, true);
List<Object> basicRes = new ArrayList<Object>();
basic.parseArray(basicRes);
Assert.assertEquals(res, basicRes);
}
}
示例5: test_2
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_2() throws Exception {
String json = "[-1883391953414482124,-3019416596934963650,6497525620823745793,2136224289077142499,-2090575024006307745]";
String json2 = "[\"-1883391953414482124\",\"-3019416596934963650\",\"6497525620823745793\",\"2136224289077142499\",\"-2090575024006307745\"]";
Model m1 = new JSONReader(new StringReader(json), Feature.SupportArrayToBean).readObject(Model.class);
Model m2 = new JSONReader(new StringReader(json2), Feature.SupportArrayToBean).readObject(Model.class);
assertNotNull(m1);
assertNotNull(m2);
assertEquals(-1883391953414482124L, m1.v1.longValue());
assertEquals(-3019416596934963650L, m1.v2.longValue());
assertEquals(6497525620823745793L, m1.v3.longValue());
assertEquals(2136224289077142499L, m1.v4.longValue());
assertEquals(-2090575024006307745L, m1.v5.longValue());
assertEquals(-1883391953414482124L, m2.v1.longValue());
assertEquals(-3019416596934963650L, m2.v2.longValue());
assertEquals(6497525620823745793L, m2.v3.longValue());
assertEquals(2136224289077142499L, m2.v4.longValue());
assertEquals(-2090575024006307745L, m2.v5.longValue());
}
示例6: test_array_mapping
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_array_mapping() throws Exception {
String json = "[-0.012671709,0.22676692048907365,0.13231707,0.80090785,0.6192943]";
String json2 = "[\"-0.012671709\",\"0.22676692048907365\",\"0.13231707\",\"0.80090785\",\"0.6192943\"]";
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportArrayToBean);
Model m2 = JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean);
assertNotNull(m1);
assertNotNull(m2);
assertEquals(-0.012671709D, m1.v1);
assertEquals(0.22676692048907365D, m1.v2);
assertEquals(0.13231707D, m1.v3);
assertEquals(0.80090785D, m1.v4);
assertEquals(0.6192943D, m1.v5);
assertEquals(-0.012671709D, m2.v1);
assertEquals(0.22676692048907365D, m2.v2);
assertEquals(0.13231707D, m2.v3);
assertEquals(0.80090785D, m2.v4);
assertEquals(0.6192943D, m2.v5);
}
示例7: test_list
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_list() throws Exception {
Model model = new Model();
LinkedHashSet tables = new LinkedHashSet();
tables.add(new ExtTable(1001));
tables.add(new Table());
model.setTables(tables);
String json = JSON.toJSONString(model);
assertEquals("{\"tables\":[{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteClassNameTest_Set5$ExtTable\",\"id\":1001},{}]}", json);
Model model2 = JSON.parseObject(json, Model.class);
assertEquals(ExtTable.class, model2.getTables().iterator().next().getClass());
JSONObject jsonObject = JSON.parseObject(json, Feature.IgnoreAutoType);
assertEquals("{\"tables\":[{\"id\":1001},{}]}", jsonObject.toJSONString());
}
示例8: parseObject
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public static final <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
if (input == null) {
return null;
}
for (Feature featrue : features) {
featureValues = Feature.config(featureValues, featrue, true);
}
DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
if (processor instanceof ExtraTypeProvider) {
parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
}
if (processor instanceof ExtraProcessor) {
parser.getExtraProcessors().add((ExtraProcessor) processor);
}
T value = parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return value;
}
示例9: test_0
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_0 () throws Exception {
Image image = new Image();
image.setHeight(123);
image.setSize(Size.LARGE);
image.setTitle("xx");
String text = JSON.toJSONString(image, SerializerFeature.BeanToArray);
System.out.println(text);
Image image2 = JSON.parseObject(text, Image.class, Feature.SupportArrayToBean);
Assert.assertEquals(image.getHeight(), image2.getHeight());
Assert.assertEquals(image.getWidth(), image2.getWidth());
Assert.assertEquals(image.getSize(), image2.getSize());
Assert.assertEquals(image.getTitle(), image2.getTitle());
Assert.assertEquals(image.getUri(), image2.getUri());
}
示例10: parseObject
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static final <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) {
if (input == null) {
return null;
}
for (Feature featrue : features) {
featureValues = Feature.config(featureValues, featrue, true);
}
DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues);
T value = (T) parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
示例11: test_AllowSingleQuotes_1
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_AllowSingleQuotes_1() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("{'a':'3'}");
parser.config(Feature.AllowSingleQuotes, true);
JSONObject json = (JSONObject) parser.parse();
Assert.assertEquals(1, json.size());
Assert.assertEquals("3", (String) json.get("a"));
}
示例12: test_1
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_1() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("['1','2']");
parser.config(Feature.AllowISO8601DateFormat, false);
List<Object> list = new ArrayList<Object>();
parser.parseArray(Integer.class, list);
Assert.assertEquals(new Integer(1), list.get(0));
Assert.assertEquals(new Integer(2), list.get(1));
}
示例13: test_default
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public void test_default() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("");
Assert.assertEquals(false, parser.isEnabled(Feature.AllowComment));
Assert.assertEquals(true, parser.isEnabled(Feature.AllowSingleQuotes));
Assert.assertEquals(true, parser.isEnabled(Feature.AllowUnQuotedFieldNames));
Assert.assertEquals(true, parser.isEnabled(Feature.AutoCloseSource));
Assert.assertEquals(true, parser.isEnabled(Feature.InternFieldNames));
}
示例14: parse
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public static Object parse(byte[] input, Feature... features) {
char[] chars = allocateChars(input.length);
int len = IOUtils.decodeUTF8(input, 0, input.length, chars);
if (len < 0) {
return null;
}
return parse(new String(chars, 0, len), features);
}
示例15: perf2
import com.alibaba.fastjson.parser.Feature; //导入依赖的package包/类
public static void perf2() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean);
}
long millis = System.currentTimeMillis() - start;
System.out.println("millis : " + millis);
}