This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug fixed for NamingPolicy, for issue #1555
- Loading branch information
Showing
2 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/com/alibaba/json/bvt/issue_1500/Issue1555.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |