Skip to content

Commit

Permalink
Merge pull request alibaba#5 from alibaba/master
Browse files Browse the repository at this point in the history
master merge
  • Loading branch information
ianate committed May 31, 2016
2 parents aaaabe8 + 2be7ad4 commit d024fcd
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ http://repo1.maven.org/maven2/com/alibaba/fastjson/
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
<version>1.2.12</version>
</dependency>
```

```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.51.android</version>
<version>1.1.52.android</version>
</dependency>
```

Expand Down
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--
<parent>
<groupId>com.alibaba</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
-->

<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12-SNAPSHOT</version>
<version>1.2.13-SNAPSHOT</version>

<packaging>jar</packaging>
<name>fastjson</name>
Expand Down Expand Up @@ -326,6 +334,13 @@
<version>3.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.4.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,9 @@ public static Object toJSON(Object javaObject, SerializeConfig config) {
}
return json;
}

return null;

String text = JSON.toJSONString(javaObject);
return JSON.parse(text);
}

public static <T> T toJavaObject(JSON json, Class<T> clazz) {
Expand All @@ -911,7 +912,7 @@ public <T> T toJavaObject(Class<T> clazz) {
return TypeUtils.cast(this, clazz, ParserConfig.getGlobalInstance());
}

public final static String VERSION = "1.2.12";
public final static String VERSION = "1.2.13";

private final static ThreadLocal<byte[]> bytesLocal = new ThreadLocal<byte[]>();
private static byte[] allocateBytes(int length) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/alibaba/fastjson/annotation/JSONField.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@
Feature[] parseFeatures() default {};

String label() default "";

/**
* @since 1.2.12
*/
boolean jsonDirect() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Collection;
import java.util.Map;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.JSONLexer;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void parseField(DefaultJSONParser parser, Object object, Type objectType,
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
Type itemType = this.itemType;
ObjectDeserializer itemTypeDeser = this.deserializer;

if (itemType instanceof TypeVariable //
&& objectType instanceof ParameterizedType) {
TypeVariable typeVar = (TypeVariable) itemType;
Expand Down Expand Up @@ -96,45 +95,45 @@ public final void parseArray(DefaultJSONParser parser, Type objectType, Collecti

final JSONLexer lexer = parser.lexer;

if (lexer.token() != JSONToken.LBRACKET) {
String errorMessage = "exepct '[', but " + JSONToken.name(lexer.token());
if (objectType != null) {
errorMessage += ", type : " + objectType;
if (lexer.token() == JSONToken.LBRACKET) {
if (itemTypeDeser == null) {
itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
itemFastMatchToken = deserializer.getFastMatchToken();
}
throw new JSONException(errorMessage);
}

if (itemTypeDeser == null) {
itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
itemFastMatchToken = deserializer.getFastMatchToken();
}
lexer.nextToken(itemFastMatchToken);

for (int i = 0;; ++i) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
while (lexer.token() == JSONToken.COMMA) {
lexer.nextToken();
continue;
}
}

if (lexer.token() == JSONToken.RBRACKET) {
break;
}

lexer.nextToken(itemFastMatchToken);
Object val = itemTypeDeser.deserialze(parser, itemType, i);
array.add(val);

for (int i = 0;; ++i) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
while (lexer.token() == JSONToken.COMMA) {
lexer.nextToken();
parser.checkListResolve(array);

if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(itemFastMatchToken);
continue;
}
}

if (lexer.token() == JSONToken.RBRACKET) {
break;
lexer.nextToken(JSONToken.COMMA);
} else {
if (itemTypeDeser == null) {
itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
}

Object val = itemTypeDeser.deserialze(parser, itemType, i);
Object val = itemTypeDeser.deserialze(parser, itemType, 0);
array.add(val);

parser.checkListResolve(array);

if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(itemFastMatchToken);
continue;
}
}

lexer.nextToken(JSONToken.COMMA);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
String methodName = null;
String fileName = null;
int lineNumber = 0;
String moduleName = null;
String moduleVersion = null;

for (;;) {
// lexer.scanSymbol
Expand Down Expand Up @@ -100,6 +102,22 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
throw new JSONException("syntax error");
}
}
} else if ("moduleName".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
moduleName = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
moduleName = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else if ("moduleVersion".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
moduleVersion = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
moduleVersion = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
} else {
throw new JSONException("syntax error : " + key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public Type getFieldType() {
public int getFeatures() {
return fieldInfo.serialzeFeatures;
}

public boolean isJsonDirect() {
return this.fieldInfo.jsonDirect;
}

public <T extends Annotation> T getAnnation(Class<T> annotationClass) {
return fieldInfo.getAnnation(annotationClass);
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.math.BigDecimal;
Expand Down Expand Up @@ -61,6 +60,7 @@
import com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec;
import com.alibaba.fastjson.parser.deserializer.OptionalCodec;
import com.alibaba.fastjson.util.ASMUtils;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.IdentityHashMap;
import com.alibaba.fastjson.util.ServiceLoader;
import com.alibaba.fastjson.util.TypeUtils;
Expand Down Expand Up @@ -110,6 +110,10 @@ private final JavaBeanSerializer createASMSerializer(SerializeBeanInfo beanInfo)

private final ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
SerializeBeanInfo beanInfo = TypeUtils.buildBeanInfo(clazz, null);
if (beanInfo.fields.length == 0 && Iterable.class.isAssignableFrom(clazz)) {
return MiscCodec.instance;
}

return createJavaBeanSerializer(beanInfo);
}

Expand Down Expand Up @@ -138,11 +142,16 @@ public ObjectSerializer createJavaBeanSerializer(SerializeBeanInfo beanInfo) {
}

if (asm) {
for(Field field : clazz.getDeclaredFields()){
JSONField annotation = field.getAnnotation(JSONField.class);
if (annotation != null //
&& ((!ASMUtils.checkName(annotation.name())) //
|| annotation.format().length() != 0)) {
for(FieldInfo field : beanInfo.fields){
JSONField annotation = field.getAnnotation();

if (annotation == null) {
continue;
}
if ((!ASMUtils.checkName(annotation.name())) //
|| annotation.format().length() != 0
|| annotation.jsonDirect()
) {
asm = false;
break;
}
Expand Down Expand Up @@ -356,7 +365,7 @@ private ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
writer = serializers.get(clazz);
}
}

if (writer == null) {
if (Map.class.isAssignableFrom(clazz)) {
put(clazz, MapSerializer.instance);
Expand Down Expand Up @@ -396,8 +405,7 @@ private ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
put(clazz, ClobSeriliazer.instance);
} else if (TypeUtils.isPath(clazz)) {
put(clazz, ToStringSerializer.instance);
} else if (Iterable.class.isAssignableFrom(clazz) //
|| Iterator.class.isAssignableFrom(clazz)) {
} else if (Iterator.class.isAssignableFrom(clazz)) {
put(clazz, MiscCodec.instance);
} else {
String className = clazz.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

public abstract class SerializeFilterable {

protected List<BeforeFilter> beforeFilters = null;
Expand Down Expand Up @@ -197,10 +199,13 @@ protected Object processValue(JSONSerializer jsonBeanDeser, //
String key, //
Object propertyValue) {

if (propertyValue != null //
&& jsonBeanDeser.out.writeNonStringValueAsString) {
if (propertyValue instanceof Number || propertyValue instanceof Boolean) {
if (propertyValue != null) {
if (jsonBeanDeser.out.writeNonStringValueAsString //
&& (propertyValue instanceof Number || propertyValue instanceof Boolean)) {
propertyValue = propertyValue.toString();
} else if (beanContext != null && beanContext.isJsonDirect()) {
String jsonStr = (String) propertyValue;
propertyValue = JSON.parse(jsonStr);
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package com.alibaba.fastjson.serializer;

import static com.alibaba.fastjson.util.IOUtils.replaceChars;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.util.IOUtils;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -24,9 +26,7 @@
import java.nio.charset.Charset;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.util.IOUtils;
import static com.alibaba.fastjson.util.IOUtils.replaceChars;

/**
* @author wenshao[szujobs@hotmail.com]
Expand Down Expand Up @@ -1155,6 +1155,12 @@ public void writeFieldValue(char seperator, String name, char value) {
}

public void writeFieldValue(char seperator, String name, boolean value) {
if (!quoteFieldNames) {
write(seperator);
writeFieldName(name);
write(value);
return;
}
int intSize = value ? 4 : 5;

int nameLen = name.length();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class FieldInfo implements Comparable<FieldInfo> {
public final char[] name_chars;

public final boolean isEnum;
public final boolean jsonDirect;

public final String format;

Expand Down Expand Up @@ -81,6 +82,7 @@ public FieldInfo(String name, //
fieldAnnotation = null;
methodAnnotation = null;
this.getOnly = false;
this.jsonDirect = false;
this.format = null;
}

Expand Down Expand Up @@ -129,12 +131,16 @@ public FieldInfo(String name, //
String format = null;
JSONField annotation = getAnnotation();

boolean jsonDirect = false;
if (annotation != null) {
format = annotation.format();

if (format.trim().length() == 0) {
format = null;
}
jsonDirect = annotation.jsonDirect();
} else {
jsonDirect = false;
}
this.format = format;

Expand Down Expand Up @@ -169,6 +175,7 @@ public FieldInfo(String name, //
getOnly = Modifier.isFinal(field.getModifiers());
}
this.getOnly = getOnly;
this.jsonDirect = jsonDirect && fieldClass == String.class;

if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) fieldType;
Expand Down
Loading

0 comments on commit d024fcd

Please sign in to comment.