Skip to content

Commit 1f16ae8

Browse files
author
seal
committed
Add supporter for Fastjson annotation generate,supporter json lib --Fastjson.
Perfect ConfigInfo statistic
1 parent 62534aa commit 1f16ae8

File tree

13 files changed

+114
-16
lines changed

13 files changed

+114
-16
lines changed

resources/META-INF/plugin.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>wu.seal.tool.jsontokotlin</id>
33
<name>JsonToKotlinClass</name>
4-
<version>1.4-alpha</version>
4+
<version>1.4</version>
55
<vendor email="[email protected]" url="https://www.github.com/wuseal">Seal</vendor>
66

77
<description><![CDATA[
@@ -12,8 +12,9 @@
1212

1313
<change-notes><![CDATA[
1414
1.4:<br>
15-
* Add supporter for Jackson annotation generate,supporter json lib --Jackson
16-
* Beautify the config settings dialog
15+
* Add supporter for Jackson annotation generate,supporter json lib --Jackson.<br>
16+
* Add supporter for Fastjson annotation generate,supporter json lib --Fastjson.<br>
17+
* Beautify the config settings dialog.<br>
1718
1.3:<br>
1819
* add property init with default value option in property tab.<br>
1920
* add property could be nullable option in property tab.<br>

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var isTestModel = false
116116
object TestConfig {
117117
var isCommentOff = false
118118
var isPropertiesVar = false
119-
var targetJsonConvertLib = TargetJsonConverter.Gson
119+
var targetJsonConvertLib = TargetJsonConverter.None
120120
var isPropertyNullable = true
121121
var initWithDefaultValue = true
122122
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package wu.seal.jsontokotlin
2+
3+
/**
4+
* supporter for alibaba fastjson
5+
* Created by Seal.Wu on 2017/9/28.
6+
*/
7+
8+
9+
object FastjsonSupporter:IJsonLibSupporter {
10+
11+
internal val annotationImportClassString = "import com.alibaba.fastjson.annotation.JSONField"
12+
13+
private val propertyAnnotation ="@JSONField(name = \"%s\")"
14+
15+
16+
override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String):String {
17+
18+
val fastjsonSupportPropertyBuilder = StringBuilder()
19+
20+
fastjsonSupportPropertyBuilder.append(propertyAnnotation.format(rawPropertyName))
21+
22+
fastjsonSupportPropertyBuilder.append(" ")
23+
24+
fastjsonSupportPropertyBuilder.append(PropertyKeyword.get())
25+
26+
fastjsonSupportPropertyBuilder.append(" ")
27+
28+
fastjsonSupportPropertyBuilder.append(KPropertyName.getName(rawPropertyName))
29+
30+
fastjsonSupportPropertyBuilder.append(": ")
31+
32+
fastjsonSupportPropertyBuilder.append(propertyType)
33+
34+
if (ConfigManager.initWithDefaultValue) {
35+
fastjsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType))
36+
}
37+
38+
fastjsonSupportPropertyBuilder.append(",")
39+
40+
return fastjsonSupportPropertyBuilder.toString()
41+
}
42+
43+
44+
}

src/wu/seal/jsontokotlin/GsonSupporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object GsonSupporter : IGsonSupporter {
2727
/**
2828
* When adapter Gson lib at most ,We should import the Anotation Class
2929
*/
30-
val gsonAnotationImportString = "import com.google.gson.annotations.SerializedName"
30+
val annotationImportClassString = "import com.google.gson.annotations.SerializedName"
3131

3232
private val anotaionOnProperty = "@SerializedName(\"%s\")"
3333

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package wu.seal.jsontokotlin
2+
3+
/**
4+
*
5+
* Created by Seal.Wu on 2017/9/28.
6+
*/
7+
interface IJsonLibSupporter {
8+
fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String
9+
}

src/wu/seal/jsontokotlin/ImportClassWriter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ object ImportClassWriter : IImportClassWriter {
4343

4444

4545
override fun insertFastJsonImportClass(project: Project?, editFile: Document) {
46-
46+
val importClassString = FastjsonSupporter.annotationImportClassString
47+
insertImportClassString(editFile, importClassString, project)
4748
}
4849

4950
override fun insertJackSonImportClass(project: Project?, editFile: Document) {
50-
val importClassString = JacksonSupporter.jacksonAnnotationImportClassString
51+
val importClassString = JacksonSupporter.annotationImportClassString
5152
insertImportClassString(editFile, importClassString, project)
5253
}
5354

5455
override fun insertGsonImportClass(project: Project?, editFile: Document) {
5556

56-
val importClassString = GsonSupporter.gsonAnotationImportString
57+
val importClassString = GsonSupporter.annotationImportClassString
5758
insertImportClassString(editFile, importClassString, project)
5859
}
5960

src/wu/seal/jsontokotlin/JacksonSupporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package wu.seal.jsontokotlin
77

88
interface IJacksonSupporter {
99

10-
val jacksonAnnotationImportClassString: String
10+
val annotationImportClassString: String
1111
get() = "import com.fasterxml.jackson.annotation.JsonProperty"
1212

1313
/**

src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo
1010
* Created by Seal.wu on 2017/8/21.
1111
*/
1212

13-
const val PLUGIN_VERSION = "1.3"
13+
const val PLUGIN_VERSION = "1.4"
1414

1515
class JsonToKotlinApplication : ApplicationComponent {
1616

src/wu/seal/jsontokotlin/KProperty.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class KProperty(private val rawPropertyName: String, private val propertyType: S
4040
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Jackson) {
4141

4242
blockBulder.append(JacksonSupporter.getJacksonSupporterProperty(rawPropertyName, propertyType))
43+
44+
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.FastJson) {
45+
46+
blockBulder.append(FastjsonSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, propertyType))
47+
4348
}
4449

4550
if (!ConfigManager.isCommentOff && propertyValue.isNotBlank()) {

src/wu/seal/jsontokotlin/KotlinMaker.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,30 @@ fun main(args: Array<String>) {
129129
TestConfig.isPropertiesVar = true
130130
TestConfig.isPropertyNullable = false
131131

132-
println("===========================================Change to none json lib support========================================= ")
132+
println("===========================================Change to Jackson json lib support========================================= ")
133+
134+
println("json1 ====>\n${KotlinMaker("Class1", json1).makeKotlinData()}")
135+
println("json2 ====>\n${KotlinMaker("Class2", json2).makeKotlinData()}")
136+
137+
138+
TestConfig.targetJsonConvertLib = TargetJsonConverter.FastJson
139+
TestConfig.isCommentOff = true
140+
TestConfig.isPropertiesVar = true
141+
TestConfig.isPropertyNullable = false
142+
143+
println("===========================================Change to FastJson json lib support========================================= ")
144+
145+
println("json1 ====>\n${KotlinMaker("Class1", json1).makeKotlinData()}")
146+
println("json2 ====>\n${KotlinMaker("Class2", json2).makeKotlinData()}")
147+
148+
149+
150+
TestConfig.targetJsonConvertLib = TargetJsonConverter.Gson
151+
TestConfig.isCommentOff = false
152+
TestConfig.isPropertiesVar = false
153+
TestConfig.isPropertyNullable = true
154+
155+
println("===========================================Change to Gson json lib support========================================= ")
133156

134157
println("json1 ====>\n${KotlinMaker("Class1", json1).makeKotlinData()}")
135158
println("json2 ====>\n${KotlinMaker("Class2", json2).makeKotlinData()}")

0 commit comments

Comments
 (0)