Skip to content

Commit 1b29a1b

Browse files
committed
improve sanitisation (leave some failing names for now, unlikely case)
1 parent 0903c20 commit 1b29a1b

File tree

5 files changed

+80
-70
lines changed

5 files changed

+80
-70
lines changed

TODO.md

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Ordered roughly in priority:
66

77
- Pass tests
88
- Add more real world scenarios (particularly nested objects)
9-
- GSON serialisation delegate
109
- Symbol pool for class names
1110

1211
- Make website tool prettier

core/src/main/kotlin/com/fractalwrench/json2kotlin/GsonBuildDelegate.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class GsonBuildDelegate: SourceBuildDelegate {
1010
override fun prepareClassProperty(propertyBuilder: PropertySpec.Builder,
1111
kotlinIdentifier: String,
1212
jsonKey: String?) {
13-
if (kotlinIdentifier != jsonKey) {
13+
if (kotlinIdentifier != jsonKey && jsonKey != null) {
1414
val serializedNameBuilder = AnnotationSpec.builder(SerializedName::class)
15-
serializedNameBuilder.addMember("value=\"$jsonKey\"")
15+
serializedNameBuilder.addMember("value=\"${jsonKey.replace("%".toRegex(), "%%")}\"", "")
1616
propertyBuilder.addAnnotation(serializedNameBuilder.build())
1717
}
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.fractalwrench.json2kotlin.valid
2-
2+
import com.google.gson.annotations.SerializedName
33
import kotlin.Any
44
import kotlin.Array
55
import kotlin.Boolean
66
import kotlin.Number
77
import kotlin.String
88

9-
109
data class PlaqueExample(val data: Array<Any>, val meta: Meta)
1110

1211
data class Meta(val view: View)
@@ -41,7 +40,7 @@ data class View(
4140
val publicationDate: Number,
4241
val publicationGroup: Number,
4342
val publicationStage: String,
44-
val query: Query,
43+
val query: License,
4544
val rights: Array<String>,
4645
val rowClass: String,
4746
val rowIdentifierColumnId: Number,
@@ -55,91 +54,81 @@ data class View(
5554
val viewType: String
5655
)
5756

58-
data class TableAuthor(
59-
val id: String,
60-
val displayName: String,
61-
val profileImageUrlLarge: String,
62-
val profileImageUrlMedium: String,
63-
val profileImageUrlSmall: String,
64-
val screenName: String,
65-
val type: String,
66-
val flags: Array<String>
67-
)
57+
data class License(val name: String?)
6858

69-
class Query
59+
data class Metadata(
60+
val availableDisplayTypes: Array<String>,
61+
val custom_fields: Custom_fields,
62+
val rdfClass: String,
63+
val rdfSubject: String,
64+
val renderTypeConfig: RenderTypeConfig,
65+
val rowIdentifier: String
66+
)
7067

7168
data class Owner(
72-
val id: String,
7369
val displayName: String,
70+
val flags: Array<String>,
71+
val id: String,
7472
val profileImageUrlLarge: String,
7573
val profileImageUrlMedium: String,
7674
val profileImageUrlSmall: String,
7775
val screenName: String,
78-
val type: String,
79-
val flags: Array<String>
80-
)
81-
82-
data class Metadata(
83-
val rdfSubject: String,
84-
val rdfClass: String,
85-
val custom_fields: Custom_fields,
86-
val rowIdentifier: String,
87-
val availableDisplayTypes: Array<String>,
88-
val renderTypeConfig: RenderTypeConfig
89-
)
90-
91-
data class RenderTypeConfig(val visible: Visible)
92-
93-
data class Visible(val table: Boolean)
94-
95-
data class Custom_fields(val Publication: Publication, val AdditionalLicenceDetail: AdditionalLicenceDetail)
96-
97-
data class AdditionalLicenceDetail(
98-
val ReuserGuidelines: String,
99-
val AdditionalLicenceInformation: String,
100-
val LicenceURL: String
101-
)
102-
103-
data class Publication(val UpdateFrequency: String)
104-
105-
data class License(val name: String)
106-
107-
data class Grants(
108-
val inherited: Boolean,
109-
val type: String,
110-
val flags: Array<String>
76+
val type: String
11177
)
11278

11379
data class Columns(
114-
val id: Number,
115-
val name: String,
80+
val cachedContents: CachedContents?,
11681
val dataTypeName: String,
117-
val description: String,
82+
val description: String?,
11883
val fieldName: String,
84+
val flags: Array<String>?,
85+
val format: CachedContents,
86+
val id: Number,
87+
val name: String,
11988
val position: Number,
12089
val renderTypeName: String,
121-
val tableColumnId: Number,
122-
val width: Number,
123-
val cachedContents: CachedContents,
124-
val format: Format,
125-
val subColumnTypes: Array<String>?
90+
val subColumnTypes: Array<String>?,
91+
val tableColumnId: Number?,
92+
val width: Number?
93+
)
94+
95+
data class Custom_fields(@SerializedName(value="Additional Licence Detail")
96+
val Additional_Licence_Detail: CachedContents, val Publication: CachedContents)
97+
98+
data class Grants(
99+
val flags: Array<String>,
100+
val inherited: Boolean,
101+
val type: String
126102
)
127103

128-
data class Format(val view: String, val align: String)
104+
data class RenderTypeConfig(val visible: CachedContents)
129105

130106
data class CachedContents(
131-
val non_null: Number,
132-
val largest: Largest,
133-
val _null: Number,
107+
@SerializedName(value="null")
108+
val `null`: Number?,
109+
@SerializedName(value="Additional Licence Information")
110+
val Additional_Licence_Information: String?,
111+
val align: String?,
112+
val average: String?,
113+
val largest: Any?,
114+
@SerializedName(value="Licence URL")
115+
val Licence_URL: String?,
116+
val noCommas: String?,
117+
val non_null: Number?,
118+
val precisionStyle: String?,
119+
@SerializedName(value="Re-user Guidelines")
120+
val Re_user_Guidelines: String?,
121+
val smallest: Any?,
122+
val sum: String?,
123+
val table: Boolean?,
134124
val top: Array<Top>?,
135-
val smallest: Smallest
125+
@SerializedName(value="Update Frequency")
126+
val Update_Frequency: String?,
127+
val view: String?
136128
)
137129

138-
data class Smallest(val latitude: String, val longitude: String)
130+
data class Largest(val latitude: String, val longitude: String)
139131

140-
data class Top(val item: Item, val count: Number)
132+
data class Top(val count: Number, val item: Any)
141133

142134
data class Item(val latitude: String, val longitude: String)
143-
144-
data class Largest(val latitude: String, val longitude: String)
145-
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
import com.google.gson.annotations.SerializedName
12
import kotlin.String
23

34
data class SanitiseExample(
5+
@SerializedName(value="1")
46
val _1: String,
7+
@SerializedName(value="$a")
58
val _a: String,
9+
@SerializedName(value="!f")
610
val _f: String,
11+
@SerializedName(value="Test!word1")
712
val Test_word1: String,
13+
@SerializedName(value="Test word10")
814
val Test_word10: String,
15+
@SerializedName(value="Test.word11")
916
val Test_word11: String,
17+
@SerializedName(value="Test"word2")
1018
val Test_word2: String,
19+
@SerializedName(value="Test£word3")
1120
val Test_word3: String,
21+
@SerializedName(value="Test$word4")
1222
val Test_word4: String,
23+
@SerializedName(value="Test%word5")
1324
val Test_word5: String,
25+
@SerializedName(value="Test^word6")
1426
val Test_word6: String,
27+
@SerializedName(value="Test&word7")
1528
val Test_word7: String,
29+
@SerializedName(value="Test*word8")
1630
val Test_word8: String,
31+
@SerializedName(value="Test{word9")
1732
val Test_word9: String
1833
)

core/src/test/resources/valid/realworld/PlaqueExample.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.google.gson.annotations.SerializedName
12
import kotlin.Any
23
import kotlin.Array
34
import kotlin.Boolean
@@ -90,7 +91,8 @@ data class Columns(
9091
val width: Number?
9192
)
9293

93-
data class Custom_fields(val Additional_Licence_Detail: CachedContents, val Publication: CachedContents)
94+
data class Custom_fields(@SerializedName(value="Additional Licence Detail")
95+
val Additional_Licence_Detail: CachedContents, val Publication: CachedContents)
9496

9597
data class Grants(
9698
val flags: Array<String>,
@@ -101,20 +103,25 @@ data class Grants(
101103
data class RenderTypeConfig(val visible: CachedContents)
102104

103105
data class CachedContents(
106+
@SerializedName(value="null")
104107
val `null`: Number?,
108+
@SerializedName(value="Additional Licence Information")
105109
val Additional_Licence_Information: String?,
106110
val align: String?,
107111
val average: String?,
108112
val largest: Any?,
113+
@SerializedName(value="Licence URL")
109114
val Licence_URL: String?,
110115
val noCommas: String?,
111116
val non_null: Number?,
112117
val precisionStyle: String?,
118+
@SerializedName(value="Re-user Guidelines")
113119
val Re_user_Guidelines: String?,
114120
val smallest: Any?,
115121
val sum: String?,
116122
val table: Boolean?,
117123
val top: Array<Top>?,
124+
@SerializedName(value="Update Frequency")
118125
val Update_Frequency: String?,
119126
val view: String?
120127
)

0 commit comments

Comments
 (0)