Skip to content

Commit 42c0bdf

Browse files
committed
fix UnSupport exception #62 #68
1 parent 4d16463 commit 42c0bdf

File tree

6 files changed

+101
-17
lines changed

6 files changed

+101
-17
lines changed

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

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package wu.seal.jsontokotlin
22

3+
import com.google.gson.Gson
34
import com.google.gson.JsonElement
45
import com.google.gson.JsonObject
6+
import com.sun.org.apache.regexp.internal.RE
57
import wu.seal.jsontokotlin.codeelements.KClassAnnotation
68
import wu.seal.jsontokotlin.codeelements.KProperty
79
import 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,16 +41,10 @@ class KotlinCodeMaker {
3541
stringBuilder.append("\n")
3642

3743
val jsonElement = inputElement
38-
if (jsonElement!!.isJsonObject) {
39-
appendClassName(stringBuilder)
40-
appendCodeMember(stringBuilder, jsonElement.asJsonObject)
41-
} else {
42-
/**
43-
* in this condition the only result it that we just give the json a List<Any> type is enough, No need to
44-
* do any convert to make class type
45-
*/
46-
throw UnSupportJsonException("Unsupported Json String")
47-
}
44+
checkIsNotEmptyObjectJSONElement(jsonElement)
45+
46+
appendClassName(stringBuilder)
47+
appendCodeMember(stringBuilder, jsonElement?.asJsonObject!!)
4848

4949
stringBuilder.append(")")
5050
if (toBeAppend.isNotEmpty()) {
@@ -54,6 +54,35 @@ class KotlinCodeMaker {
5454
return stringBuilder.toString()
5555
}
5656

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+
5786
private fun appendSubClassCode(stringBuilder: StringBuilder) {
5887
if (ConfigManager.isInnerClassModel) {
5988
appendInnerClassModelSubClassCode(stringBuilder)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,16 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
7676

7777

7878
} catch (e: UnSupportJsonException) {
79-
Messages.showInfoMessage("No need converting, just use List<Any> is enough for your json string", "Tip")
79+
val advice = e.advice
80+
Messages.showInfoMessage(dealWithHtmlConvert(advice), "Tip")
8081
} catch (e: Throwable) {
8182
dealWithException(jsonString, e)
8283
throw e
8384
}
8485
}
8586

87+
private fun dealWithHtmlConvert(advice: String) = advice.replace("<", "&lt;").replace(">", "&gt;")
88+
8689
private fun reuseClassName(
8790
couldGetAndReuseClassNameInCurrentEditFileForInserCode: Boolean,
8891
className: String,
@@ -127,6 +130,8 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
127130
return false
128131
}
129132

133+
val generateClassesString = codeMaker.makeKotlinDataClassCode()
134+
130135
executeCouldRollBackAction(project) {
131136
var offset: Int
132137

@@ -141,7 +146,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
141146
}
142147
document.insertString(
143148
Math.max(offset, 0),
144-
ClassCodeFilter.removeDuplicateClassCode(codeMaker.makeKotlinDataClassCode())
149+
ClassCodeFilter.removeDuplicateClassCode(generateClassesString)
145150
)
146151
}
147152
return true

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wu.seal.jsontokotlin
22

33
import com.google.gson.*
4+
import wu.seal.jsontokotlin.utils.onlyHasOneElementRecursive
45
import java.util.*
56

67
/**
@@ -29,6 +30,8 @@ class TargetJsonElement : ITargetJsonElement {
2930
} else {
3031
throw IllegalStateException("Unbelievable! should not throw out this exception")
3132
}
33+
} else if(jsonElement.asJsonArray.onlyHasOneElementRecursive()){
34+
return getArrayChildElement(this.jsonElement.asJsonArray)
3235
} else if (allElementAreSamePrimitiveType(jsonElement.asJsonArray)) {
3336
return jsonElement.asJsonArray[0]
3437
} else {
@@ -43,6 +46,7 @@ class TargetJsonElement : ITargetJsonElement {
4346
}
4447
}
4548

49+
4650
private fun allElementAreObject(jsonArray: JsonArray): Boolean {
4751
var allElementAreObject = true
4852
jsonArray.forEach {
@@ -95,6 +99,8 @@ class TargetJsonElement : ITargetJsonElement {
9599
/**
96100
* get an element from the element array , And the result element should contains all the json field in every
97101
* element of the array
102+
*
103+
* the input argument jsonArray should only contains jsonObject or only contains one element Recursive like [[["element"]]]
98104
*/
99105
fun getFullFieldElementFromArrayElement(jsonArray: JsonArray): JsonElement {
100106

@@ -118,6 +124,8 @@ class TargetJsonElement : ITargetJsonElement {
118124

119125
return gson.fromJson(gson.toJson(map), JsonElement::class.java)
120126
}
127+
128+
121129
}
122130

123131
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ package wu.seal.jsontokotlin
44
/**
55
* Throw out when the json to be convert don't support by this plugin or no need to convert to any classes
66
*/
7-
class UnSupportJsonException(message: String = "") : Exception(message)
7+
class UnSupportJsonException(message: String = "") : Exception(message) {
8+
var advice: String = ""
9+
10+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package wu.seal.jsontokotlin.utils
22

3+
import com.google.gson.JsonArray
34
import java.awt.Component
45
import java.awt.Container
56
import java.util.regex.Matcher
@@ -47,4 +48,30 @@ fun String.numberOf(subString: String):Int {
4748
count++
4849
}
4950
return count
51+
}
52+
53+
/**
54+
* array only has one element
55+
*/
56+
private fun JsonArray.onlyHasOneElement(): Boolean {
57+
return size() == 1
58+
}
59+
60+
/**
61+
* if Multidimensional Arrays only has one element
62+
*/
63+
fun JsonArray.onlyHasOneElementRecursive(): Boolean {
64+
65+
if (size() == 0) {
66+
return false
67+
}
68+
if (onlyHasOneElement().not()) {
69+
return false
70+
}
71+
72+
if (get(0).isJsonPrimitive || get(0).isJsonObject || get(0).isJsonNull) {
73+
return true
74+
}
75+
76+
return get(0).asJsonArray.onlyHasOneElementRecursive()
5077
}

src/test/kotlin/wu/seal/jsontokotlin/TargetJsonElementTest.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class TargetJsonElementTest {
4141

4242
val text7 = """[1,"2",true]"""
4343

44+
val text8 = """[[[1]]]"""
45+
46+
val text9 = """[[1,2],[]]"""
47+
48+
49+
4450

4551
val targetElementJson1 = getTargetElementJson(gson, text1)
4652
targetElementJson1.should.be.equal(gson.toJson(gson.fromJson(text1, JsonElement::class.java)))
@@ -52,16 +58,22 @@ class TargetJsonElementTest {
5258
targetElementJson3.should.be.equal(gson.toJson(Any()))
5359

5460
val targetElementJson4 = getTargetElementJson(gson, text4)
55-
targetElementJson4.should.be.equal(error)
61+
targetElementJson4.should.be.equal("1")
5662

5763
val targetElementJson5 = getTargetElementJson(gson, text5)
58-
targetElementJson5.should.be.equal(error)
64+
targetElementJson5.should.be.equal("1")
5965

6066
val targetElementJson6 = getTargetElementJson(gson, text6)
61-
targetElementJson6.should.be.equal(error)
67+
targetElementJson6.should.be.equal("\"1\"")
6268

6369
val targetElementJson7 = getTargetElementJson(gson, text7)
64-
targetElementJson7.should.be.equal(error)
70+
targetElementJson7.should.be.equal("{}")
71+
72+
val targetElementJson8 = getTargetElementJson(gson, text8)
73+
targetElementJson8.should.be.equal("1")
74+
75+
val targetElementJson9 = getTargetElementJson(gson, text9)
76+
targetElementJson9.should.be.equal("{}")
6577

6678
}
6779

0 commit comments

Comments
 (0)