@@ -15,54 +15,93 @@ import com.squareup.moshi.JsonAdapter
1515import com.squareup.moshi.JsonAdapter.Factory
1616import io.vavr.control.Either
1717import java.lang.reflect.Type
18+ import org.immutables.value.Value
1819
20+ /* *
21+ * Parses a JSON file and converts it to a POJO of type [PojoT].
22+ *
23+ * @param pojoType The type of the POJO to be created
24+ * @param jsonFilePath The path to the JSON file
25+ * @param customAdapters A list of custom JSON adapters to be used during the parsing process
26+ * @param customAdaptersWithType A map of custom JSON adapters with their respective types
27+ * @param typesToSkip A set of types to be skipped during the parsing process
28+ * @param <PojoT> The type of the POJO to be created
29+ * @return the parsed POJO or null if the parsing fails
30+ */
1931@JvmOverloads
2032fun <PojoT : Any > jsonFileToPojo (
2133 pojoType : Type ,
2234 jsonFilePath : String ,
2335 customAdapters : List <Any > = emptyList(),
2436 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
25- typesToIgnore : Set <Class <out Any >> = emptySet()
37+ typesToSkip : Set <Class <out Any >> = emptySet()
2638): PojoT ? {
27- val jsonAdapter =
28- initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToIgnore, pojoType)
39+ val jsonAdapter = initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToSkip, pojoType)
2940 return jsonAdapter.fromJson(bufferFileInResources(jsonFilePath))
3041}
3142
43+ fun <PojoT : Any > jsonFileToPojo (jsonFile : JsonFile <PojoT >): PojoT ? =
44+ jsonFileToPojo(
45+ jsonFile.pojoType(),
46+ jsonFile.jsonFilePath(),
47+ jsonFile.customAdapters(),
48+ jsonFile.customAdaptersWithType(),
49+ jsonFile.typesToSkip()
50+ )
51+
3252inline fun <reified PojoT : Any > jsonFileToPojo (
3353 jsonFilePath : String ,
3454 customAdapters : List <Any > = emptyList(),
3555 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
36- typesToIgnore : Set <Class <out Any >> = emptySet()
56+ typesToSkip : Set <Class <out Any >> = emptySet()
3757): PojoT ? =
3858 jsonFileToPojo(
3959 PojoT ::class .java,
4060 jsonFilePath,
4161 customAdapters,
4262 customAdaptersWithType,
43- typesToIgnore
63+ typesToSkip
4464 )
4565
66+ /* *
67+ * A generic function that parses a JSON string into a POJO (Plain Old Java Object) of type PojoT.
68+ *
69+ * @param <PojoT> The type of the resulting POJO.
70+ * @param pojoType The class of the resulting POJO.
71+ * @param jsonStr The JSON string to be parsed.
72+ * @param customAdapters A list of custom adapters to be used during the parsing process.
73+ * @param customAdaptersWithType A map of custom adapters with their respective types.
74+ * @param typesToSkip A set of classes to be skipped during the parsing process.
75+ * @return The parsed POJO of type PojoT.
76+ */
4677@JvmOverloads
4778fun <PojoT : Any > jsonToPojo (
4879 pojoType : Type ,
4980 jsonStr : String ,
5081 customAdapters : List <Any > = emptyList(),
5182 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
52- typesToIgnore : Set <Class <out Any >> = emptySet()
83+ typesToSkip : Set <Class <out Any >> = emptySet()
5384): PojoT ? {
54- val jsonAdapter =
55- initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToIgnore, pojoType)
85+ val jsonAdapter = initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToSkip, pojoType)
5686 return jsonAdapter.fromJson(jsonStr)
5787}
5888
89+ fun <PojoT : Any > jsonToPojo (jsonString : JsonString <PojoT >): PojoT ? =
90+ jsonToPojo(
91+ jsonString.pojoType(),
92+ jsonString.jsonString(),
93+ jsonString.customAdapters(),
94+ jsonString.customAdaptersWithType(),
95+ jsonString.typesToSkip()
96+ )
97+
5998inline fun <reified PojoT : Any > jsonToPojo (
6099 jsonStr : String ,
61100 customAdapters : List <Any > = emptyList(),
62101 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
63- typesToIgnore : Set <Class <out Any >> = emptySet()
102+ typesToSkip : Set <Class <out Any >> = emptySet()
64103): PojoT ? =
65- jsonToPojo(PojoT ::class .java, jsonStr, customAdapters, customAdaptersWithType, typesToIgnore )
104+ jsonToPojo(PojoT ::class .java, jsonStr, customAdapters, customAdaptersWithType, typesToSkip )
66105
67106/* *
68107 * Generate a JSON string from a POJO object.
@@ -71,7 +110,7 @@ inline fun <reified PojoT : Any> jsonToPojo(
71110 * @param pojo The POJO object to be converted to JSON.
72111 * @param customAdapters A list of custom adapters for Moshi to use during the conversion.
73112 * @param customAdaptersWithType A map of custom adapters with their respective types.
74- * @param typesToIgnore A set of classes to ignore during the conversion.
113+ * @param typesToSkip A set of classes to ignore during the conversion.
75114 * @param indent An optional string for pretty-printing the JSON output.
76115 * @param <PojoT> The type of the POJO object.
77116 * @return A JSON string or null if the input is null.
@@ -82,30 +121,107 @@ fun <PojoT : Any> pojoToJson(
82121 pojo : PojoT ,
83122 customAdapters : List <Any > = emptyList(),
84123 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
85- typesToIgnore : Set <Class <out Any >> = emptySet(),
124+ typesToSkip : Set <Class <out Any >> = emptySet(),
86125 indent : String? = " "
87126): String? {
88- val jsonAdapter =
89- initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToIgnore, pojoType)
127+ val jsonAdapter = initMoshi<PojoT >(customAdapters, customAdaptersWithType, typesToSkip, pojoType)
90128 return (indent?.let { jsonAdapter.indent(indent) } ? : jsonAdapter).toJson(pojo)
91129}
92130
131+ fun <PojoT : Any > pojoToJson (config : Pojo <PojoT >): String? =
132+ pojoToJson(
133+ config.pojoType(),
134+ config.pojo(),
135+ config.customAdapters(),
136+ config.customAdaptersWithType(),
137+ config.typesToSkip(),
138+ config.indent()
139+ )
140+
93141inline fun <reified PojoT : Any > pojoToJson (
94142 pojo : PojoT ,
95143 customAdapters : List <Any > = emptyList(),
96144 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
97- typesToIgnore : Set <Class <out Any >> = emptySet(),
145+ typesToSkip : Set <Class <out Any >> = emptySet(),
98146 indent : String? = " "
99147): String? =
100- pojoToJson(PojoT ::class .java, pojo, customAdapters, customAdaptersWithType, typesToIgnore , indent)
148+ pojoToJson(PojoT ::class .java, pojo, customAdapters, customAdaptersWithType, typesToSkip , indent)
101149
102150@SuppressWarnings(" kotlin:S3923" )
103151private fun <PojoT : Any > initMoshi (
104152 customAdapters : List <Any > = emptyList(),
105153 customAdaptersWithType : Map <Type , List <Either <JsonAdapter <Any >, Factory >>> = emptyMap(),
106- typesToIgnore : Set <Class <out Any >> = emptySet(),
154+ typesToSkip : Set <Class <out Any >> = emptySet(),
107155 pojoType : Type
108156): JsonAdapter <PojoT > {
109- val moshiBuilder = buildMoshi(customAdapters, customAdaptersWithType, typesToIgnore )
157+ val moshiBuilder = buildMoshi(customAdapters, customAdaptersWithType, typesToSkip )
110158 return moshiBuilder.build().adapter(pojoType)
111159}
160+
161+ @PojoConfig
162+ @Value.Immutable
163+ internal interface PojoDef <PojoT > {
164+ fun pojoType (): Type
165+
166+ fun pojo (): PojoT
167+
168+ fun customAdapters (): List <Any >
169+
170+ fun customAdaptersWithType (): Map <Type , List <Either <JsonAdapter <Any >, Factory>>>
171+
172+ fun typesToSkip (): Set <Class <out Any >>
173+
174+ @Value.Default fun indent (): String = " "
175+ }
176+
177+ @Target(AnnotationTarget .CLASS )
178+ @Retention(AnnotationRetention .SOURCE )
179+ @Value.Style (
180+ typeImmutable = " *" ,
181+ typeAbstract = [" *Def" ],
182+ builder = " marshall" ,
183+ build = " done" ,
184+ depluralize = true ,
185+ add = " *" ,
186+ put = " *" ,
187+ with = " override*" ,
188+ visibility = Value .Style .ImplementationVisibility .PUBLIC ,
189+ )
190+ private annotation class PojoConfig
191+
192+ internal interface JsonConfig <PojoT > {
193+ fun pojoType (): Type
194+
195+ fun customAdapters (): List <Any >
196+
197+ fun customAdaptersWithType (): Map <Type , List <Either <JsonAdapter <Any >, Factory>>>
198+
199+ fun typesToSkip (): Set <Class <out Any >>
200+ }
201+
202+ @ConfigForJson
203+ @Value.Immutable
204+ internal interface JsonFileDef <PojoT > : JsonConfig <PojoT > {
205+ fun jsonFilePath (): String
206+ }
207+
208+ @ConfigForJson
209+ @Value.Immutable
210+ internal interface JsonStringDef <PojoT > : JsonConfig <PojoT > {
211+ fun jsonString (): String
212+ }
213+
214+ @Target(AnnotationTarget .CLASS )
215+ @Retention(AnnotationRetention .SOURCE )
216+ @Value.Style (
217+ typeImmutable = " *" ,
218+ typeAbstract = [" *Def" ],
219+ builder = " unmarshall" ,
220+ build = " done" ,
221+ depluralize = true ,
222+ add = " *" ,
223+ put = " *" ,
224+ with = " override*" ,
225+ visibility = Value .Style .ImplementationVisibility .PUBLIC ,
226+ )
227+ private annotation class ConfigForJson
0 commit comments