Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for NamingPolicy, for issue #1555
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2018
1 parent 4a32a0e commit 3870aa9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
}
}

boolean fieldAnnotationExists = false;
if (annotation != null) {
if (!annotation.serialize()) {
continue;
Expand All @@ -1105,6 +1106,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //

if (annotation.name().length() != 0) {
String propertyName = annotation.name();
fieldAnnotationExists = true;

if (aliasMap != null) {
propertyName = aliasMap.get(propertyName);
Expand Down Expand Up @@ -1165,6 +1167,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //

if (fieldAnnotation.name().length() != 0) {
propertyName = fieldAnnotation.name();
fieldAnnotationExists = true;

if (aliasMap != null) {
propertyName = aliasMap.get(propertyName);
Expand All @@ -1176,7 +1179,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
}
}

if (propertyNamingStrategy != null) {
if (propertyNamingStrategy != null && !fieldAnnotationExists) {
propertyName = propertyNamingStrategy.translate(propertyName);
}

Expand Down
94 changes: 94 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1500/Issue1555.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.alibaba.json.bvt.issue_1500;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.StringCodec;
import junit.framework.TestCase;

import java.io.IOException;
import java.lang.reflect.Type;

public class Issue1555 extends TestCase {
public void test_for_issue() throws Exception {
Model model = new Model();
model.userId = 1001;
model.userName = "test";
String text = JSON.toJSONString(model);
assertEquals("{\"userName\":\"test\",\"user_id\":1001}", text);

Model model2 = JSON.parseObject(text, Model.class);

assertEquals(1001, model2.userId);
assertEquals("test", model2.userName);
}

/**
* 当某个字段有JSONField注解,JSONField中name属性不存在,json属性名也要用类上的属性名转换策略
* @throws Exception
*/
public void test_when_JSONField_have_not_name_attr() throws Exception {
ModelTwo modelTwo = new ModelTwo();
modelTwo.userId = 1001;
modelTwo.userName = "test";
String text = JSON.toJSONString(modelTwo);
assertEquals("{\"userName\":\"test\",\"user_id\":1001}", text);

Model model2 = JSON.parseObject(text, Model.class);

assertEquals(1001, model2.userId);
assertEquals("test", model2.userName);
}

@JSONType(naming = PropertyNamingStrategy.SnakeCase)
public static class Model {
private int userId;
@JSONField(name = "userName")
private String userName;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

@JSONType(naming = PropertyNamingStrategy.SnakeCase)
public static class ModelTwo {
/**
* 此字段准备序列化为字符串类型
*/
private int userId;
@JSONField(name = "userName")
private String userName;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}
}

0 comments on commit 3870aa9

Please sign in to comment.