Skip to content

Commit

Permalink
add jsonDirect config support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 20, 2016
1 parent aaaabe8 commit 07038dd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/alibaba/fastjson/annotation/JSONField.java
Expand Up @@ -49,4 +49,9 @@
Feature[] parseFeatures() default {}; Feature[] parseFeatures() default {};


String label() default ""; String label() default "";

/**
* @since 1.2.12
*/
boolean jsonDirect() default false;
} }
Expand Up @@ -54,6 +54,10 @@ public Type getFieldType() {
public int getFeatures() { public int getFeatures() {
return fieldInfo.serialzeFeatures; return fieldInfo.serialzeFeatures;
} }

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


public <T extends Annotation> T getAnnation(Class<T> annotationClass) { public <T extends Annotation> T getAnnation(Class<T> annotationClass) {
return fieldInfo.getAnnation(annotationClass); return fieldInfo.getAnnation(annotationClass);
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java
Expand Up @@ -19,7 +19,6 @@
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; 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.Jdk8DateCodec;
import com.alibaba.fastjson.parser.deserializer.OptionalCodec; import com.alibaba.fastjson.parser.deserializer.OptionalCodec;
import com.alibaba.fastjson.util.ASMUtils; import com.alibaba.fastjson.util.ASMUtils;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.IdentityHashMap; import com.alibaba.fastjson.util.IdentityHashMap;
import com.alibaba.fastjson.util.ServiceLoader; import com.alibaba.fastjson.util.ServiceLoader;
import com.alibaba.fastjson.util.TypeUtils; import com.alibaba.fastjson.util.TypeUtils;
Expand Down Expand Up @@ -138,11 +138,16 @@ public ObjectSerializer createJavaBeanSerializer(SerializeBeanInfo beanInfo) {
} }


if (asm) { if (asm) {
for(Field field : clazz.getDeclaredFields()){ for(FieldInfo field : beanInfo.fields){
JSONField annotation = field.getAnnotation(JSONField.class); JSONField annotation = field.getAnnotation();
if (annotation != null //
&& ((!ASMUtils.checkName(annotation.name())) // if (annotation == null) {
|| annotation.format().length() != 0)) { continue;
}
if ((!ASMUtils.checkName(annotation.name())) //
|| annotation.format().length() != 0
|| annotation.jsonDirect()
) {
asm = false; asm = false;
break; break;
} }
Expand Down
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


import com.alibaba.fastjson.JSON;

public abstract class SerializeFilterable { public abstract class SerializeFilterable {


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


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


Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Expand Up @@ -39,6 +39,7 @@ public class FieldInfo implements Comparable<FieldInfo> {
public final char[] name_chars; public final char[] name_chars;


public final boolean isEnum; public final boolean isEnum;
public final boolean jsonDirect;


public final String format; public final String format;


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


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


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


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


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


if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) { if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) fieldType; TypeVariable<?> tv = (TypeVariable<?>) fieldType;
Expand Down
@@ -0,0 +1,26 @@
package com.alibaba.json.bvt.serializer.features;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

import junit.framework.TestCase;

public class JSONDirectTest extends TestCase {
public void test_feature() throws Exception {
Model model = new Model();
model.id = 1001;
model.value = "{}";

String json = JSON.toJSONString(model);
System.out.println(json);
Assert.assertEquals("{\"id\":1001,\"value\":{}}", json);
}

public static class Model {
public int id;
@JSONField(jsonDirect=true)
public String value;
}
}
@@ -0,0 +1,26 @@
package com.alibaba.json.bvt.serializer.features;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

import junit.framework.TestCase;

public class JSONDirectTest_private extends TestCase {
public void test_feature() throws Exception {
Model model = new Model();
model.id = 1001;
model.value = "{}";

String json = JSON.toJSONString(model);
System.out.println(json);
Assert.assertEquals("{\"id\":1001,\"value\":{}}", json);
}

private static class Model {
public int id;
@JSONField(jsonDirect=true)
public String value;
}
}

0 comments on commit 07038dd

Please sign in to comment.