Skip to content

Commit 43c883c

Browse files
committed
Fix bug #87 again
1 parent a29bed5 commit 43c883c

File tree

5 files changed

+174
-22
lines changed

5 files changed

+174
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class KotlinCodeMaker {
137137
var type = getArrayType(property, jsonElementValue.asJsonArray)
138138

139139
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray) || jsonElementValue.asJsonArray.onlyHasOneObjectElementRecursive()
140-
|| jsonElementValue.asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()) {
140+
|| jsonElementValue.asJsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()) {
141141

142142
val subCode = try {
143143
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package wu.seal.jsontokotlin
22

33
import com.google.gson.*
4+
import wu.seal.jsontokotlin.utils.filterOutNullElement
45
import wu.seal.jsontokotlin.utils.onlyHasOneElementRecursive
5-
import wu.seal.jsontokotlin.utils.onlyOneSubArrayContainsElementAndAllObjectRecursive
6+
import wu.seal.jsontokotlin.utils.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive
67

78
/**
89
* This class aim at filtering out the expected Json Element to be convert from Json array
@@ -21,19 +22,20 @@ class TargetJsonElement : ITargetJsonElement {
2122

2223
override fun getTargetJsonElementForGeneratingCode(): JsonElement {
2324
if (this.jsonElement.isJsonArray) {
24-
if (jsonElement.asJsonArray.size() == 0) {
25+
val jsonArrayWithoutNullElement = jsonElement.asJsonArray.filterOutNullElement()
26+
if (jsonArrayWithoutNullElement.size() == 0) {
2527
return gson.toJsonTree(Any())
26-
} else if (allElementAreObject(jsonElement.asJsonArray)) {
27-
val jsonElementNotArray = getArrayChildElement(this.jsonElement.asJsonArray)
28+
} else if (allElementAreObject(jsonArrayWithoutNullElement)) {
29+
val jsonElementNotArray = getArrayChildElement(jsonArrayWithoutNullElement)
2830
if (jsonElementNotArray.isJsonObject) {
2931
return jsonElementNotArray
3032
} else {
3133
throw IllegalStateException("Unbelievable! should not throw out this exception")
3234
}
33-
} else if (jsonElement.asJsonArray.onlyHasOneElementRecursive() || jsonElement.asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()) {
34-
return getArrayChildElement(this.jsonElement.asJsonArray)
35-
} else if (allElementAreSamePrimitiveType(jsonElement.asJsonArray)) {
36-
return jsonElement.asJsonArray[0]
35+
} else if (jsonArrayWithoutNullElement.onlyHasOneElementRecursive() || jsonArrayWithoutNullElement.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()) {
36+
return getArrayChildElement(jsonArrayWithoutNullElement)
37+
} else if (allElementAreSamePrimitiveType(jsonArrayWithoutNullElement)) {
38+
return jsonArrayWithoutNullElement[0]
3739
} else {
3840
return gson.toJsonTree(Any())
3941
}
@@ -60,16 +62,22 @@ class TargetJsonElement : ITargetJsonElement {
6062

6163
private fun allElementAreSamePrimitiveType(jsonArray: JsonArray): Boolean {
6264
var allElementAreSamePrimitiveType = true
63-
jsonArray.forEach {
64-
if (it.isJsonPrimitive.not()) {
65-
allElementAreSamePrimitiveType = false
66-
return@forEach
67-
}
68-
if (theSamePrimitiveType(jsonArray[0].asJsonPrimitive, it.asJsonPrimitive).not()) {
69-
allElementAreSamePrimitiveType = false
70-
return@forEach
65+
66+
run loop@{
67+
68+
jsonArray.forEach {
69+
if (it.isJsonPrimitive.not()) {
70+
allElementAreSamePrimitiveType = false
71+
return@loop
72+
}
73+
if (theSamePrimitiveType(jsonArray[0].asJsonPrimitive, it.asJsonPrimitive).not()) {
74+
allElementAreSamePrimitiveType = false
75+
return@loop
76+
}
7177
}
78+
7279
}
80+
7381
return allElementAreSamePrimitiveType
7482
}
7583

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private fun JsonArray.onlyHasOneElement(): Boolean {
4646
/**
4747
* array only has object element
4848
*/
49-
private fun JsonArray.allObjectElement(): Boolean {
49+
private fun JsonArray.allItemAreObjectElement(): Boolean {
5050
forEach {
5151
if (it.isJsonObject.not() && it.isJsonNull.not()) {
5252
return false
@@ -99,20 +99,36 @@ fun JsonArray.onlyHasOneObjectElementRecursive(): Boolean {
9999

100100

101101
/**
102-
* if Multidimensional Arrays only has one dimension contains element and the elements are all object element
102+
* if Multidimensional Arrays only has one dimension contains element and the elements all are object element
103103
*/
104-
fun JsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive(): Boolean {
104+
fun JsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive(): Boolean {
105105
if (size() == 0) {
106106
return false
107107
}
108108

109+
if (onlyHasOneElement().not()) {
110+
return false
111+
}
112+
109113
if (get(0).isJsonPrimitive || get(0).isJsonNull) {
110114
return false
111115
}
112116

113-
if (allObjectElement()) {
117+
if (allItemAreObjectElement()) {
114118
return true
115119
}
116120

117-
return get(0).asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()
121+
return get(0).asJsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
118122
}
123+
124+
125+
fun JsonArray.filterOutNullElement(): JsonArray {
126+
127+
val jsonElements = filter { it.isJsonNull.not() }
128+
return JsonArray().apply {
129+
jsonElements.forEach {jsonElement->
130+
add(jsonElement)
131+
}
132+
}
133+
134+
}

src/test/kotlin/wu/seal/jsontokotlin/TargetJsonElementTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class TargetJsonElementTest {
4646
val text9 = """[[1,2],[]]"""
4747

4848

49+
val text10 = """[
50+
{
51+
"distTypeId": "55f40fa5-b6b4-4dcf-a963-52a57f53a71e",
52+
"distTypeName": "มแี ผง"
53+
},
54+
"..."
55+
]"""
4956

5057

5158
val targetElementJson1 = getTargetElementJson(gson, text1)
@@ -75,12 +82,16 @@ class TargetJsonElementTest {
7582
val targetElementJson9 = getTargetElementJson(gson, text9)
7683
targetElementJson9.should.be.equal("{}")
7784

85+
val targetElementJson10 = getTargetElementJson(gson, text10)
86+
targetElementJson10.should.be.equal("{}")
87+
7888
}
7989

8090
private fun getTargetElementJson(gson: Gson, text1: String): String {
8191
return try {
8292
gson.toJson(TargetJsonElement(text1).getTargetJsonElementForGeneratingCode())
8393
} catch(e: Exception) {
94+
e.printStackTrace()
8495
error
8596
}
8697
}

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

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,94 @@
11
package wu.seal.jsontokotlin.utils
22

3+
import com.google.gson.Gson
4+
import com.google.gson.JsonArray
35
import com.winterbe.expekt.should
46
import org.junit.Test
57

68
class ExtensionsKtTest {
79

10+
val gson= Gson()
11+
val jsonArrayStringArray = arrayOf("""[
12+
{
13+
"fid": "919a8918-8189-11e7-9f08-00163e0cb30c",
14+
"fclient": null,
15+
"fbooth": null,
16+
"fjumpposition": 1,
17+
"ffunctiontype": 0,
18+
"fproductid": null,
19+
"forder": 0,
20+
"fname": null,
21+
"furl": "www.baidu.com",
22+
"fimgpath": "img0/M00/05/9D/ChpbMFmSn6eEamI5AAAAAIlBwUQ075.jpg",
23+
"feffect": 0,
24+
"fbegintime": null,
25+
"fendtime": null,
26+
"fcreatorid": null,
27+
"fcreatetime": null,
28+
"fupdateuserid": null,
29+
"fupdatetime": null,
30+
"fdescrpition": null,
31+
"fkeyarea": null,
32+
"farea": null,
33+
"fdwelltime": 0,
34+
"fsliptime": 0
35+
},
36+
{
37+
"fid": "03e0867e-7e5c-11e7-9f08-00163e0cb30c",
38+
"fclient": null,
39+
"fbooth": null,
40+
"fjumpposition": 1,
41+
"ffunctiontype": 0,
42+
"fproductid": null,
43+
"forder": 0,
44+
"fname": null,
45+
"furl": "",
46+
"fimgpath": "img0/M00/05/8F/ChpbMFmNSryEeKYnAAAAADcdJRc354.jpg",
47+
"feffect": 0,
48+
"fbegintime": null,
49+
"fendtime": null,
50+
"fcreatorid": null,
51+
"fcreatetime": null,
52+
"fupdateuserid": null,
53+
"fupdatetime": null,
54+
"fdescrpition": null,
55+
"fkeyarea": null,
56+
"farea": null,
57+
"fdwelltime": 0,
58+
"fsliptime": 0
59+
},
60+
[
61+
{
62+
"fdwelltime": 5,
63+
"fsliptime": 0.3
64+
}
65+
]
66+
]""","""
67+
[
68+
{
69+
"fid": "414b83e6-eece-11e8-9463-00163e0cb30c",
70+
"fimgpath": "img0/M00/0C/DB/ChpbMFv3cTKEAhMaAAAAAD7RexY296.jpg"
71+
},
72+
{
73+
"fid": "c9bb824e-c394-11e8-9463-00163e0cb30c",
74+
"fimgpath": "img0/M00/0C/93/ChpbMFvbuDyEDHAyAAAAAMUFSak005.jpg"
75+
},
76+
[
77+
{
78+
"fdwelltime": 5,
79+
"fsliptime": 0.3
80+
}
81+
]
82+
]
83+
""","""
84+
[
85+
{
86+
"distTypeId": "55f40fa5-b6b4-4dcf-a963-52a57f53a71e",
87+
"distTypeName": "มแี ผง"
88+
},
89+
"..."
90+
]
91+
""".trimIndent())
892
@Test
993
fun numberOf() {
1094
val s = "I am a %s boy yes you yes you yes !"
@@ -21,4 +105,37 @@ class ExtensionsKtTest {
21105
it.should.be.equal("Seal")
22106
}
23107
}
108+
109+
@Test
110+
fun onlyHasOneElementRecursive() {
111+
112+
jsonArrayStringArray.forEach { jsonArrayString->
113+
114+
val jsonArray =gson.fromJson<JsonArray>(jsonArrayString,JsonArray::class.java)
115+
116+
jsonArray.onlyHasOneElementRecursive().should.be.`false`
117+
}
118+
}
119+
120+
@Test
121+
fun onlyHasOneObjectElementRecursive() {
122+
123+
jsonArrayStringArray.forEach { jsonArrayString->
124+
125+
val jsonArray =gson.fromJson<JsonArray>(jsonArrayString,JsonArray::class.java)
126+
127+
jsonArray.onlyHasOneObjectElementRecursive().should.be.`false`
128+
}
129+
}
130+
131+
@Test
132+
fun onlyHasOneSubArrayAndAllAreObjectElementRecursive() {
133+
134+
jsonArrayStringArray.forEach { jsonArrayString->
135+
136+
val jsonArray =gson.fromJson<JsonArray>(jsonArrayString,JsonArray::class.java)
137+
138+
jsonArray.onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive().should.be.`false`
139+
}
140+
}
24141
}

0 commit comments

Comments
 (0)