11package wu.seal.jsontokotlin
22
3+ import com.google.gson.Gson
34import com.google.gson.JsonElement
45import com.google.gson.JsonObject
6+ import com.sun.org.apache.regexp.internal.RE
57import wu.seal.jsontokotlin.codeelements.KClassAnnotation
68import wu.seal.jsontokotlin.codeelements.KProperty
79import wu.seal.jsontokotlin.utils.*
@@ -16,16 +18,20 @@ class KotlinCodeMaker {
1618 private var className: String? = null
1719 private var inputElement: JsonElement ? = null
1820
21+ private var originElement: JsonElement
22+
1923 private val indent = getIndent()
2024
2125 private val toBeAppend = HashSet <String >()
2226
2327 constructor (className: String , inputText: String ) {
28+ originElement = Gson ().fromJson<JsonElement >(inputText,JsonElement ::class .java)
2429 this .inputElement = TargetJsonElement (inputText).getTargetJsonElementForGeneratingCode()
2530 this .className = className
2631 }
2732
2833 constructor (className: String , jsonElement: JsonElement ) {
34+ originElement = jsonElement
2935 this .inputElement = TargetJsonElement (jsonElement).getTargetJsonElementForGeneratingCode()
3036 this .className = className
3137 }
@@ -35,12 +41,10 @@ class KotlinCodeMaker {
3541 stringBuilder.append(" \n " )
3642
3743 val jsonElement = inputElement
38- if (jsonElement!! .isJsonObject) {
39- appClassName(stringBuilder)
40- appendCodeMember(stringBuilder, jsonElement.asJsonObject)
41- } else {
42- throw IllegalArgumentException (" UnSupport" )
43- }
44+ checkIsNotEmptyObjectJSONElement(jsonElement)
45+
46+ appendClassName(stringBuilder)
47+ appendCodeMember(stringBuilder, jsonElement?.asJsonObject!! )
4448
4549 stringBuilder.append(" )" )
4650 if (toBeAppend.isNotEmpty()) {
@@ -50,6 +54,35 @@ class KotlinCodeMaker {
5054 return stringBuilder.toString()
5155 }
5256
57+ // the fucking code
58+ private fun checkIsNotEmptyObjectJSONElement (jsonElement : JsonElement ? ) {
59+ if (jsonElement!! .isJsonObject) {
60+ if (jsonElement.asJsonObject.entrySet().isEmpty() && originElement.isJsonArray) {
61+ // when [[[{}]]]
62+ if (originElement.asJsonArray.onlyHasOneElementRecursive()) {
63+ val unSupportJsonException = UnSupportJsonException (" Unsupported Json String" )
64+ val adviceType = getArrayType(" Any" , originElement.asJsonArray).replace(Regex (" Int|Float|String|Boolean" ), " Any" )
65+ unSupportJsonException.advice = """ No need converting, just use $adviceType is enough for your json string"""
66+ throw unSupportJsonException
67+ } else {
68+ // when [1,"a"]
69+ val unSupportJsonException = UnSupportJsonException (" Unsupported Json String" )
70+ unSupportJsonException.advice = """ No need converting, just use List<Any> is enough for your json string"""
71+ throw unSupportJsonException
72+ }
73+ }
74+ } else {
75+ /* *
76+ * in this condition the only result it that we just give the json a List<Any> type is enough, No need to
77+ * do any convert to make class type
78+ */
79+ val unSupportJsonException = UnSupportJsonException (" Unsupported Json String" )
80+ val adviceType = getArrayType(" Any" , originElement.asJsonArray).replace(" AnyX" , " Any" )
81+ unSupportJsonException.advice = """ No need converting, just use $adviceType is enough for your json string"""
82+ throw unSupportJsonException
83+ }
84+ }
85+
5386 private fun appendSubClassCode (stringBuilder : StringBuilder ) {
5487 if (ConfigManager .isInnerClassModel) {
5588 appendInnerClassModelSubClassCode(stringBuilder)
@@ -77,7 +110,7 @@ class KotlinCodeMaker {
77110 }
78111 }
79112
80- private fun appClassName (stringBuilder : StringBuilder ) {
113+ private fun appendClassName (stringBuilder : StringBuilder ) {
81114 val classAnnotation = KClassAnnotation .getClassAnnotation(className.toString())
82115 stringBuilder.append(classAnnotation)
83116 if (classAnnotation.isNotBlank()) stringBuilder.append(" \n " )
@@ -111,14 +144,12 @@ class KotlinCodeMaker {
111144 if (ConfigManager .enableMapType && maybeJsonObjectBeMapType(jsonElementValue.asJsonObject)) {
112145 val mapKeyType = getMapKeyTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
113146 val mapValueType = getMapValueTypeConvertFromJsonObject(jsonElementValue.asJsonObject)
114- if (mapValueType == MAP_DEFAULT_OBJECT_VALUE_TYPE
115- || mapValueType.contains(MAP_DEFAULT_ARRAY_ITEM_VALUE_TYPE )
116- ) {
147+ if (mapValueIsObjectType(mapValueType)) {
117148 toBeAppend.add(
118- KotlinCodeMaker (
119- getChildType(mapValueType),
120- jsonElementValue.asJsonObject.entrySet().first().value
121- ).makeKotlinData()
149+ KotlinCodeMaker (
150+ getChildType(mapValueType),
151+ jsonElementValue.asJsonObject.entrySet().first().value
152+ ).makeKotlinData()
122153 )
123154 }
124155 val mapType = " Map<$mapKeyType ,$mapValueType >"
@@ -136,13 +167,16 @@ class KotlinCodeMaker {
136167 }
137168 }
138169
170+ private fun mapValueIsObjectType (mapValueType : String ) = (mapValueType == MAP_DEFAULT_OBJECT_VALUE_TYPE
171+ || mapValueType.contains(MAP_DEFAULT_ARRAY_ITEM_VALUE_TYPE ))
172+
139173
140174 private fun addProperty (
141- stringBuilder : StringBuilder ,
142- property : String ,
143- type : String ,
144- value : String? ,
145- isLast : Boolean = false
175+ stringBuilder : StringBuilder ,
176+ property : String ,
177+ type : String ,
178+ value : String? ,
179+ isLast : Boolean = false
146180 ) {
147181 var innerValue = value
148182 if (innerValue == null ) {
@@ -158,7 +192,7 @@ class KotlinCodeMaker {
158192 val propertyComment = p.getPropertyComment()
159193 if (! ConfigManager .isCommentOff && propertyComment.isNotBlank())
160194 stringBuilder.append(" // " )
161- .append(getCommentCode(propertyComment))
195+ .append(getCommentCode(propertyComment))
162196 stringBuilder.append(" \n " )
163197 }
164198
0 commit comments