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

Commit

Permalink
improved kotlin support, fix #3274
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 25, 2020
1 parent 974b413 commit 2143fdb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1545,12 +1545,16 @@ public Object createInstance(Map<String, Object> map, ParserConfig config) //
boolean hasNull = false;
if (beanInfo.kotlin) {
for (int i = 0; i < params.length; i++) {
if (params[i] == null && beanInfo.fields != null && i < beanInfo.fields.length) {
FieldInfo fieldInfo = beanInfo.fields[i];
if (fieldInfo.fieldClass == String.class) {
hasNull = true;
Object param = params[i];
if (param == null) {
if (beanInfo.fields != null && i < beanInfo.fields.length) {
FieldInfo fieldInfo = beanInfo.fields[i];
if (fieldInfo.fieldClass == String.class) {
hasNull = true;
}
}
break;
} else if (param.getClass() != beanInfo.fields[i].fieldClass){
params[i] = TypeUtils.cast(param, beanInfo.fields[i].fieldClass, config);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3200/Issue3274.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.alibaba.json.bvt.issue_3200

import com.alibaba.fastjson.JSON
import junit.framework.Assert.assertEquals
import org.junit.Test
import java.util.*

class TestFJ {

@Test
fun test() {
val str = """
{"data": {"id": "1", "name":"n1"}}
""".trimIndent()


val d1 = JSON.parseObject(str, Data2::class.java)

val data = JSON.parseObject(str)
val d2 = data.getObject("data", Data::class.java)

assertEquals(1, d1.data.id)
assertEquals(1, d2.id)
}
}

data class Data(
val id: Int = 0,
val name: String = "",
val date: Date? = null
)
data class Data2(
val data: Data
)

0 comments on commit 2143fdb

Please sign in to comment.