Skip to content

Commit 8a3f3cd

Browse files
committed
Fix issue #67
1 parent d3d7545 commit 8a3f3cd

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package wu.seal.jsontokotlin
33
import com.google.gson.Gson
44
import com.google.gson.JsonElement
55
import com.google.gson.JsonObject
6-
import com.sun.org.apache.regexp.internal.RE
76
import wu.seal.jsontokotlin.codeelements.KClassAnnotation
87
import wu.seal.jsontokotlin.codeelements.KProperty
98
import wu.seal.jsontokotlin.utils.*
@@ -18,14 +17,14 @@ class KotlinCodeMaker {
1817
private var className: String? = null
1918
private var inputElement: JsonElement? = null
2019

21-
private var originElement:JsonElement
20+
private var originElement: JsonElement
2221

2322
private val indent = getIndent()
2423

2524
private val toBeAppend = HashSet<String>()
2625

2726
constructor(className: String, inputText: String) {
28-
originElement = Gson().fromJson<JsonElement>(inputText,JsonElement::class.java)
27+
originElement = Gson().fromJson<JsonElement>(inputText, JsonElement::class.java)
2928
this.inputElement = TargetJsonElement(inputText).getTargetJsonElementForGeneratingCode()
3029
this.className = className
3130
}
@@ -131,7 +130,9 @@ class KotlinCodeMaker {
131130
if (jsonElementValue.isJsonArray) {
132131
val type = getArrayType(property, jsonElementValue.asJsonArray)
133132

134-
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray)) {
133+
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray) || jsonElementValue.asJsonArray.onlyHasOneObjectElementRecursive()
134+
|| jsonElementValue.asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()) {
135+
135136
toBeAppend.add(KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData())
136137
}
137138
addProperty(stringBuilder, property, type, "", isLast)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package wu.seal.jsontokotlin
22

33
import com.google.gson.*
44
import wu.seal.jsontokotlin.utils.onlyHasOneElementRecursive
5-
import java.util.*
5+
import wu.seal.jsontokotlin.utils.onlyOneSubArrayContainsElementAndAllObjectRecursive
66

77
/**
88
* This class aim at filtering out the expected Json Element to be convert from Json array
@@ -30,7 +30,7 @@ class TargetJsonElement : ITargetJsonElement {
3030
} else {
3131
throw IllegalStateException("Unbelievable! should not throw out this exception")
3232
}
33-
} else if(jsonElement.asJsonArray.onlyHasOneElementRecursive()){
33+
} else if (jsonElement.asJsonArray.onlyHasOneElementRecursive() || jsonElement.asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()) {
3434
return getArrayChildElement(this.jsonElement.asJsonArray)
3535
} else if (allElementAreSamePrimitiveType(jsonElement.asJsonArray)) {
3636
return jsonElement.asJsonArray[0]

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

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package wu.seal.jsontokotlin.utils
33
import com.google.gson.JsonArray
44
import java.awt.Component
55
import java.awt.Container
6-
import java.util.regex.Matcher
76
import java.util.regex.Pattern
87
import javax.swing.Box
98
import javax.swing.BoxLayout
@@ -24,7 +23,7 @@ fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component) {
2423

2524
}
2625

27-
fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component, leftMargin:Int) {
26+
fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component, leftMargin: Int) {
2827
if (layout is BoxLayout) {
2928

3029
val hBox = Box.createHorizontalBox()
@@ -40,10 +39,10 @@ fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component, lef
4039
/**
4140
* How many substring in the parent string
4241
*/
43-
fun String.numberOf(subString: String):Int {
42+
fun String.numberOf(subString: String): Int {
4443
var count = 0
4544
val pattern = Pattern.compile(subString)
46-
val matcher= pattern.matcher(this)
45+
val matcher = pattern.matcher(this)
4746
while (matcher.find()) {
4847
count++
4948
}
@@ -57,6 +56,25 @@ private fun JsonArray.onlyHasOneElement(): Boolean {
5756
return size() == 1
5857
}
5958

59+
/**
60+
* array only has one object element
61+
*/
62+
private fun JsonArray.onlyHasOneObjectElement(): Boolean {
63+
return size() == 1 && get(0).isJsonObject
64+
}
65+
66+
/**
67+
* array only has object element
68+
*/
69+
private fun JsonArray.allObjectElement(): Boolean {
70+
forEach {
71+
if (it.isJsonObject.not()) {
72+
return false
73+
}
74+
}
75+
return true
76+
}
77+
6078
/**
6179
* if Multidimensional Arrays only has one element
6280
*/
@@ -74,4 +92,48 @@ fun JsonArray.onlyHasOneElementRecursive(): Boolean {
7492
}
7593

7694
return get(0).asJsonArray.onlyHasOneElementRecursive()
95+
}
96+
97+
98+
/**
99+
* if Multidimensional Arrays only has one element
100+
*/
101+
fun JsonArray.onlyHasOneObjectElementRecursive(): Boolean {
102+
103+
if (size() == 0) {
104+
return false
105+
}
106+
if (onlyHasOneElement().not()) {
107+
return false
108+
}
109+
110+
if (get(0).isJsonPrimitive || get(0).isJsonNull) {
111+
return false
112+
}
113+
114+
if (get(0).isJsonObject) {
115+
return true
116+
}
117+
return get(0).asJsonArray.onlyHasOneObjectElementRecursive()
118+
}
119+
120+
121+
/**
122+
* if Multidimensional Arrays only has one dimension contains element and the elements are all object element
123+
*/
124+
fun JsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive(): Boolean {
125+
126+
if (size() == 0) {
127+
return false
128+
}
129+
130+
if (get(0).isJsonPrimitive || get(0).isJsonNull) {
131+
return false
132+
}
133+
134+
if (allObjectElement()) {
135+
return true
136+
}
137+
138+
return get(0).asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()
77139
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,15 @@ fun getArrayType(propertyName: String, jsonElementValue: JsonArray): String {
103103
getJsonObjectType(preSubType)
104104

105105
} else if (next.isJsonArray) {
106-
getArrayType(preSubType, next.asJsonArray)
106+
if (jsonArray.size() == 1) {
107+
getArrayType(preSubType, next.asJsonArray)
108+
} else {
109+
DEFAULT_TYPE
110+
}
107111
} else {
108112
DEFAULT_TYPE
109113
}
114+
return "List<$subType>"
110115
}
111116
return "List<$subType>"
112117
}

0 commit comments

Comments
 (0)