Skip to content

Commit 2f05f65

Browse files
authored
Merge pull request #97 from wuseal/fixbu/issue90
Fix issue #90
2 parents 1650628 + 40feb44 commit 2f05f65

File tree

3 files changed

+418
-10
lines changed

3 files changed

+418
-10
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ class KotlinCodeMaker {
6161
if (originElement.asJsonArray.onlyHasOneElementRecursive()) {
6262
val unSupportJsonException = UnSupportJsonException("Unsupported Json String")
6363
val adviceType = getArrayType("Any", originElement.asJsonArray).replace(Regex("Int|Float|String|Boolean"), "Any")
64+
unSupportJsonException.adviceType = adviceType
6465
unSupportJsonException.advice = """No need converting, just use $adviceType is enough for your json string"""
6566
throw unSupportJsonException
6667
} else {
6768
//when [1,"a"]
6869
val unSupportJsonException = UnSupportJsonException("Unsupported Json String")
70+
unSupportJsonException.adviceType = "List<Any>"
6971
unSupportJsonException.advice = """No need converting, List<Any> may be a good class type for your json string"""
7072
throw unSupportJsonException
7173
}
@@ -128,12 +130,18 @@ class KotlinCodeMaker {
128130
val isLast = (index == size - 1)
129131

130132
if (jsonElementValue.isJsonArray) {
131-
val type = getArrayType(property, jsonElementValue.asJsonArray)
133+
var type = getArrayType(property, jsonElementValue.asJsonArray)
132134

133135
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray) || jsonElementValue.asJsonArray.onlyHasOneObjectElementRecursive()
134136
|| jsonElementValue.asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()) {
135137

136-
toBeAppend.add(KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData())
138+
val subCode = try {
139+
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
140+
} catch (e: UnSupportJsonException) {
141+
type = e.adviceType
142+
""
143+
}
144+
toBeAppend.add(subCode)
137145
}
138146
addProperty(stringBuilder, property, type, "", isLast)
139147

@@ -144,21 +152,33 @@ class KotlinCodeMaker {
144152
} else if (jsonElementValue.isJsonObject) {
145153
if (ConfigManager.enableMapType && maybeJsonObjectBeMapType(jsonElementValue.asJsonObject)) {
146154
val mapKeyType = getMapKeyTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
147-
val mapValueType = getMapValueTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
155+
var mapValueType = getMapValueTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
148156
if (mapValueIsObjectType(mapValueType)) {
157+
val subCode = try {
158+
KotlinCodeMaker(
159+
getChildType(mapValueType),
160+
jsonElementValue.asJsonObject.entrySet().first().value
161+
).makeKotlinData()
162+
} catch (e: UnSupportJsonException) {
163+
mapValueType = e.adviceType
164+
""
165+
}
149166
toBeAppend.add(
150-
KotlinCodeMaker(
151-
getChildType(mapValueType),
152-
jsonElementValue.asJsonObject.entrySet().first().value
153-
).makeKotlinData()
167+
subCode
154168
)
155169
}
156170
val mapType = "Map<$mapKeyType,$mapValueType>"
157171
addProperty(stringBuilder, property, mapType, "", isLast)
158172

159173
} else {
160-
val type = getJsonObjectType(property)
161-
toBeAppend.add(KotlinCodeMaker(getRawType(type), jsonElementValue).makeKotlinData())
174+
var type = getJsonObjectType(property)
175+
val subCode = try {
176+
KotlinCodeMaker(getRawType(type), jsonElementValue).makeKotlinData()
177+
} catch (e: UnSupportJsonException) {
178+
type = e.adviceType
179+
""
180+
}
181+
toBeAppend.add(subCode)
162182
addProperty(stringBuilder, property, type, "", isLast)
163183
}
164184

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ package wu.seal.jsontokotlin
66
*/
77
class UnSupportJsonException(message: String = "") : Exception(message) {
88
var advice: String = ""
9-
9+
var adviceType: String = ""
1010
}

0 commit comments

Comments
 (0)