Skip to content

Commit 7d5aa30

Browse files
authored
Merge pull request #173 from IZIVIA/feat/#170-kotlinx-serialization-support
2 parents 5035a39 + 95f36f9 commit 7d5aa30

File tree

23 files changed

+1394
-44
lines changed

23 files changed

+1394
-44
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.izivia.ocpi.toolkit.annotations
2+
3+
import kotlin.reflect.KClass
4+
5+
/**
6+
* Defines a custom serializer for a specific field within a class.
7+
*
8+
* This annotation is used in conjunction with [CustomFieldSerializer] to specify which serializer
9+
* should be used for a particular field when generating surrogate classes.
10+
*
11+
* @property name The name of the field (property) to apply the serializer to
12+
* @property serializer The KSerializer class to use for this field
13+
*
14+
* @see CustomFieldSerializer
15+
* @see GenerateSerializers
16+
*/
17+
@Target()
18+
@Retention(AnnotationRetention.SOURCE)
19+
annotation class FieldSerializer(
20+
val name: String,
21+
val serializer: KClass<*>,
22+
)
23+
24+
/**
25+
* Groups custom field serializers for a specific target class.
26+
*
27+
* This annotation allows you to specify multiple custom serializers for different fields
28+
* within a single class. It's particularly useful when you need fine-grained control
29+
* over how specific fields are serialized/deserialized.
30+
*
31+
* @property targetClass The class for which field serializers are being defined
32+
* @property fieldSerializer Array of [FieldSerializer] annotations defining custom serializers for specific fields
33+
*
34+
* @see FieldSerializer
35+
* @see GenerateSerializers
36+
*/
37+
@Target()
38+
@Retention(AnnotationRetention.SOURCE)
39+
annotation class CustomFieldSerializer(
40+
val targetClass: KClass<*>,
41+
val fieldSerializer: Array<FieldSerializer>,
42+
)
43+
44+
/**
45+
* Defines a custom serializer for a specific class type.
46+
*
47+
* When specified, this serializer will be used for all occurrences of the class type
48+
* across all generated serializers, unless overridden by a field-specific serializer.
49+
* This is particularly useful for:
50+
* - Enum classes with custom serialization logic
51+
* - Built-in types like BigDecimal, Instant, etc.
52+
* - Third-party classes that need custom serialization
53+
*
54+
* @property targetClass The class to apply the serializer to
55+
* @property serializer The KSerializer class to use for this class
56+
*
57+
* @see GenerateSerializers
58+
*/
59+
@Target()
60+
@Retention(AnnotationRetention.SOURCE)
61+
annotation class CustomClassSerializer(
62+
val targetClass: KClass<*>,
63+
val serializer: KClass<*>,
64+
)
65+
66+
/**
67+
* Generates kotlinx.serialization serializers for the specified classes using the surrogate pattern.
68+
*
69+
* This annotation triggers KSP processing to generate:
70+
* 1. A surrogate data class for each target class (with @Serializable)
71+
* 2. A serializer object that maps between the original class and its surrogate
72+
* 3. A SerializersModule containing all generated serializers
73+
*
74+
* The surrogate pattern is useful for:
75+
* - Adding serialization to classes you don't control (third-party libraries)
76+
* - Customizing serialization behavior without modifying the original class
77+
* - Applying custom serializers to specific fields or types
78+
*
79+
* @property classes Array of classes for which to generate serializers
80+
* @property excludedSerializersFromModule Array of classes whose serializers should not be
81+
* included in the generated SerializersModule
82+
* @property customClassSerializers Custom serializers for specific class types, applied globally across all
83+
* generated serializers unless overridden at the field level. Useful for enums, built-in types
84+
* (BigDecimal, Instant), and third-party classes.
85+
* @property customFieldSerializers Field-specific custom serializers, allowing fine-grained control
86+
* over how individual fields are serialized/deserialized
87+
*
88+
* @see CustomFieldSerializer
89+
* @see CustomClassSerializer
90+
*
91+
* Example usage:
92+
* ```
93+
* @GenerateSerializers(
94+
* classes = [
95+
* MyClass::class,
96+
* AnotherClass::class
97+
* ],
98+
* customClassSerializers = [
99+
* CustomClassSerializer(Status::class, StatusSerializer::class),
100+
* CustomClassSerializer(BigDecimal::class, BigDecimalSerializer::class),
101+
* CustomClassSerializer(Instant::class, InstantSerializer::class)
102+
* ],
103+
* customFieldSerializers = [
104+
* CustomFieldSerializer(
105+
* targetClass = MyClass::class,
106+
* fieldSerializer = [
107+
* FieldSerializer("specialField", CustomFieldSerializer::class)
108+
* ]
109+
* )
110+
* ]
111+
* )
112+
* class MySerializers
113+
* ```
114+
*/
115+
@Target(AnnotationTarget.CLASS)
116+
@Retention(AnnotationRetention.SOURCE)
117+
annotation class GenerateSerializers(
118+
val classes: Array<KClass<*>>,
119+
val excludedSerializersFromModule: Array<KClass<*>> = [],
120+
val customClassSerializers: Array<CustomClassSerializer> = [],
121+
val customFieldSerializers: Array<CustomFieldSerializer> = [],
122+
)

0 commit comments

Comments
 (0)