mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-29 06:06:50 +08:00
JSONType annotation support config mappingTo
This commit is contained in:
@@ -5,14 +5,13 @@ import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.annotation.JSONType;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
|
||||
|
||||
public class AbstractSerializeTest extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
@@ -22,9 +21,6 @@ public class AbstractSerializeTest extends TestCase {
|
||||
public void test_mapping_0() throws Exception {
|
||||
String text = "{\"@type\":\"com.alibaba.json.bvt.parser.deser.AbstractSerializeTest$A\"}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
B b = (B) JSON.parse(text);
|
||||
Assert.assertNotNull(b);
|
||||
}
|
||||
@@ -32,9 +28,6 @@ public class AbstractSerializeTest extends TestCase {
|
||||
public void test_mapping_1() throws Exception {
|
||||
String text = "{\"@type\":\"com.alibaba.json.bvt.parser.deser.AbstractSerializeTest$A\",\"id\":123}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
B b = (B) JSON.parse(text);
|
||||
Assert.assertNotNull(b);
|
||||
Assert.assertEquals(123, b.getId());
|
||||
@@ -55,9 +48,6 @@ public class AbstractSerializeTest extends TestCase {
|
||||
public void test_mapping_group() throws Exception {
|
||||
String text = "{\"a\":{\"id\":234,\"name\":\"abc\"}}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
G g = JSON.parseObject(text, G.class);
|
||||
Assert.assertTrue(g.getA() instanceof B);
|
||||
}
|
||||
@@ -76,6 +66,7 @@ public class AbstractSerializeTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
@JSONType(mappingTo = B.class)
|
||||
public static abstract class A {
|
||||
|
||||
private int id;
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.alibaba.json.bvt.parser.deser;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
|
||||
|
||||
public class AbstractSerializeTest2 extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, null);
|
||||
}
|
||||
|
||||
public void test_mapping_0() throws Exception {
|
||||
String text = "{\"@type\":\"com.alibaba.json.bvt.parser.deser.AbstractSerializeTest$A\"}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
B b = (B) JSON.parse(text);
|
||||
Assert.assertNotNull(b);
|
||||
}
|
||||
|
||||
public void test_mapping_1() throws Exception {
|
||||
String text = "{\"@type\":\"com.alibaba.json.bvt.parser.deser.AbstractSerializeTest$A\",\"id\":123}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
B b = (B) JSON.parse(text);
|
||||
Assert.assertNotNull(b);
|
||||
Assert.assertEquals(123, b.getId());
|
||||
}
|
||||
|
||||
public void test_mapping_2() throws Exception {
|
||||
String text = "{\"@type\":\"com.alibaba.json.bvt.parser.deser.AbstractSerializeTest$A\",\"id\":234,\"name\":\"abc\"}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
B b = (B) JSON.parse(text);
|
||||
Assert.assertNotNull(b);
|
||||
Assert.assertEquals(234, b.getId());
|
||||
Assert.assertEquals("abc", b.getName());
|
||||
}
|
||||
|
||||
public void test_mapping_group() throws Exception {
|
||||
String text = "{\"a\":{\"id\":234,\"name\":\"abc\"}}";
|
||||
|
||||
ObjectDeserializer serializerB = ParserConfig.getGlobalInstance().getDeserializer(B.class);
|
||||
ParserConfig.getGlobalInstance().putDeserializer(A.class, serializerB);
|
||||
|
||||
G g = JSON.parseObject(text, G.class);
|
||||
Assert.assertTrue(g.getA() instanceof B);
|
||||
}
|
||||
|
||||
public static class G {
|
||||
|
||||
private A a;
|
||||
|
||||
public A getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(A a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static abstract class A {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static class B extends A {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user