mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-28 13:50:47 +08:00
support DisableSpecialKey
This commit is contained in:
@@ -281,7 +281,7 @@ public class DefaultJSONParser extends AbstractJSONParser implements Closeable {
|
||||
|
||||
lexer.resetStringPosition();
|
||||
|
||||
if (key == JSON.DEFAULT_TYPE_KEY) {
|
||||
if (key == JSON.DEFAULT_TYPE_KEY && !isEnabled(Feature.DisableSpecialKeyDetect)) {
|
||||
String typeName = lexer.scanSymbol(symbolTable, '"');
|
||||
Class<?> clazz = TypeUtils.loadClass(typeName);
|
||||
|
||||
@@ -326,7 +326,7 @@ public class DefaultJSONParser extends AbstractJSONParser implements Closeable {
|
||||
return deserializer.deserialze(this, clazz, fieldName);
|
||||
}
|
||||
|
||||
if (key == "$ref") {
|
||||
if (key == "$ref" && !isEnabled(Feature.DisableSpecialKeyDetect)) {
|
||||
lexer.nextToken(JSONToken.LITERAL_STRING);
|
||||
if (lexer.token() == JSONToken.LITERAL_STRING) {
|
||||
String ref = lexer.stringVal();
|
||||
|
||||
@@ -90,7 +90,13 @@ public enum Feature {
|
||||
* @since 1.2.3
|
||||
*
|
||||
*/
|
||||
OrderedField
|
||||
OrderedField,
|
||||
|
||||
/**
|
||||
* @since 1.2.5
|
||||
*
|
||||
*/
|
||||
DisableSpecialKeyDetect
|
||||
;
|
||||
|
||||
private Feature(){
|
||||
|
||||
@@ -128,7 +128,7 @@ public class MapDeserializer implements ObjectDeserializer {
|
||||
|
||||
lexer.resetStringPosition();
|
||||
|
||||
if (key == JSON.DEFAULT_TYPE_KEY) {
|
||||
if (key == JSON.DEFAULT_TYPE_KEY && !parser.isEnabled(Feature.DisableSpecialKeyDetect)) {
|
||||
String typeName = lexer.scanSymbol(parser.getSymbolTable(), '"');
|
||||
Class<?> clazz = TypeUtils.loadClass(typeName);
|
||||
|
||||
@@ -205,7 +205,8 @@ public class MapDeserializer implements ObjectDeserializer {
|
||||
break;
|
||||
}
|
||||
|
||||
if (lexer.token() == JSONToken.LITERAL_STRING && lexer.isRef()) {
|
||||
if (lexer.token() == JSONToken.LITERAL_STRING && lexer.isRef()
|
||||
&& !parser.isEnabled(Feature.DisableSpecialKeyDetect)) {
|
||||
Object object = null;
|
||||
|
||||
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
|
||||
@@ -243,7 +244,8 @@ public class MapDeserializer implements ObjectDeserializer {
|
||||
|
||||
if (map.size() == 0 //
|
||||
&& lexer.token() == JSONToken.LITERAL_STRING //
|
||||
&& JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal())) {
|
||||
&& JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal()) //
|
||||
&& !parser.isEnabled(Feature.DisableSpecialKeyDetect)) {
|
||||
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
|
||||
lexer.nextToken(JSONToken.COMMA);
|
||||
if (lexer.token() == JSONToken.RBRACE) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.alibaba.json.bvt;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.fastjson.parser.Feature;
|
||||
|
||||
public class DisableSpecialKeyDetectTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String json = "{\"schema\":{\"$ref\":{\"@title\":\"类目ID\",\"@type\":\"string\"},\"$\":{\"@\":\"类目名称\",\"type\":\"string\"},\"cat_desc\":{\"title\":\"类目描述\",\"type\":\"string\"}}}";
|
||||
JSONObject errorJson = JSON.parseObject(json, Feature.DisableSpecialKeyDetect);
|
||||
JSONObject schema = errorJson.getJSONObject("schema");
|
||||
Set<Map.Entry<String, Object>> es2 = schema.entrySet();
|
||||
for (Map.Entry<String, Object> entry : es2) {
|
||||
System.out.println(entry.getKey() + "_" + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
String text = "{\"@v1\":\"v1\",\"@type\":\"v2\", \"@\":\"v3\",\"$\":\"v4\",\"$ref\":\"v5\"}";
|
||||
JSONObject json = JSON.parseObject(text, Feature.DisableSpecialKeyDetect);
|
||||
Assert.assertEquals("v1", json.getString("@v1"));
|
||||
Assert.assertEquals("v2", json.getString("@type"));
|
||||
Assert.assertEquals("v3", json.getString("@"));
|
||||
Assert.assertEquals("v4", json.getString("$"));
|
||||
Assert.assertEquals("v5", json.getString("$ref"));
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String text = "{\"@v1\":\"v1\",\"@type\":\"v2\", \"@\":\"v3\",\"$\":\"v4\",\"$ref\":\"v5\"}";
|
||||
Map<String,String> map = JSON.parseObject(text, new TypeReference<Map<String,String>>(){}, Feature.DisableSpecialKeyDetect);
|
||||
Assert.assertEquals("v1", map.get("@v1"));
|
||||
Assert.assertEquals("v2", map.get("@type"));
|
||||
Assert.assertEquals("v3", map.get("@"));
|
||||
Assert.assertEquals("v4", map.get("$"));
|
||||
Assert.assertEquals("v5", map.get("$ref"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user