Skip to content

Commit 287f148

Browse files
committed
Fix issue #90
1 parent 1650628 commit 287f148

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: 35 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,20 @@ 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+
} catch (e: Exception) {
144+
""
145+
}
146+
toBeAppend.add(subCode)
137147
}
138148
addProperty(stringBuilder, property, type, "", isLast)
139149

@@ -144,21 +154,37 @@ class KotlinCodeMaker {
144154
} else if (jsonElementValue.isJsonObject) {
145155
if (ConfigManager.enableMapType && maybeJsonObjectBeMapType(jsonElementValue.asJsonObject)) {
146156
val mapKeyType = getMapKeyTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
147-
val mapValueType = getMapValueTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
157+
var mapValueType = getMapValueTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
148158
if (mapValueIsObjectType(mapValueType)) {
159+
val subCode = try {
160+
KotlinCodeMaker(
161+
getChildType(mapValueType),
162+
jsonElementValue.asJsonObject.entrySet().first().value
163+
).makeKotlinData()
164+
} catch (e: UnSupportJsonException) {
165+
mapValueType = e.adviceType
166+
""
167+
} catch (e: Exception) {
168+
""
169+
}
149170
toBeAppend.add(
150-
KotlinCodeMaker(
151-
getChildType(mapValueType),
152-
jsonElementValue.asJsonObject.entrySet().first().value
153-
).makeKotlinData()
171+
subCode
154172
)
155173
}
156174
val mapType = "Map<$mapKeyType,$mapValueType>"
157175
addProperty(stringBuilder, property, mapType, "", isLast)
158176

159177
} else {
160-
val type = getJsonObjectType(property)
161-
toBeAppend.add(KotlinCodeMaker(getRawType(type), jsonElementValue).makeKotlinData())
178+
var type = getJsonObjectType(property)
179+
val subCode = try {
180+
KotlinCodeMaker(getRawType(type), jsonElementValue).makeKotlinData()
181+
} catch (e: UnSupportJsonException) {
182+
type = e.adviceType
183+
""
184+
} catch (e: Exception) {
185+
""
186+
}
187+
toBeAppend.add(subCode)
162188
addProperty(stringBuilder, property, type, "", isLast)
163189
}
164190

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)