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.
- Loading branch information
Showing
9 changed files
with
297 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
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
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
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
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
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
97 changes: 97 additions & 0 deletions
97
src/test/java/com/alibaba/json/bvt/parser/array/BeanToArrayAutoTypeTest.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,97 @@ | ||
package com.alibaba.json.bvt.parser.array; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.annotation.JSONField; | ||
import com.alibaba.fastjson.annotation.JSONType; | ||
import com.alibaba.fastjson.parser.Feature; | ||
import com.alibaba.fastjson.serializer.SerializerFeature; | ||
import junit.framework.TestCase; | ||
|
||
public class BeanToArrayAutoTypeTest extends TestCase { | ||
public void test_for_issue_x() throws Exception { | ||
String json = "[\"@type\":\"B\",\"chengchao\",1001]"; | ||
A a = JSON.parseObject(json, A.class, Feature.SupportAutoType, Feature.SupportArrayToBean); | ||
B b = (B) a; | ||
} | ||
|
||
public void test_for_issue() throws Exception { | ||
Model m = new Model(); | ||
m.value = new B(1001, "chengchao"); | ||
String json = JSON.toJSONString(m); | ||
assertEquals("{\"value\":[\"@type\":\"B\",\"chengchao\",1001]}", json); | ||
|
||
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportAutoType); | ||
assertEquals(m.value.getClass(), m1.value.getClass()); | ||
assertEquals(json, JSON.toJSONString(m1)); | ||
} | ||
|
||
public void test_for_issue_1() throws Exception { | ||
Model m = new Model(); | ||
m.value = new C(1001, 58); | ||
String json = JSON.toJSONString(m); | ||
assertEquals("{\"value\":[\"@type\":\"C\",58,1001]}", json); | ||
|
||
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportAutoType); | ||
assertEquals(m.value.getClass(), m1.value.getClass()); | ||
assertEquals(json, JSON.toJSONString(m1)); | ||
} | ||
|
||
@JSONType(seeAlso = {B.class, C.class}) | ||
public static class A { | ||
protected int id; | ||
|
||
public int getId() | ||
{ | ||
return id; | ||
} | ||
|
||
public void setId(int id) | ||
{ | ||
this.id = id; | ||
} | ||
} | ||
|
||
@JSONType(typeName = "B", orders = {"name", "id"}) | ||
public static class B extends A { | ||
private String name; | ||
|
||
public B() { | ||
|
||
} | ||
|
||
public B(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
public void setName(String name) | ||
{ | ||
this.name = name; | ||
} | ||
} | ||
|
||
@JSONType(typeName = "C", orders = {"age", "id"}) | ||
public static class C extends A { | ||
public int age; | ||
|
||
public C() { | ||
|
||
} | ||
|
||
public C(int id, int age) { | ||
this.id = id; | ||
this.age = age; | ||
} | ||
} | ||
|
||
public static class Model { | ||
@JSONField(serialzeFeatures = {SerializerFeature.BeanToArray, SerializerFeature.WriteClassName} | ||
, parseFeatures = Feature.SupportArrayToBean) | ||
public A value; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/com/alibaba/json/bvt/parser/array/BeanToArrayAutoTypeTest2.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,77 @@ | ||
package com.alibaba.json.bvt.parser.array; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.annotation.JSONField; | ||
import com.alibaba.fastjson.annotation.JSONType; | ||
import com.alibaba.fastjson.parser.Feature; | ||
import com.alibaba.fastjson.parser.ParserConfig; | ||
import com.alibaba.fastjson.serializer.SerializerFeature; | ||
import junit.framework.TestCase; | ||
|
||
public class BeanToArrayAutoTypeTest2 | ||
extends TestCase { | ||
public void test_for_issue() throws Exception { | ||
Model m = new Model(); | ||
m.value = new B(1001, "chengchao"); | ||
String json = JSON.toJSONString(m); | ||
assertEquals("{\"value\":[\"@type\":\"B\",\"chengchao\",1001]}", json); | ||
|
||
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportAutoType); | ||
assertEquals(m.value.getClass(), m1.value.getClass()); | ||
assertEquals(json, JSON.toJSONString(m1)); | ||
} | ||
|
||
public void test_for_issue_1() throws Exception { | ||
Model m = new Model(); | ||
m.value = new C(1001, 58); | ||
String json = JSON.toJSONString(m); | ||
assertEquals("{\"value\":[\"@type\":\"C\",58,1001]}", json); | ||
|
||
Model m1 = JSON.parseObject(json, Model.class, Feature.SupportAutoType); | ||
assertEquals(m.value.getClass(), m1.value.getClass()); | ||
assertEquals(json, JSON.toJSONString(m1)); | ||
} | ||
|
||
@JSONType(seeAlso = {B.class, C.class} | ||
, serialzeFeatures = {SerializerFeature.BeanToArray, SerializerFeature.WriteClassName} | ||
, parseFeatures = Feature.SupportArrayToBean) | ||
public static class A { | ||
public int id; | ||
} | ||
|
||
@JSONType(typeName = "B", orders = {"name", "id"} | ||
, serialzeFeatures = {SerializerFeature.BeanToArray, SerializerFeature.WriteClassName} | ||
, parseFeatures = Feature.SupportArrayToBean) | ||
public static class B extends A { | ||
public String name; | ||
|
||
public B() { | ||
|
||
} | ||
|
||
public B(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
|
||
@JSONType(typeName = "C", orders = {"age", "id"} | ||
, serialzeFeatures = {SerializerFeature.BeanToArray, SerializerFeature.WriteClassName} | ||
, parseFeatures = Feature.SupportArrayToBean) | ||
public static class C extends A { | ||
public int age; | ||
|
||
public C() { | ||
|
||
} | ||
|
||
public C(int id, int age) { | ||
this.id = id; | ||
this.age = age; | ||
} | ||
} | ||
|
||
public static class Model { | ||
public A value; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/alibaba/json/bvt/parser/array/BeanToArrayAutoTypeTest3.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,41 @@ | ||
package com.alibaba.json.bvt.parser.array; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.annotation.JSONType; | ||
import com.alibaba.fastjson.parser.Feature; | ||
import junit.framework.TestCase; | ||
|
||
import java.util.List; | ||
|
||
public class BeanToArrayAutoTypeTest3 | ||
extends TestCase { | ||
public void test_beanToArray() throws Exception { | ||
Topology topology = JSON.parseObject("{\"maps\":[[\"@type\":\"Log\"]]}", Topology.class); | ||
assertEquals(LogSourceMeta.class, topology.maps.get(0).getClass()); | ||
} | ||
|
||
public void test_beanToArray1() throws Exception { | ||
Topology topology = JSON.parseObject("{\"maps\":[[\"@type\":\"Log\",123]]}", Topology.class); | ||
assertEquals(LogSourceMeta.class, topology.maps.get(0).getClass()); | ||
assertEquals(123, ((LogSourceMeta) topology.maps.get(0)).id); | ||
} | ||
|
||
@JSONType(typeName = "Log") | ||
public static class LogSourceMeta extends MapTaskMeta { | ||
public int id; | ||
} | ||
|
||
@JSONType(seeAlso = {LogSourceMeta.class, OtherMeta.class}, parseFeatures = Feature.SupportArrayToBean) | ||
public static class MapTaskMeta { | ||
|
||
} | ||
|
||
@JSONType(typeName = "Other") | ||
public static class OtherMeta extends MapTaskMeta { | ||
|
||
} | ||
|
||
public static class Topology { | ||
public List<MapTaskMeta> maps; | ||
} | ||
} |