forked from Kotlin/kotlinx.serialization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialImplicits.kt
76 lines (61 loc) · 3.27 KB
/
SerialImplicits.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlinx.serialization.json.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.modules.*
import kotlin.RequiresOptIn.Level.*
/**
* This annotation marks declaration which try to obtain serializer implicitly
* using reflection, e.g. from KClass or instance itself.
*
* This approach is discouraged in general because it has several drawbacks, including:
* - Reflection is not available on Kotlin/Native and is very limited on Kotlin/JS.
* - Reflection won't infer correct serializers for generic classes, like collections.
* - SerialModule may not be available, since it is bound to particular format, not serializer.
* - Such reflection calls are usually slow.
*
* It's always better to specify serializer explicitly, using generated `.serializer()`
* function on serializable class' companion.
*/
@RequiresOptIn
public annotation class ImplicitReflectionSerializer
/**
* This annotation marks declarations with default parameters that are subject to semantic change without a migration path.
*
* For example, [JsonConfiguration.Default] marked as unstable, thus indicating that it can change its default values
* in the next releases (e.g. disable strict-mode by default), leading to a semantic (not source code level) change.
*/
@RequiresOptIn(level = WARNING)
public annotation class UnstableDefault
@ImplicitReflectionSerializer
public inline fun <reified T : Any> BinaryFormat.dump(value: T): ByteArray =
dump(context.getContextualOrDefault(T::class), value)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> BinaryFormat.dumps(value: T): String =
dumps(context.getContextualOrDefault(T::class), value)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> BinaryFormat.load(raw: ByteArray): T =
load(context.getContextualOrDefault(T::class), raw)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> BinaryFormat.loads(hex: String): T =
loads(context.getContextualOrDefault(T::class), hex)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> StringFormat.stringify(value: T): String =
stringify(context.getContextualOrDefault(T::class), value)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> StringFormat.stringify(objects: List<T>): String =
stringify(context.getContextualOrDefault(T::class).list, objects)
@ImplicitReflectionSerializer
public inline fun <reified K : Any, reified V : Any> StringFormat.stringify(map: Map<K, V>): String
= stringify(MapSerializer(context.getContextualOrDefault(K::class), context.getContextualOrDefault(V::class)), map)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> StringFormat.parse(str: String): T =
parse(context.getContextualOrDefault(T::class), str)
@ImplicitReflectionSerializer
public inline fun <reified T : Any> StringFormat.parseList(objects: String): List<T> =
parse(context.getContextualOrDefault(T::class).list, objects)
@ImplicitReflectionSerializer
public inline fun <reified K : Any, reified V : Any> StringFormat.parseMap(map: String)
= parse(MapSerializer(context.getContextualOrDefault(K::class), context.getContextualOrDefault(V::class)), map)