Skip to content

Commit

Permalink
* 支持对 \" 进行反转义
Browse files Browse the repository at this point in the history
  • Loading branch information
fjn committed Sep 6, 2022
1 parent 62e445b commit 6673af3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
22 changes: 20 additions & 2 deletions easyjson-core/src/main/java/com/jn/easyjson/core/JSONBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ public abstract class JSONBuilder implements Cloneable {

private JsonCustomizer jsonHandlerCustomizer;

/**
* 对 \xFF 的处理
*/
private boolean enableDecodeHex = false;

/**
* 对 \" 的处理
*/
private boolean enableUnescapeQuote = false;

/**
* 全局配置项, 这里面其实是每个类每个字段的默认配置项
*/
Expand Down Expand Up @@ -319,15 +327,24 @@ public boolean serializeBooleanUsing1_0() {
return serializeBooleanUsing1_0;
}

public JSONBuilder enableDecodeHex(boolean decodeHex){
public JSONBuilder enableDecodeHex(boolean decodeHex) {
this.enableDecodeHex = decodeHex;
return this;
}

public boolean enableDecodeHex(){
public boolean enableDecodeHex() {
return this.enableDecodeHex;
}

public boolean enableUnescapeQuote() {
return enableUnescapeQuote;
}

public JSONBuilder enableUnescapeQuote(boolean enableUnescapeQuote) {
this.enableUnescapeQuote = enableUnescapeQuote;
return this;
}

public JSONBuilder enableIgnoreAnnotation() {
IgnoreAnnotationExclusion ignoreAnnotationExclusion = new IgnoreAnnotationExclusion();
exclusionConfiguration.appendExclusion(ignoreAnnotationExclusion, true, true);
Expand Down Expand Up @@ -450,5 +467,6 @@ protected <E extends JSONBuilder> void copyTo(E builder) {
builder.jsonHandlerCustomizer(this.jsonHandlerCustomizer);
builder.beanPropertyNamingPolicy(this.beanPropertyNamingPolicy);
builder.enableDecodeHex(this.enableDecodeHex);
builder.enableUnescapeQuote(this.enableUnescapeQuote);
}
}
19 changes: 11 additions & 8 deletions easyjson-core/src/main/java/com/jn/easyjson/core/util/JSONs.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import java.util.List;
import java.util.Map;

public class JSONs extends JsonTreeNodes{
private static JSON json = JSONBuilderProvider.create().enableDecodeHex(true).build();
public class JSONs extends JsonTreeNodes {
private static JSON json = JSONBuilderProvider.create()
.enableDecodeHex(true)
.enableUnescapeQuote(true)
.build();

public static JsonNodeNavigator JSON_NODE_NAVIGATOR = new JsonNodeNavigator();

/**
Expand Down Expand Up @@ -104,37 +108,36 @@ public static <T> T toJavaObject(String jsonString) {
/**
* @since 3.2.22
*/
public static <T> T parse(String jsonString, Type t){
public static <T> T parse(String jsonString, Type t) {
return json.fromJson(jsonString, t);
}

/**
* @since 3.2.22
*/
public static JsonTreeNode parse(String jsonString){
public static JsonTreeNode parse(String jsonString) {
return json.fromJson(jsonString);
}



/**
* @since 3.2.22
*/
public static JsonObjectNode parseObject(String jsonString){
public static JsonObjectNode parseObject(String jsonString) {
return (JsonObjectNode) parse(jsonString);
}

/**
* @since 3.2.22
*/
public static JsonArrayNode parseArray(String jsonString){
public static JsonArrayNode parseArray(String jsonString) {
return (JsonArrayNode) parse(jsonString);
}

/**
* @since 3.2.23
*/
public static String toJson(Object obj){
public static String toJson(Object obj) {
return json.toJson(obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@

package com.jn.easyjson.fastjson;

import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONReaderScanner;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.jn.easyjson.core.JsonException;
import com.jn.easyjson.core.JsonHandlerAdapter;
import com.jn.easyjson.core.JsonTreeNode;
import com.jn.easyjson.core.tree.JsonTreeDeserializer;
import com.jn.langx.util.Strings;
import com.jn.langx.util.io.IOs;
import com.jn.langx.util.io.unicode.Utf8s;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;

public class FastJsonAdapter extends JsonHandlerAdapter<FastJson> {

@Override
public <T> T deserialize(String json, Type typeOfT) throws JsonException {
if (getJsonBuilder().enableDecodeHex()) {
json = Utf8s.convertHexToUnicode(json);
}
if (getJsonBuilder().enableUnescapeQuote()) {
json = Strings.replace(json, "\\\"", "\"");
json = Strings.replace(json, "\\\\\"", "\\\"");
}
DefaultJSONParser parser = getDelegate().getDeserializerBuilder().build(json);
T value = parser.parseObject(typeOfT);
parser.handleResovleTask(value);
Expand All @@ -39,9 +48,12 @@ public <T> T deserialize(String json, Type typeOfT) throws JsonException {

@Override
public <T> T deserialize(Reader reader, Type typeOfT) throws JsonException {
JSONReaderScanner jsonReaderScanner = new JSONReaderScanner(reader, getDelegate().getDeserializerBuilder().getFeatures());
JSONReader jsonReader = new JSONReader(jsonReaderScanner);
return jsonReader.readObject(typeOfT);
try {
String json = IOs.readAsString(reader);
return deserialize(json, typeOfT);
} catch (IOException ex) {
throw new JsonException(ex.getMessage(), ex);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.jn.easyjson.core.JsonHandlerAdapter;
import com.jn.easyjson.core.JsonTreeNode;
import com.jn.easyjson.gson.node.GsonJsonMapper;
import com.jn.langx.util.Strings;
import com.jn.langx.util.io.IOs;
import com.jn.langx.util.io.unicode.Utf8s;

Expand All @@ -44,6 +45,10 @@ public <T> T deserialize(String json, Type typeOfT) throws JsonException {
if (getJsonBuilder().enableDecodeHex()) {
json = Utf8s.convertHexToUnicode(json);
}
if(getJsonBuilder().enableUnescapeQuote()){
json = Strings.replace(json, "\\\"", "\"");
json = Strings.replace(json, "\\\\\"", "\\\"");
}
return getDelegate().fromJson(json, typeOfT);
}

Expand All @@ -62,6 +67,10 @@ public JsonTreeNode deserialize(String json) throws JsonException {
if (getJsonBuilder().enableDecodeHex()) {
json = Utf8s.convertHexToUnicode(json);
}
if(getJsonBuilder().enableUnescapeQuote()){
json = Strings.replace(json, "\\\"", "\"");
json = Strings.replace(json, "\\\\\"", "\\\"");
}
JsonElement node = new JsonParser().parse(json);
return GsonJsonMapper.toJsonTreeNode(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.jn.easyjson.core.JsonHandlerAdapter;
import com.jn.easyjson.core.JsonTreeNode;
import com.jn.easyjson.jackson.node.JacksonJsonMapper;
import com.jn.langx.util.Strings;
import com.jn.langx.util.collection.multivalue.MultiValueMap;
import com.jn.langx.util.io.IOs;
import com.jn.langx.util.io.unicode.Utf8s;
Expand All @@ -44,6 +45,10 @@ public <T> T deserialize(String json, Type typeOfT) throws JsonException {
if (getJsonBuilder().enableDecodeHex()) {
json = Utf8s.convertHexToUnicode(json);
}
if(getJsonBuilder().enableUnescapeQuote()){
json = Strings.replace(json, "\\\"", "\"");
json = Strings.replace(json, "\\\\\"", "\\\"");
}
return getDelegate().readValue(json, toJavaType(typeOfT));
} catch (Throwable ex) {
throw new JsonException(ex);
Expand Down Expand Up @@ -97,6 +102,10 @@ public JsonTreeNode deserialize(String json) throws JsonException {
if (getJsonBuilder().enableDecodeHex()) {
json = Utf8s.convertHexToUnicode(json);
}
if(getJsonBuilder().enableUnescapeQuote()){
json = Strings.replace(json, "\\\"", "\"");
json = Strings.replace(json, "\\\\\"", "\\\"");
}
JsonNode jsonNode = getDelegate().readTree(json);
return JacksonJsonMapper.toJsonTreeNode(jsonNode);
} catch (Throwable ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
public class EasyjsonHexUnicodeTest extends AbstractBaseTest {

@Test(priority = 10103)
public void decodeHexOrUnicode(){
public void decodeHexOrUnicode() {

String jsonString2 = "{\"hello\":\"wor\\u005Cld\"}";
String jsonString1 = "{\"hello\":\"wor\\x5Cld\"}";

Map map2 = JSONs.parse(jsonString2,Map.class);
Map map1 = JSONs.parse(jsonString1,Map.class);
Map map2 = JSONs.parse(jsonString2, Map.class);
Map map1 = JSONs.parse(jsonString1, Map.class);
System.out.println(map2);
System.out.println(map1);

String jsonString3 = "{\\\"hello\\\":\\\"wor\\\\u005Cld\\\"}";
Map map3 = JSONs.parse(jsonString3, Map.class);
System.out.println(map3);
}


Expand Down

0 comments on commit 6673af3

Please sign in to comment.