Skip to content

Commit

Permalink
bug fixed for BrowserSecure.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 13, 2017
1 parent 649d522 commit 3708811
Show file tree
Hide file tree
Showing 28 changed files with 1,093 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/alibaba/fastjson/JSON.java
Expand Up @@ -161,6 +161,9 @@ public static Object parse(String text, int features) {
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);
}

Expand Down Expand Up @@ -382,8 +385,14 @@ public static <T> T parseObject(byte[] bytes, int offset, int len, Charset chars
if (charset == IOUtils.UTF8) {
char[] chars = allocateChars(bytes.length);
int chars_len = IOUtils.decodeUTF8(bytes, offset, len, chars);
if (chars_len < 0) {
return null;
}
strVal = new String(chars, 0, chars_len);
} else {
if (len < 0) {
return null;
}
strVal = new String(bytes, offset, len, charset);
}
return (T) parseObject(strVal, clazz, features);
Expand Down
48 changes: 38 additions & 10 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java
Expand Up @@ -774,7 +774,13 @@ public void writeStringWithDoubleQuote(String text, final char seperator) {
char ch = text.charAt(i);

if (isEnabled(SerializerFeature.BrowserSecure)) {
if (!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z')
if (ch == '<') {
write("&lt;");
continue;
} else if (ch == '>') {
write("&gt;");
continue;
} else if (!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z')
&& !(ch == ',') && !(ch == '.') && !(ch == '_')) {
write('\\');
write('u');
Expand Down Expand Up @@ -864,7 +870,11 @@ public void writeStringWithDoubleQuote(String text, final char seperator) {
if (!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch == ',')
&& !(ch == '.') && !(ch == '_')) {
lastSpecialIndex = i;
newcount += 5;
if (ch == '<' || ch == '>') {
newcount += 3;
} else {
newcount += 5;
}
continue;
}
}
Expand All @@ -879,14 +889,32 @@ public void writeStringWithDoubleQuote(String text, final char seperator) {

if (!(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch == ',')
&& !(ch == '.') && !(ch == '_')) {
System.arraycopy(buf, i + 1, buf, i + 6, end - i - 1);
buf[i] = '\\';
buf[i + 1] = 'u';
buf[i + 2] = IOUtils.DIGITS[(ch >>> 12) & 15];
buf[i + 3] = IOUtils.DIGITS[(ch >>> 8) & 15];
buf[i + 4] = IOUtils.DIGITS[(ch >>> 4) & 15];
buf[i + 5] = IOUtils.DIGITS[ch & 15];
end += 5;
if (ch == '<') {
// &lt;
System.arraycopy(buf, i + 1, buf, i + 4, end - i - 1);
buf[i] = '&';
buf[i + 1] = 'l';
buf[i + 2] = 't';
buf[i + 3] = ';';
end += 3;
} else if (ch == '>') {
// &lt;
System.arraycopy(buf, i + 1, buf, i + 4, end - i - 1);
buf[i] = '&';
buf[i + 1] = 'g';
buf[i + 2] = 't';
buf[i + 3] = ';';
end += 3;
} else {
System.arraycopy(buf, i + 1, buf, i + 6, end - i - 1);
buf[i] = '\\';
buf[i + 1] = 'u';
buf[i + 2] = IOUtils.DIGITS[(ch >>> 12) & 15];
buf[i + 3] = IOUtils.DIGITS[(ch >>> 8) & 15];
buf[i + 4] = IOUtils.DIGITS[(ch >>> 4) & 15];
buf[i + 5] = IOUtils.DIGITS[ch & 15];
end += 5;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/alibaba/json/bvt/CurrencyTest5.java
Expand Up @@ -18,7 +18,7 @@ public void test_0() throws Exception {
jsonObject.put("value", Currency.getInstance("CNY"));

String text = JSON.toJSONString(jsonObject, config);
System.out.println(text);
assertEquals("{\"value\":{\"currencyCode\":\"CNY\",\"displayName\":\"Chinese Yuan\",\"symbol\":\"CNY\"}}", text);

Currency currency = JSON.parseObject(text, VO.class).value;

Expand Down
Expand Up @@ -31,7 +31,8 @@ public void test_special_browsecue() throws Exception {
model.name = buf.toString();

String text = JSON.toJSONString(model, SerializerFeature.BrowserSecure);

text = text.replaceAll("&lt;", "<");
text = text.replaceAll("&gt;", ">");
Model model2 = JSON.parseObject(text, Model.class);
Assert.assertEquals(model.name, model2.name);
}
Expand Down
Expand Up @@ -31,7 +31,8 @@ public void test_special_browsecue() throws Exception {
model.name = buf.toString();

String text = JSON.toJSONString(model, SerializerFeature.BrowserSecure);

text = text.replaceAll("&lt;", "<");
text = text.replaceAll("&gt;", ">");
Model model2 = JSON.parseObject(text, Model.class);
Assert.assertEquals(model.name, model2.name);
}
Expand Down
Expand Up @@ -36,8 +36,13 @@ public void test_special_browsecue() throws Exception {
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, model, SerializerFeature.BrowserSecure);

Model model2 = JSON.parseObject(writer.toString(), Model.class);
Assert.assertEquals(model.name, model2.name);
String text = writer.toString();

text = text.replaceAll("&lt;", "<");
text = text.replaceAll("&gt;", ">");

Model model2 = JSON.parseObject(text, Model.class);
assertEquals(model.name, model2.name);
}

public void test_special_browsecompatible() throws Exception {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Issue569.java
@@ -0,0 +1,42 @@
package com.alibaba.json.bvt.bug;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import junit.framework.TestCase;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Map;

/**
* Created by wenshao on 02/07/2017.
*/
public class Issue569 extends TestCase {
public void test_for_issue() throws Exception {
String jsonString = "{\"backingMap\":{\"a\":{\"b\":{}}}}";
Type type = new TypeReference<MyTable<String, String, MyValue>>() {}.getType();
MyTable<String, String, MyValue> table = JSON.parseObject(jsonString, type);

Map<String, MyValue> valueMap = table.backingMap.get("a");
assertNotNull(valueMap);

MyValue value = valueMap.get("b");
assertNotNull(value);
}

public static class MyTable<R, C, V> implements Serializable {
private Map<R, Map<C, V>> backingMap;

public Map<R, Map<C, V>> getBackingMap() {
return backingMap;
}

public void setBackingMap(Map<R, Map<C, V>> backingMap) {
this.backingMap = backingMap;
}
}

public static class MyValue {

}
}
28 changes: 28 additions & 0 deletions src/test/java/com/alibaba/json/bvt/emoji/EmojiTest0.java
@@ -0,0 +1,28 @@
package com.alibaba.json.bvt.emoji;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;

import java.io.ByteArrayOutputStream;

/**
* Created by wenshao on 13/04/2017.
*/
public class EmojiTest0 extends TestCase {
public void test_for_emoji() throws Exception {
Model model = new Model();
model.value = "An 😀awesome 😃string with a few 😉emojis!";

ByteArrayOutputStream out = new ByteArrayOutputStream();

JSON.writeJSONString(out, model);

String text = new String(out.toByteArray(), "UTF-8");
System.out.println(text);
}

public static class Model {
public String value;
}
}
46 changes: 46 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1200/Issue1229.java
@@ -0,0 +1,46 @@
package com.alibaba.json.bvt.issue_1200;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import junit.framework.TestCase;

import java.util.List;

/**
* Created by wenshao on 30/05/2017.
*/
public class Issue1229 extends TestCase {
public void test_for_issue() throws Exception {
final Object parsed = JSON.parse("{\"data\":{}}");
assertTrue(parsed instanceof JSONObject);
assertTrue(((JSONObject)parsed).get("data") instanceof JSONObject);

final Result<Data> result = JSON.parseObject("{\"data\":{}}", new TypeReference<Result<Data>>(){});
assertNotNull(result.data);
assertTrue(result.data instanceof Data);

final Result<List<Data>> result2 = JSON.parseObject("{\"data\":[]}", new TypeReference<Result<List<Data>>>(){});
assertNotNull(result2.data);
assertTrue(result2.data instanceof List);
assertEquals(0, result2.data.size());
}

public void parseErr() throws Exception {
JSON.parseObject("{\"data\":{}}", new TypeReference<Result<List<Data>>>(){});
fail("should be failed due to error json");
}

public static class Result<T>{
T data;
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}

public static class Data {
}
}
90 changes: 90 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1200/Issue1233.java
@@ -0,0 +1,90 @@
package com.alibaba.json.bvt.issue_1200;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import junit.framework.TestCase;

import java.lang.reflect.Type;
import java.util.List;

/**
* Created by wenshao on 30/05/2017.
*/
public class Issue1233 extends TestCase {
public void test_for_issue() throws Exception {
ParserConfig.getGlobalInstance().putDeserializer(Area.class, new ObjectDeserializer() {
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONObject jsonObject = (JSONObject) parser.parse();
String areaType;

if (jsonObject.get("type") instanceof String) {
areaType = (String) jsonObject.get("type");
} else {
return null;
}
if (Area.TYPE_SECTION.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, Section.class);
} else if (Area.TYPE_FLOORV1.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, FloorV1.class);
} else if (Area.TYPE_FLOORV2.equals(areaType)) {
return (T) JSON.toJavaObject(jsonObject, FloorV2.class);
}
return null;
}

public int getFastMatchToken() {
return 0;
}
});

JSONObject jsonObject = JSON.parseObject("{\"type\":\"floorV2\",\"templateId\":\"x123\"}");

FloorV2 floorV2 = (FloorV2) jsonObject.toJavaObject(Area.class);
assertNotNull(floorV2);
assertEquals("x123", floorV2.templateId);
}

public interface Area {
public static final String TYPE_SECTION = "section";
public static final String TYPE_FLOORV1 = "floorV1";
public static final String TYPE_FLOORV2 = "floorV2";

String getName();
}

public static class Section implements Area {
public List<Area> children;

public String type;

public String templateId;

public String getName() {
return templateId;
}
}

public static class FloorV1 implements Area {
public String type;
public String templateId;

public String getName() {
return templateId;
}
}

public static class FloorV2 implements Area {
public List<Area> children;

public String type;

public String templateId;

public String getName() {
return templateId;
}
}
}

0 comments on commit 3708811

Please sign in to comment.