Skip to content

Commit

Permalink
Merge pull request #1 from alibaba/master
Browse files Browse the repository at this point in the history
merge master
  • Loading branch information
J-Cod3r authored May 10, 2020
2 parents 31e8496 + e2cfb4f commit 1f846a3
Show file tree
Hide file tree
Showing 26 changed files with 458 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ https://github.com/eishay/jvm-serializers/wiki
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
<version>1.2.68</version>
</dependency>
```

Expand All @@ -59,7 +59,7 @@ https://github.com/eishay/jvm-serializers/wiki
## Gradle via JCenter

``` groovy
compile 'com.alibaba:fastjson:1.2.61'
compile 'com.alibaba:fastjson:1.2.68'
```

``` groovy
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/alibaba/fastjson/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,15 @@ private static void paths(Map<Object, String> values, Map<String, Object> paths,

String p = values.put(javaObject, parent);
if (p != null) {
boolean basicType = javaObject instanceof String
Class<?> type = javaObject.getClass();
boolean basicType = type == String.class
|| type == Boolean.class
|| type == Character.class
|| type == UUID.class
|| type.isEnum()
|| javaObject instanceof Number
|| javaObject instanceof Date
|| javaObject instanceof UUID;
;

if (!basicType) {
return;
Expand Down Expand Up @@ -886,7 +891,13 @@ Object parseArrayAccessFilter(boolean acceptBracket) {
predicateFlag = true;
}

if (predicateFlag || IOUtils.firstIdentifier(ch) || ch == '\\' || ch == '@') {
//

if (predicateFlag
|| IOUtils.firstIdentifier(ch)
|| Character.isJavaIdentifierStart(ch)
|| ch == '\\'
|| ch == '@') {
boolean self = false;
if (ch == '@') {
next();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/asm/ByteVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public ByteVector putUTF8(final String s) {
data[len++] = (byte) charLength;
for (int i = 0; i < charLength; ++i) {
final char c = s.charAt(i);
if (c >= '\001' && c <= '\177') {
if ((c >= '\001' && c <= '\177') || (c >= '\u4E00' && c <= '\u9FFF')) {
data[len++] = (byte) c;
} else {
throw new UnsupportedOperationException();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public static ParserConfig getGlobalInstance() {
0xC963695082FD728EL,
0xD1EFCDF4B3316D34L,
0xD54B91CC77B239EDL,
0xD59EE91F0B09EA01L,
0xD8CA3D595E982BACL,
0xDE23A0809A8B9BD6L,
0xDEFC208F237D4104L,
Expand Down Expand Up @@ -1373,6 +1374,10 @@ public Class<?> checkAutoType(String typeName, Class<?> expectClass, int feature
if (Arrays.binarySearch(acceptHashCodes, hash) >= 0) {
clazz = TypeUtils.loadClass(typeName, defaultClassLoader, true);

if (clazz == null) {
return expectClass;
}

if (expectClass != null && expectClass.isAssignableFrom(clazz)) {
throw new JSONException("type not match. " + typeName + " -> " + expectClass.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
int intValue = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);

if (intValue < 0 || intValue > ordinalEnums.length) {
if (intValue < 0 || intValue >= ordinalEnums.length) {
throw new JSONException("parse enum " + enumClass.getName() + " error, value : " + intValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,28 +510,28 @@ protected <T> T deserialze(DefaultJSONParser parser, //
String typeKey = beanInfo.typeKey;
for (int fieldIndex = 0, notMatchCount = 0;; fieldIndex++) {
String key = null;
FieldDeserializer fieldDeser = null;
FieldDeserializer fieldDeserializer = null;
FieldInfo fieldInfo = null;
Class<?> fieldClass = null;
JSONField feildAnnotation = null;
boolean customDeserilizer = false;
JSONField fieldAnnotation = null;
boolean customDeserializer = false;
if (fieldIndex < sortedFieldDeserializers.length && notMatchCount < 16) {
fieldDeser = sortedFieldDeserializers[fieldIndex];
fieldInfo = fieldDeser.fieldInfo;
fieldDeserializer = sortedFieldDeserializers[fieldIndex];
fieldInfo = fieldDeserializer.fieldInfo;
fieldClass = fieldInfo.fieldClass;
feildAnnotation = fieldInfo.getAnnotation();
if (feildAnnotation != null && fieldDeser instanceof DefaultFieldDeserializer) {
customDeserilizer = ((DefaultFieldDeserializer) fieldDeser).customDeserilizer;
fieldAnnotation = fieldInfo.getAnnotation();
if (fieldAnnotation != null && fieldDeserializer instanceof DefaultFieldDeserializer) {
customDeserializer = ((DefaultFieldDeserializer) fieldDeserializer).customDeserilizer;
}
}

boolean matchField = false;
boolean valueParsed = false;

Object fieldValue = null;
if (fieldDeser != null) {
if (fieldDeserializer != null) {
char[] name_chars = fieldInfo.name_chars;
if (customDeserilizer && lexer.matchField(name_chars)) {
if (customDeserializer && lexer.matchField(name_chars)) {
matchField = true;
} else if (fieldClass == int.class || fieldClass == Integer.class) {
int intVal = lexer.scanFieldInt(name_chars);
Expand Down Expand Up @@ -651,10 +651,10 @@ protected <T> T deserialze(DefaultJSONParser parser, //
}
} else if (fieldClass.isEnum() //
&& parser.getConfig().getDeserializer(fieldClass) instanceof EnumDeserializer
&& (feildAnnotation == null || feildAnnotation.deserializeUsing() == Void.class)
&& (fieldAnnotation == null || fieldAnnotation.deserializeUsing() == Void.class)
) {
if (fieldDeser instanceof DefaultFieldDeserializer) {
ObjectDeserializer fieldValueDeserilizer = ((DefaultFieldDeserializer) fieldDeser).fieldValueDeserilizer;
if (fieldDeserializer instanceof DefaultFieldDeserializer) {
ObjectDeserializer fieldValueDeserilizer = ((DefaultFieldDeserializer) fieldDeserializer).fieldValueDeserilizer;
fieldValue = this.scanEnum(lexer, name_chars, fieldValueDeserilizer);

if (lexer.matchStat > 0) {
Expand Down Expand Up @@ -835,7 +835,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //

if (matchField) {
if (!valueParsed) {
fieldDeser.parseField(parser, object, type, fieldValues);
fieldDeserializer.parseField(parser, object, type, fieldValues);
} else {
if (object == null) {
fieldValues.put(fieldInfo.name, fieldValue);
Expand All @@ -846,10 +846,10 @@ protected <T> T deserialze(DefaultJSONParser parser, //
&& fieldClass != double.class //
&& fieldClass != boolean.class //
) {
fieldDeser.setValue(object, fieldValue);
fieldDeserializer.setValue(object, fieldValue);
}
} else {
fieldDeser.setValue(object, fieldValue);
fieldDeserializer.setValue(object, fieldValue);
}

if (setFlags != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,22 @@ protected <T> T castTimestamp(DefaultJSONParser parser, Type clazz, Object field
long longVal;
JSONScanner dateLexer = new JSONScanner(strVal);
try {
if (strVal.length() > 19
&& strVal.charAt(4) == '-'
&& strVal.charAt(7) == '-'
&& strVal.charAt(10) == ' '
&& strVal.charAt(13) == ':'
&& strVal.charAt(16) == ':'
&& strVal.charAt(19) == '.') {
String dateFomartPattern = parser.getDateFomartPattern();
if (dateFomartPattern.length() != strVal.length() && dateFomartPattern == JSON.DEFFAULT_DATE_FORMAT) {
return (T) java.sql.Timestamp.valueOf(strVal);
}
}

if (dateLexer.scanISO8601DateIfMatch(false)) {
longVal = dateLexer.getCalendar().getTimeInMillis();
} else {
if (strVal.length() == 29) {
String dateFomartPattern = parser.getDateFomartPattern();
if (dateFomartPattern.length() != 29 && dateFomartPattern == JSON.DEFFAULT_DATE_FORMAT) {
return (T) java.sql.Timestamp.valueOf(strVal);
}
}
DateFormat dateFormat = parser.getDateFormat();
try {
java.util.Date date = (java.util.Date) dateFormat.parse(strVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ public JavaBeanSerializer createJavaBeanSerializer(SerializeBeanInfo beanInfo) t
classNameFull = className;
}

String packageName = ASMSerializerFactory.class.getPackage().getName();


ClassWriter cw = new ClassWriter();
cw.visit(V1_5 //
, ACC_PUBLIC + ACC_SUPER //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
serializer.setContext(context, object, fieldName, 0);

if (out.isEnabled(SerializerFeature.WriteClassName)) {
if (HashSet.class == collection.getClass()) {
if (HashSet.class.isAssignableFrom(collection.getClass())) {
out.append("Set");
} else if (TreeSet.class == collection.getClass()) {
out.append("TreeSet");
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/alibaba/fastjson/serializer/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,23 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
IOUtils.getChars(year, 4, buf);
}
}
out.write(buf);


if (nanos > 0) { // java.sql.Timestamp
int i = 0;
for (; i < 9; ++i) {
int off = buf.length - i - 1;
if (buf[off] != '0') {
break;
}
}
out.write(buf, 0, buf.length - i);
out.write(quote);
return;
}

out.write(buf);

float timeZoneF = calendar.getTimeZone().getOffset(calendar.getTimeInMillis()) / (3600.0f * 1000);
int timeZone = (int)timeZoneF;
if (timeZone == 0.0) {
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,28 @@ public ObjectSerializer getObjectWriter(Class<?> clazz) {
public ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
ObjectSerializer writer = get(clazz);

if (writer == null) {
try {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
if (!(o instanceof AutowiredObjectSerializer)) {
continue;
}
if (writer != null) {
return writer;
}

AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
for (Type forType : autowired.getAutowiredFor()) {
put(forType, autowired);
}
try {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
if (!(o instanceof AutowiredObjectSerializer)) {
continue;
}
} catch (ClassCastException ex) {
// skip
}

writer = get(clazz);
AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
for (Type forType : autowired.getAutowiredFor()) {
put(forType, autowired);
}
}
} catch (ClassCastException ex) {
// skip
}

writer = get(clazz);

if (writer == null) {
final ClassLoader classLoader = JSON.class.getClassLoader();
if (classLoader != Thread.currentThread().getContextClassLoader()) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/alibaba/json/bvt/SqlTimestampTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ public void test_date_1() throws Exception {
assertEquals('"' + ts.toString() + '"', '"' + ts2.toString() + '"');
}

// 1997-03-17 15:53:01.01
public void test_date_2() throws Exception {
// 2020-04-11 03:10:19.516
Timestamp ts = new Timestamp(
97,
3,
17,
15,
53,
01,
10000000
);

System.out.println('"' + ts.toString() + '"');

String json = JSON.toJSONString(ts, SerializerFeature.UseISO8601DateFormat);
System.out.println(json);
Timestamp ts2 = JSON.parseObject(json, Timestamp.class);
String json2 = JSON.toJSONString(ts2, SerializerFeature.UseISO8601DateFormat);
System.out.println(json2);
assertEquals('"' + ts.toString() + '"', '"' + ts2.toString() + '"');
}

public void test_date_999999999() throws Exception {
// 2020-04-11 03:10:19.516
Timestamp ts = new Timestamp(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.json.bvt.bug;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.junit.Assert;
Expand All @@ -12,7 +12,7 @@
public class Bug_for_smoothrat6 extends TestCase {

public void test_set() throws Exception {
Set<Object> set = new HashSet<Object>();
Set<Object> set = new LinkedHashSet<Object>();
set.add(3L);
set.add(4L);

Expand Down
33 changes: 32 additions & 1 deletion src/test/java/com/alibaba/json/bvt/issue_2000/Issue2065.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package com.alibaba.json.bvt.issue_2000;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;
import org.junit.Test;

public class Issue2065 extends TestCase {

public void test_for_issue() throws Exception {
// JSON.parseObject("{\"code\":1}", Model.class);
Exception error = null;
try {
JSON.parseObject("{\"code\":1}", Model.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
error.printStackTrace();
}

public void test_for_issue_01() {
Exception error = null;
try {
JSON.parseObject("1", EnumClass.class);
} catch (JSONException e) {
error = e;
}
assertNotNull(error);
error.printStackTrace();
}

@Test
public void test_for_issue_02() {
JSON.parseObject("0", EnumClass.class);
}

@Test
public void test_for_issue_03() {
JSON.parseObject("{\"code\":0}", Model.class);
}

public static class Model {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_2700/Issue2721Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.json.bvt.issue_2700;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONPath;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class Issue2721Test extends TestCase
{
public void test2721() {
String chineseKeyString = "[{\"名称\": \"脆皮青豆\", \"配料\": [\"豌豆\", \"棕榈油\", \"白砂糖\", \"食用盐\", \"玉米淀粉\"]}]";
System.out.println(JSONPath.read(chineseKeyString, "$[名称 = '脆皮青豆']"));
// [{"名称":"脆皮青豆","配料":["豌豆","棕榈油","白砂糖","食用盐","玉米淀粉"]}]

String normalKeyString = "[{ \"name\": \"脆皮青豆\", \"配料\": [\"豌豆\", \"棕榈油\", \"白砂糖\", \"食用盐\", \"玉米淀粉\"] }]";
System.out.println(JSONPath.read(normalKeyString, "$[name = '脆皮青豆']"));
// [{"name":"脆皮青豆","配料":["豌豆","棕榈油","白砂糖","食用盐","玉米淀粉"]}]
//
Assert.assertFalse("Chinese Key is NOT OK, Array length is 0!", ((List) JSONPath.read(chineseKeyString, "$[名称 = '脆皮青豆']")).isEmpty());
Assert.assertFalse("Chinese Key is NOT OK, Array length is 0!", ((List) JSONPath.read(normalKeyString, "$[name = '脆皮青豆']")).isEmpty());
}
}
Loading

0 comments on commit 1f846a3

Please sign in to comment.