• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.serialization.cbor
2 
3 import kotlinx.serialization.cbor.internal.SuppressAnimalSniffer
4 import kotlinx.serialization.*
5 
6 /**
7  * Specifies that a property shall be tagged and the tag is serialized as CBOR major type 6: optional semantic tagging
8  * of other major types.
9  *
10  * Example usage:
11  *
12  * ```
13  * @Serializable
14  * data class Data(
15  *     @ValueTags(1337uL)
16  *     @ByteString
17  *     val a: ByteArray, // CBOR major type 6 1337(major type 2: a byte string).
18  *
19  *     @ValueTags(1234567uL)
20  *     val b: ByteArray  // CBOR major type 6 1234567(major type 4: an array of data items).
21  * )
22  * ```
23  *
24  * See [RFC 8949 3.4. Tagging of Items](https://datatracker.ietf.org/doc/html/rfc8949#name-tagging-of-items).
25  */
26 @SerialInfo
27 @Target(AnnotationTarget.PROPERTY)
28 @ExperimentalSerializationApi
29 @SuppressAnimalSniffer
30 public annotation class ValueTags(@OptIn(ExperimentalUnsignedTypes::class) vararg val tags: ULong)
31 
32 /**
33  * Contains a set of predefined tags, named in accordance with
34  * [RFC 8949 3.4. Tagging of Items](https://datatracker.ietf.org/doc/html/rfc8949#name-tagging-of-items)
35  */
36 public object CborTag{
37     public const val DATE_TIME_STANDARD: ULong = 0u;
38     public const val DATE_TIME_EPOCH: ULong = 1u;
39     public const val BIGNUM_POSITIVE: ULong = 2u;
40     public const val BIGNUM_NEGAIVE: ULong = 3u;
41     public const val DECIMAL_FRACTION: ULong = 4u;
42     public const val BIGFLOAT: ULong = 5u;
43     public const val BASE64_URL: ULong = 21u;
44     public const val BASE64: ULong = 22u;
45     public const val BASE16: ULong = 23u;
46     public const val CBOR_ENCODED_DATA: ULong = 24u;
47     public const val URI: ULong = 32u;
48     public const val STRING_BASE64_URL: ULong = 33u;
49     public const val STRING_BASE64: ULong = 34u;
50     public const val REGEX: ULong = 35u;
51     public const val MIME_MESSAGE: ULong = 36u;
52     public const val CBOR_SELF_DESCRIBE: ULong = 55799u;
53 }
54 
55 /**
56  * Specifies that a key (i.e. a property identifier) shall be tagged and serialized as CBOR major type 6: optional
57  * semantic tagging of other major types.
58  *
59  * Example usage:
60  *
61  * ```
62  * @Serializable
63  * data class Data(
64  *     @KeyTags(34uL)
65  *     val b: Int = -1   // results in the CBOR equivalent of 34("b"): -1
66  * )
67  * ```
68  *
69  * See [RFC 8949 3.4. Tagging of Items](https://datatracker.ietf.org/doc/html/rfc8949#name-tagging-of-items).
70  */
71 @SerialInfo
72 @Target(AnnotationTarget.PROPERTY)
73 @ExperimentalSerializationApi
74 @SuppressAnimalSniffer
75 public annotation class KeyTags(@OptIn(ExperimentalUnsignedTypes::class) vararg val tags: ULong)
76 
77 
78 
79 /**
80  * Specifies that an object of a class annotated using `ObjectTags` shall be tagged and serialized as
81  * CBOR major type 6: optional semantic tagging of other major types. Can be combined with [CborArray] and [ValueTags].
82  * Note that `ObjectTags` will always be encoded directly before to the data of the tagged object, i.e. a value-tagged
83  * property of an object-tagged type will have the value tags preceding the object tags.
84  *
85  * Example usage:
86  *
87  * ```
88  * @ObjectTags(1337uL)
89  * @Serializable
90  * data class ClassAsTagged(
91  *     @SerialName("alg")
92  *     val alg: Int,
93  * )
94  * ```
95  *
96  * Encoding to CBOR results in the following byte string:
97  * ```
98  * D9 0539         # tag(1337)
99  *    BF           # map(*)
100  *       63        # text(3)
101  *          616C67 # "alg"
102  *       13        # unsigned(19)
103  *       FF        # primitive(*)
104  * ```
105  *
106  * See [RFC 8949 3.4. Tagging of Items](https://datatracker.ietf.org/doc/html/rfc8949#name-tagging-of-items).
107  */
108 @SerialInfo
109 @Target(AnnotationTarget.CLASS)
110 @ExperimentalSerializationApi
111 @SuppressAnimalSniffer
112 public annotation class ObjectTags(@OptIn(ExperimentalUnsignedTypes::class) vararg val tags: ULong)
113 
114