@@ -40,14 +40,14 @@ class GenericListClassGeneratorByJSONArray(private val jsonKey: String, jsonArra
4040 return GenericListClass (generic = elementKotlinClass)
4141 }
4242 jsonArrayExcludeNull.allItemAreObjectElement() -> {
43- val fatJsonObject = getFatJsonObject(jsonArrayExcludeNull )
43+ val fatJsonObject = jsonArrayExcludeNull. getFatJsonObject()
4444 val itemObjClassName = getRecommendItemName(jsonKey)
4545 val dataClassFromJsonObj = DataClassGeneratorByJSONObject (itemObjClassName, fatJsonObject).generate()
4646 LogUtil .i(" $tag jsonArray allItemAreObjectElement, return GenericListClass with generic type ${dataClassFromJsonObj.name} " )
4747 return GenericListClass (generic = dataClassFromJsonObj)
4848 }
4949 jsonArrayExcludeNull.allItemAreArrayElement() -> {
50- val fatJsonArray = getFatJsonArray( jsonArrayExcludeNull.map { it.asJsonArray } )
50+ val fatJsonArray = jsonArrayExcludeNull.getFatJsonArray( )
5151 val genericListClassFromFatJsonArray = GenericListClassGeneratorByJSONArray (jsonKey, fatJsonArray.toString()).generate()
5252 LogUtil .i(" $tag jsonArray allItemAreArrayElement, return GenericListClass with generic type ${genericListClassFromFatJsonArray.name} " )
5353 return GenericListClass (generic = genericListClassFromFatJsonArray)
@@ -62,68 +62,4 @@ class GenericListClassGeneratorByJSONArray(private val jsonKey: String, jsonArra
6262 private fun getRecommendItemName (jsonKey : String ): String {
6363 return adjustPropertyNameForGettingArrayChildType(jsonKey)
6464 }
65-
66- private fun getFatJsonArray (jsonArrayList : List <JsonArray >): JsonArray {
67- val fatJsonArray = JsonArray ()
68- jsonArrayList.forEach {
69- fatJsonArray.addAll(it)
70- }
71- return fatJsonArray
72- }
73-
74-
75- /* *
76- * get a Fat JsonObject whose fields contains all the objects' fields around the objects of the json array
77- */
78- private fun getFatJsonObject (jsonArray : JsonArray ): JsonObject {
79- if (jsonArray.size() == 0 || ! jsonArray.allItemAreObjectElement()) {
80- throw IllegalStateException (" input arg jsonArray must not be empty and all element should be json object! " )
81- }
82- val allFields = jsonArray.flatMap { it.asJsonObject.entrySet().map { entry -> Pair (entry.key, entry.value) } }
83- val fatJsonObject = JsonObject ()
84- allFields.forEach { (key, value) ->
85- if (value is JsonNull ) {
86- // if the value is null and pre added the same key into the fatJsonObject,
87- // then translate it to a new special property to indicate that the property is nullable
88- // later will consume this property (do it here[DataClassGeneratorByJSONObject#consumeBackstageProperties])
89- // delete it or translate it back to normal property without [BACKSTAGE_NULLABLE_POSTFIX] when consume it
90- // and will not be generated in final code
91- if (fatJsonObject.has(key)) {
92- fatJsonObject.add(key + BACKSTAGE_NULLABLE_POSTFIX , value)
93- } else {
94- fatJsonObject.add(key, value)
95- fatJsonObject.add(key + BACKSTAGE_NULLABLE_POSTFIX , value)
96- }
97- } else if (fatJsonObject.has(key)) {
98- if (fatJsonObject[key].isJsonObject && value.isJsonObject) {// try not miss any fields of the key's related json object
99- val newValue = getFatJsonObject(JsonArray ().apply { add(fatJsonObject[key]);add(value) })
100- fatJsonObject.add(key, newValue)
101- } else if (fatJsonObject[key].isJsonArray && value.isJsonArray) {// //try not miss any elements of the key's related json array
102- val newValue = getFatJsonArray(listOf (fatJsonObject[key].asJsonArray, value.asJsonArray))
103- fatJsonObject.add(key, newValue)
104- } else if (fatJsonObject[key].isJsonPrimitive && value.isJsonPrimitive && theSamePrimitiveType(fatJsonObject[key].asJsonPrimitive, value.asJsonPrimitive)) {
105- // if the value and exist value are the same primitive type then ignore it
106- // except for the following scenario:
107- // when the the field is a number type, we need to select the value with largest scope
108- // e.g. given [{"key":10},{"key":11.2}]
109- // we should use the object with value = 11.2 to represent the object type which will be Double
110-
111- val prev = fatJsonObject[key].asJsonPrimitive
112- val cur = value.asJsonPrimitive
113- if (prev.isNumber && cur.isNumber && cur.toKotlinClass().getNumLevel() > prev.toKotlinClass().getNumLevel()) {
114- fatJsonObject.add(key, value);
115- }
116- } else if (value.isJsonNull) {
117- // if the value is null, we ignore this value
118- } else {
119- // others the two values of the key are not the same type, then give it a null value indicate that it's should be an Any Type in Kotlin
120- fatJsonObject.add(key, JsonNull .INSTANCE )
121- }
122- } else {
123- fatJsonObject.add(key, value)
124- }
125- }
126- return fatJsonObject
127- }
128-
12965}
0 commit comments