Skip to content

Commit af171c4

Browse files
authored
Merge pull request #205 from xurui1995/3.4.0_fix195
3.4.0 fix195
2 parents 2cbceb6 + dded4c7 commit af171c4

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

src/main/kotlin/wu/seal/jsontokotlin/KotlinCodeMaker.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,18 @@ class KotlinCodeMaker {
163163
jsonElementValue.isJsonArray -> {
164164
jsonElementValue.asJsonArray.run {
165165
var type = getArrayType(property, this)
166-
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
167-
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
168-
) {
169-
val subCode = try {
170-
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
171-
} catch (e: UnSupportJsonException) {
172-
type = e.adviceType
173-
""
166+
if(!allChildrenAreEmptyArray()) {
167+
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
168+
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
169+
) {
170+
val subCode = try {
171+
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
172+
} catch (e: UnSupportJsonException) {
173+
type = e.adviceType
174+
""
175+
}
176+
toBeAppend.add(subCode)
174177
}
175-
toBeAppend.add(subCode)
176178
}
177179
addProperty(stringBuilder, property, type, "", isLast)
178180
}

src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ tailrec fun JsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive():
121121
return get(0).asJsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
122122
}
123123

124+
fun JsonArray.allChildrenAreEmptyArray(): Boolean {
125+
126+
if (size() == 0) {
127+
return true
128+
}
129+
130+
return all { (it as? JsonArray)?.allChildrenAreEmptyArray() ?: false }
131+
}
132+
124133

125134
/**
126135
* filter out all null json element of JsonArray
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package wu.seal.jsontokotlin.regression
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Before
5+
import org.junit.Test
6+
import wu.seal.jsontokotlin.KotlinDataClassCodeMaker
7+
import wu.seal.jsontokotlin.test.TestConfig
8+
9+
class Issue195Test {
10+
11+
private val expected = """data class A(
12+
@SerializedName("xxx")
13+
val xxx: List<List<Any>> = listOf()
14+
)"""
15+
16+
/**
17+
* init test environment before test
18+
*/
19+
@Before
20+
fun setUp() {
21+
TestConfig.setToTestInitState()
22+
}
23+
24+
/**
25+
* test issue #130 of Github Project issue
26+
*/
27+
@Test
28+
fun testIssue195() {
29+
val generated = KotlinDataClassCodeMaker("A", "{\"xxx\":[[]]}").makeKotlinDataClassCode()
30+
generated.trim().should.be.equal(expected)
31+
}
32+
}

src/test/kotlin/wu/seal/jsontokotlin/utils/ExtensionsKtTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,32 @@ class ExtensionsKtTest {
177177
s.containsAnyOf(listOf("is", "true")).should.be.`true`
178178
s.containsAnyOf(listOf("bad", "test")).should.be.`false`
179179
}
180+
181+
@Test
182+
fun allChildrenAreEmptyArray() {
183+
val emptyArray1 = """[
184+
[]
185+
]
186+
"""
187+
188+
val emptyArray2 = """[
189+
[[]]
190+
]
191+
"""
192+
193+
val emptyArray3 = """[
194+
[],[]
195+
]
196+
"""
197+
198+
val nonEmptyArray4 = """[
199+
[{"name":"tom"}],[]
200+
]
201+
"""
202+
203+
gson.fromJson<JsonArray>(emptyArray1, JsonArray::class.java).allChildrenAreEmptyArray().should.be.`true`
204+
gson.fromJson<JsonArray>(emptyArray2, JsonArray::class.java).allChildrenAreEmptyArray().should.be.`true`
205+
gson.fromJson<JsonArray>(emptyArray3, JsonArray::class.java).allChildrenAreEmptyArray().should.be.`true`
206+
gson.fromJson<JsonArray>(nonEmptyArray4, JsonArray::class.java).allChildrenAreEmptyArray().should.be.`false`
207+
}
180208
}

0 commit comments

Comments
 (0)