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.
improved support kotlin data class. #1374
- Loading branch information
Showing
12 changed files
with
243 additions
and
8 deletions.
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
38 changes: 38 additions & 0 deletions
38
src/test/java/com/alibaba/json/bvt/kotlin/ClassWithPairMixedTypesTest.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,38 @@ | ||
package com.alibaba.json.bvt.kotlin; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by wenshao on 06/08/2017. | ||
*/ | ||
public class ClassWithPairMixedTypesTest extends TestCase { | ||
public void test_user() throws Exception { | ||
ExtClassLoader classLoader = new ExtClassLoader(); | ||
Class clazz = classLoader.loadClass("ClassWithPairMixedTypes"); | ||
|
||
String json = "{\"person\":{\"first\":\"wenshao\",\"second\":99}}"; | ||
Object obj = JSON.parseObject(json, clazz); | ||
assertEquals("{\"person\":{\"first\":\"wenshao\",\"second\":99}}", JSON.toJSONString(obj)); | ||
} | ||
|
||
public static class ExtClassLoader extends ClassLoader { | ||
|
||
public ExtClassLoader() throws IOException { | ||
super(Thread.currentThread().getContextClassLoader()); | ||
|
||
{ | ||
byte[] bytes; | ||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ClassWithPairMixedTypes.clazz"); | ||
bytes = IOUtils.toByteArray(is); | ||
is.close(); | ||
|
||
super.defineClass("ClassWithPairMixedTypes", bytes, 0, bytes.length); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/alibaba/json/bvt/kotlin/ClassWithPairTest.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,38 @@ | ||
package com.alibaba.json.bvt.kotlin; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by wenshao on 06/08/2017. | ||
*/ | ||
public class ClassWithPairTest extends TestCase { | ||
public void test_user() throws Exception { | ||
ExtClassLoader classLoader = new ExtClassLoader(); | ||
Class clazz = classLoader.loadClass("ClassWithPair"); | ||
|
||
String json = "{\"name\":{\"first\":\"shaojin\",\"second\":\"wen\"},\"age\":99}"; | ||
Object obj = JSON.parseObject(json, clazz); | ||
assertEquals("{\"age\":99,\"name\":{\"first\":\"shaojin\",\"second\":\"wen\"}}", JSON.toJSONString(obj)); | ||
} | ||
|
||
public static class ExtClassLoader extends ClassLoader { | ||
|
||
public ExtClassLoader() throws IOException { | ||
super(Thread.currentThread().getContextClassLoader()); | ||
|
||
{ | ||
byte[] bytes; | ||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ClassWithPair.clazz"); | ||
bytes = IOUtils.toByteArray(is); | ||
is.close(); | ||
|
||
super.defineClass("ClassWithPair", bytes, 0, bytes.length); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/alibaba/json/bvt/kotlin/ClassWithRangesTest.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,38 @@ | ||
package com.alibaba.json.bvt.kotlin; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by wenshao on 06/08/2017. | ||
*/ | ||
public class ClassWithRangesTest extends TestCase { | ||
public void test_user() throws Exception { | ||
ExtClassLoader classLoader = new ExtClassLoader(); | ||
Class clazz = classLoader.loadClass("ClassWithRanges"); | ||
|
||
String json = "{\"ages\":{\"start\":18,\"end\":40},\"distance\":{\"start\":5,\"end\":50}}"; | ||
Object obj = JSON.parseObject(json, clazz); | ||
assertEquals("{\"ages\":{\"first\":18,\"last\":0,\"start\":18,\"step\":1},\"distance\":{\"first\":5,\"last\":0,\"start\":5,\"step\":1}}", JSON.toJSONString(obj)); | ||
} | ||
|
||
public static class ExtClassLoader extends ClassLoader { | ||
|
||
public ExtClassLoader() throws IOException { | ||
super(Thread.currentThread().getContextClassLoader()); | ||
|
||
{ | ||
byte[] bytes; | ||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ClassWithRanges.clazz"); | ||
bytes = IOUtils.toByteArray(is); | ||
is.close(); | ||
|
||
super.defineClass("ClassWithRanges", bytes, 0, bytes.length); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/alibaba/json/bvt/kotlin/ClassWithTripleTest.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,38 @@ | ||
package com.alibaba.json.bvt.kotlin; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by wenshao on 06/08/2017. | ||
*/ | ||
public class ClassWithTripleTest extends TestCase { | ||
public void test_user() throws Exception { | ||
ExtClassLoader classLoader = new ExtClassLoader(); | ||
Class clazz = classLoader.loadClass("ClassWithTriple"); | ||
|
||
String json = "{\"name\":{\"first\":\"wen\",\"second\":\"shaojin\",\"third\":99}}"; | ||
Object obj = JSON.parseObject(json, clazz); | ||
assertEquals("{\"age\":0,\"name\":{\"first\":\"wen\",\"second\":\"shaojin\",\"third\":\"99\"}}", JSON.toJSONString(obj)); | ||
} | ||
|
||
public static class ExtClassLoader extends ClassLoader { | ||
|
||
public ExtClassLoader() throws IOException { | ||
super(Thread.currentThread().getContextClassLoader()); | ||
|
||
{ | ||
byte[] bytes; | ||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ClassWithTriple.clazz"); | ||
bytes = IOUtils.toByteArray(is); | ||
is.close(); | ||
|
||
super.defineClass("ClassWithTriple", bytes, 0, bytes.length); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/alibaba/json/bvt/kotlin/Class_WithPrimaryAndSecondaryConstructorTest.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,38 @@ | ||
package com.alibaba.json.bvt.kotlin; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by wenshao on 06/08/2017. | ||
*/ | ||
public class Class_WithPrimaryAndSecondaryConstructorTest extends TestCase { | ||
public void test_user() throws Exception { | ||
ExtClassLoader classLoader = new ExtClassLoader(); | ||
Class clazz = classLoader.loadClass("Class_WithPrimaryAndSecondaryConstructor"); | ||
|
||
String json = "{\"name\":\"John Smith\",\"age\":30}"; | ||
Object obj = JSON.parseObject(json, clazz); | ||
assertEquals("{\"age\":30,\"name\":\"John Smith\"}", JSON.toJSONString(obj)); | ||
} | ||
|
||
public static class ExtClassLoader extends ClassLoader { | ||
|
||
public ExtClassLoader() throws IOException { | ||
super(Thread.currentThread().getContextClassLoader()); | ||
|
||
{ | ||
byte[] bytes; | ||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/Class_WithPrimaryAndSecondaryConstructor.clazz"); | ||
bytes = IOUtils.toByteArray(is); | ||
is.close(); | ||
|
||
super.defineClass("Class_WithPrimaryAndSecondaryConstructor", bytes, 0, bytes.length); | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.72 KB
src/test/resources/kotlin/Class_WithPrimaryAndSecondaryConstructor.clazz
Binary file not shown.