summaryrefslogtreecommitdiff
path: root/runtime/common/src/main/kotlin/kotlinx/serialization/json/JsonElements.kt
blob: 86be450f065cb9677bf569a98ec8cab9d50a9e0f (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("RedundantVisibilityModifier", "FunctionName")

package kotlinx.serialization.json

/**
 * Convenience method to create [JsonPrimitive] from given boolean.
 * Returns [JsonNull] if [value] is `null` or [JsonPrimitive] otherwise
 */
public fun JsonPrimitive(value: Boolean?): JsonPrimitive {
    if (value == null) return JsonNull
    return JsonLiteral(value)
}

/**
 * Convenience method to create [JsonPrimitive] from given number.
 * Returns [JsonNull] if [value] is `null` or [JsonPrimitive] otherwise
 */
public fun JsonPrimitive(value: Number?): JsonPrimitive {
    if (value == null) return JsonNull
    return JsonLiteral(value)
}

/**
 * Convenience method to create [JsonPrimitive] from given string literal.
 * Returns [JsonNull] if [value] is `null` or [JsonPrimitive] otherwise
 */
public fun JsonPrimitive(value: String?): JsonPrimitive {
    if (value == null) return JsonNull
    return JsonLiteral(value)
}

/**
 * Convenience method, returns content of current element as [Int]
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 * @throws NumberFormatException if current element is [JsonPrimitive] but it is not a valid representation of number
 */
public val JsonElement.int: Int get() = primitive.int

/**
 * Convenience method, returns content of current element as [Int] or `null` if element is not a valid representation of number
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.intOrNull: Int? get() = primitive.intOrNull

/**
 * Convenience method, returns content of current element as [Long]
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 * @throws NumberFormatException if current element is [JsonPrimitive] but it is not a valid representation of number
 */
public val JsonElement.long: Long get() = primitive.long

/**
 * Convenience method, returns content of current element as [Long] or `null` if element is not a valid representation of number
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.longOrNull: Long? get() = primitive.longOrNull

/**
 * Convenience method, returns content of current element as [Double]
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 * @throws NumberFormatException if current element is [JsonPrimitive] but it is not a valid representation of number
 */
public val JsonElement.double: Double get() = primitive.double

/**
 * Convenience method, returns content of current element as [Double] or `null` if element is not a valid representation of number
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.doubleOrNull: Double? get() = primitive.doubleOrNull

/**
 * Convenience method, returns content of current element as [Float]
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 * @throws NumberFormatException if current element is [JsonPrimitive], but it is not a valid representation of number
 */
public val JsonElement.float: Float get() = primitive.float

/**
 * Convenience method, returns content of current element as [Float] or `null` if element is not a valid representation of number
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.floatOrNull: Float? get() = primitive.floatOrNull

/**
 * Convenience method, returns content of current element as [Boolean]
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 * @throws IllegalStateException if current element is [JsonPrimitive], but it is not a valid boolean value
 */
public val JsonElement.boolean: Boolean get() = primitive.boolean

/**
 * Convenience method, returns content of current element as [Boolean] or `null` if element is not a valid boolean value
 *
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.booleanOrNull: Boolean? get() = primitive.booleanOrNull

/**
 * Convenience method, returns content of current element
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.content: String get() = primitive.content

/**
 * Convenience method, returns content of current element or `null` if element is [JsonNull]
 * @throws JsonElementTypeMismatchException if current element is not a [JsonPrimitive]
 */
public val JsonElement.contentOrNull: String? get() = primitive.contentOrNull