Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/alibaba/fastjson
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 10, 2015
2 parents 210a82b + 9687918 commit 64047cd
Show file tree
Hide file tree
Showing 22 changed files with 675 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>

<packaging>jar</packaging>
<name>fastjson</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public abstract class JSON implements JSONStreamAware, JSONAware {
int features = 0;
features |= SerializerFeature.QuoteFieldNames.getMask();
features |= SerializerFeature.SkipTransientField.getMask();
features |= SerializerFeature.WriteEnumUsingToString.getMask();
features |= SerializerFeature.WriteEnumUsingName.getMask();
features |= SerializerFeature.SortField.getMask();
// features |=
// com.alibaba.fastjson.serializer.SerializerFeature.WriteSlashAsSpecial.getMask();
Expand Down Expand Up @@ -737,5 +737,5 @@ private static void setFilter(JSONSerializer serializer, SerializeFilter filter)
}
}

public final static String VERSION = "1.2.4";
public final static String VERSION = "1.2.5";
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public final Object parseObject(final Map object, Object fieldName) {

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);

Expand Down Expand Up @@ -326,7 +326,7 @@ public final Object parseObject(final Map object, Object fieldName) {
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();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/alibaba/fastjson/parser/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ public enum Feature {
* @since 1.2.3
*
*/
OrderedField
OrderedField,

/**
* @since 1.2.5
*
*/
DisableSpecialKeyDetect
;

private Feature(){
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ private ParserConfig(ASMDeserializerFactory asmFactory, ClassLoader parentClassL

derializers.put(Object.class, JavaObjectDeserializer.instance);
derializers.put(String.class, StringCodec.instance);
derializers.put(StringBuffer.class, StringCodec.instance);
derializers.put(StringBuilder.class, StringCodec.instance);
derializers.put(char.class, CharacterCodec.instance);
derializers.put(Character.class, CharacterCodec.instance);
derializers.put(byte.class, NumberDeserializer.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static Map parseMap(DefaultJSONParser parser, Map<String, Object> map, Ty

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);

Expand Down Expand Up @@ -205,7 +205,8 @@ public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map,
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);
Expand Down Expand Up @@ -243,7 +244,8 @@ public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map,

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) {
Expand Down
85 changes: 78 additions & 7 deletions src/main/java/com/alibaba/fastjson/serializer/CalendarCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,104 @@
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.DateDeserializer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.util.IOUtils;

public class CalendarCodec implements ObjectSerializer, ObjectDeserializer {

public final static CalendarCodec instance = new CalendarCodec();

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
SerializeWriter out = serializer.getWriter();

if (object == null) {
out.writeNull();
return;
}

Calendar calendar = (Calendar) object;
Date date = calendar.getTime();
serializer.write(date);

if (serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)) {
if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
out.append('\'');
} else {
out.append('\"');
}

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millis = calendar.get(Calendar.MILLISECOND);

char[] buf;
if (millis != 0) {
buf = "0000-00-00T00:00:00.000".toCharArray();
IOUtils.getChars(millis, 23, buf);
IOUtils.getChars(second, 19, buf);
IOUtils.getChars(minute, 16, buf);
IOUtils.getChars(hour, 13, buf);
IOUtils.getChars(day, 10, buf);
IOUtils.getChars(month, 7, buf);
IOUtils.getChars(year, 4, buf);

} else {
if (second == 0 && minute == 0 && hour == 0) {
buf = "0000-00-00".toCharArray();
IOUtils.getChars(day, 10, buf);
IOUtils.getChars(month, 7, buf);
IOUtils.getChars(year, 4, buf);
} else {
buf = "0000-00-00T00:00:00".toCharArray();
IOUtils.getChars(second, 19, buf);
IOUtils.getChars(minute, 16, buf);
IOUtils.getChars(hour, 13, buf);
IOUtils.getChars(day, 10, buf);
IOUtils.getChars(month, 7, buf);
IOUtils.getChars(year, 4, buf);
}
}

out.write(buf);

int timeZone = calendar.getTimeZone().getRawOffset() / (3600 * 1000);
if (timeZone == 0) {
out.append("Z");
} else if (timeZone > 0) {
out.append("+").append(String.format("%02d", timeZone)).append(":00");
} else {
out.append("-").append(String.format("%02d", -timeZone)).append(":00");
}

if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
out.append('\'');
} else {
out.append('\"');
}
} else {
Date date = calendar.getTime();
serializer.write(date);
}
}

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
Object value = DateDeserializer.instance.deserialze(parser, type, fieldName);

if (value instanceof Calendar) {
return (T) value;
}

Date date = (Date) value;
if (date == null) {
return null;
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

return (T) calendar;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,17 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
IOUtils.getChars(year, 4, buf);
}
}

out.write(buf);

int timeZone = calendar.getTimeZone().getRawOffset()/(3600*1000);
if (timeZone == 0) {
out.append("Z");
} else if (timeZone > 0) {
out.append("+").append(String.format("%02d", timeZone)).append(":00");
} else {
out.append("-").append(String.format("%02d", -timeZone)).append(":00");
}

if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
out.append('\'');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
return;
}

if (serializer.isEnabled(SerializerFeature.WriteEnumUsingToString)) {
Enum<?> e = (Enum<?>) object;
Enum<?> e = (Enum<?>) object;
if(serializer.isEnabled(SerializerFeature.WriteEnumUsingName)){
serializer.write(e.name());
} else if (serializer.isEnabled(SerializerFeature.WriteEnumUsingToString)) {
serializer.write(e.toString());
} else {
Enum<?> e = (Enum<?>) object;
out.writeInt(e.ordinal());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.alibaba.fastjson.serializer;

import java.util.Collection;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.FieldInfo;

import java.util.Collection;

/**
* @author wenshao[szujobs@hotmail.com]
*/
Expand All @@ -31,6 +31,7 @@ public class ObjectFieldSerializer extends FieldSerializer {
boolean writeNullBooleanAsFalse = false;
boolean writeNullListAsEmpty = false;
boolean writeEnumUsingToString = false;
boolean writeEnumUsingName = false;

private RuntimeSerializerInfo runtimeInfo;

Expand All @@ -57,6 +58,8 @@ public ObjectFieldSerializer(FieldInfo fieldInfo){
writeNullListAsEmpty = true;
} else if (feature == SerializerFeature.WriteEnumUsingToString) {
writeEnumUsingToString = true;
}else if(feature == SerializerFeature.WriteEnumUsingName){
writeEnumUsingName = true;
}
}
}
Expand Down Expand Up @@ -110,9 +113,15 @@ public void writeValue(JSONSerializer serializer, Object propertyValue) throws E
return;
}

if (writeEnumUsingToString == true && runtimeInfo.runtimeFieldClass.isEnum()) {
serializer.getWriter().writeString(((Enum<?>) propertyValue).name());
return;
if(runtimeInfo.runtimeFieldClass.isEnum()){
if(writeEnumUsingName){
serializer.getWriter().writeString(((Enum<?>) propertyValue).name());
return;
}
if(writeEnumUsingToString){
serializer.getWriter().writeString(((Enum<?>) propertyValue).toString());
return;
}
}

Class<?> valueClass = propertyValue.getClass();
Expand Down
60 changes: 39 additions & 21 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public SerializeWriter(Writer writer, int initialSize){
public void config(SerializerFeature feature, boolean state) {
if (state) {
features |= feature.getMask();
//由于枚举序列化特性WriteEnumUsingToString和WriteEnumUsingName不能共存,需要检查
if(feature == SerializerFeature.WriteEnumUsingToString){
features &= ~SerializerFeature.WriteEnumUsingName.getMask();
}else if(feature == SerializerFeature.WriteEnumUsingName){
features &= ~SerializerFeature.WriteEnumUsingToString.getMask();
}
} else {
features &= ~feature.getMask();
}
Expand Down Expand Up @@ -499,25 +505,34 @@ public void writeEnum(Enum<?> value, char c) {
return;
}

if (isEnabled(SerializerFeature.WriteEnumUsingToString)) {
if (isEnabled(SerializerFeature.UseSingleQuotes)) {
write('\'');
write(value.name());
write('\'');
write(c);
if (isEnabled(SerializerFeature.WriteEnumUsingName)) {
writeEnumValue(value.name(),c);
return;
}

} else {
write('\"');
write(value.name());
write('\"');
write(c);
}
if (isEnabled(SerializerFeature.WriteEnumUsingToString)) {
writeEnumValue(value.toString(),c);
return;
}

writeIntAndChar(value.ordinal(), c);
}

private void writeEnumValue(String value,char c){
if (isEnabled(SerializerFeature.UseSingleQuotes)) {
write('\'');
write(value);
write('\'');
write(c);

} else {
write('\"');
write(value);
write('\"');
write(c);
}
}

public void writeIntAndChar(int i, char c) {
if (i == Integer.MIN_VALUE) {
write("-2147483648");
Expand Down Expand Up @@ -1423,20 +1438,23 @@ public void writeFieldValue(char seperator, String name, Enum<?> value) {
return;
}

if (isEnabled(SerializerFeature.WriteEnumUsingToString)) {
if (isEnabled(SerializerFeature.UseSingleQuotes)) {
writeFieldValue(seperator, name, value.name());
} else {
writeFieldValueStringWithDoubleQuote(seperator, name, value.name(), false);
return;
}

// writeStringWithDoubleQuote
if (isEnabled(SerializerFeature.WriteEnumUsingName)) {
writeEnumFieldValue(seperator,name,value.name());
}else if(isEnabled(SerializerFeature.WriteEnumUsingToString)){
writeEnumFieldValue(seperator,name,value.toString());
} else {
writeFieldValue(seperator, name, value.ordinal());
}
}

private void writeEnumFieldValue(char seperator,String name,String value){
if (isEnabled(SerializerFeature.UseSingleQuotes)) {
writeFieldValue(seperator, name, value);
} else {
writeFieldValueStringWithDoubleQuote(seperator, name, value, false);
}
}

public void writeFieldValue(char seperator, String name, BigDecimal value) {
write(seperator);
writeFieldName(name);
Expand Down
Loading

0 comments on commit 64047cd

Please sign in to comment.