Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit fea0c05

Browse files
committed
feat(cufyorg-json): newer consistent api
1 parent 21c4045 commit fea0c05

1 file changed

Lines changed: 104 additions & 17 deletions

File tree

cufyorg-json/src/commonMain/kotlin/ExtensionTransform.kt

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,138 @@ inline val JsonPrimitive.stringOrNull get() = if (isString) content else null
2828
//
2929

3030
/** if this json element is a json primitive, return it. Otherwise, return `null`. */
31-
inline val JsonElement.jsonPrimitiveOrNull get() = this as? JsonPrimitive
31+
@Deprecated("Use new API", ReplaceWith("asPrimitiveOrNull"))
32+
inline val JsonElement.jsonPrimitiveOrNull get() = asJsonPrimitiveOrNull
3233

3334
/** if this json element is a json object, return it. Otherwise, return `null`. */
34-
inline val JsonElement.jsonObjectOrNull get() = this as? JsonObject
35+
@Deprecated("Use new API", ReplaceWith("asObjectOrNull"))
36+
inline val JsonElement.jsonObjectOrNull get() = asJsonObjectOrNull
3537

3638
/** if this json element is a json array, return it. Otherwise, return `null`. */
37-
inline val JsonElement.jsonArrayOrNull get() = this as? JsonArray
39+
@Deprecated("Use new API", ReplaceWith("asJsonArrayOrNull"))
40+
inline val JsonElement.jsonArrayOrNull get() = asJsonArrayOrNull
41+
42+
//
43+
44+
/** if this json element is a json primitive, return it. Otherwise, fail. */
45+
inline val JsonElement.asJsonPrimitive: JsonPrimitive get() = this as JsonPrimitive
46+
47+
/** if this json element is a json primitive, return it. Otherwise, return `null`. */
48+
inline val JsonElement.asJsonPrimitiveOrNull: JsonPrimitive? get() = this as? JsonPrimitive
49+
50+
/** if this json element is a json object, return it. Otherwise, fail. */
51+
inline val JsonElement.asJsonObject: JsonObject get() = this as JsonObject
52+
53+
/** if this json element is a json object, return it. Otherwise, return `null`. */
54+
inline val JsonElement.asJsonObjectOrNull: JsonObject? get() = this as? JsonObject
55+
56+
/** if this json element is a json array, return it. Otherwise, fail. */
57+
inline val JsonElement.asJsonArray: JsonArray get() = this as JsonArray
58+
59+
/** if this json element is a json array, return it. Otherwise, return `null`. */
60+
inline val JsonElement.asJsonArrayOrNull: JsonArray? get() = this as? JsonArray
3861

3962
//
4063

4164
/** if this json element is a json string, return its content. Otherwise, fail. */
42-
inline val JsonElement.jsonPrimitiveString get() = jsonPrimitive.string
65+
@Deprecated("Use new API", ReplaceWith("asString"))
66+
inline val JsonElement.jsonPrimitiveString get() = asString
4367

4468
/** if this json element is a json string, return its content. Otherwise, return `null`. */
45-
inline val JsonElement.jsonPrimitiveStringOrNull get() = jsonPrimitiveOrNull?.stringOrNull
69+
@Deprecated("Use new API", ReplaceWith("asStringOrNull"))
70+
inline val JsonElement.jsonPrimitiveStringOrNull get() = asStringOrNull
4671

4772
/** if this json element is a json number, return its content as [Int]. Otherwise, fail. */
48-
inline val JsonElement.jsonPrimitiveInt get() = jsonPrimitive.int
73+
@Deprecated("Use new API", ReplaceWith("asInt"))
74+
inline val JsonElement.jsonPrimitiveInt get() = asInt
4975

5076
/** if this json element is a json number, return its content as [Int]. Otherwise, return `null`. */
51-
inline val JsonElement.jsonPrimitiveIntOrNull get() = jsonPrimitiveOrNull?.intOrNull
77+
@Deprecated("Use new API", ReplaceWith("asIntOrNull"))
78+
inline val JsonElement.jsonPrimitiveIntOrNull get() = asIntOrNull
5279

5380
/** if this json element is a json number, return its content as [Long]. Otherwise, fail. */
54-
inline val JsonElement.jsonPrimitiveLong get() = jsonPrimitive.long
81+
@Deprecated("Use new API", ReplaceWith("asLong"))
82+
inline val JsonElement.jsonPrimitiveLong get() = asLong
5583

5684
/** if this json element is a json number, return its content as [Long]. Otherwise, return `null`. */
57-
inline val JsonElement.jsonPrimitiveLongOrNull get() = jsonPrimitiveOrNull?.longOrNull
85+
@Deprecated("Use new API", ReplaceWith("asLongOrNull"))
86+
inline val JsonElement.jsonPrimitiveLongOrNull get() = asLongOrNull
5887

5988
/** if this json element is a json number, return its content as [Double]. Otherwise, fail. */
60-
inline val JsonElement.jsonPrimitiveDouble get() = jsonPrimitive.double
89+
@Deprecated("Use new API", ReplaceWith("asDouble"))
90+
inline val JsonElement.jsonPrimitiveDouble get() = asDouble
6191

6292
/** if this json element is a json number, return its content as [Double]. Otherwise, return `null`. */
63-
inline val JsonElement.jsonPrimitiveDoubleOrNull get() = jsonPrimitiveOrNull?.doubleOrNull
93+
@Deprecated("Use new API", ReplaceWith("asDoubleOrNull"))
94+
inline val JsonElement.jsonPrimitiveDoubleOrNull get() = asDoubleOrNull
6495

6596
/** if this json element is a json number, return its content as [Float]. Otherwise, fail. */
66-
inline val JsonElement.jsonPrimitiveFloat get() = jsonPrimitive.float
97+
@Deprecated("Use new API", ReplaceWith("asFloat"))
98+
inline val JsonElement.jsonPrimitiveFloat get() = asFloat
6799

68100
/** if this json element is a json number, return its content as [Float]. Otherwise, return `null`. */
69-
inline val JsonElement.jsonPrimitiveFloatOrNull get() = jsonPrimitiveOrNull?.floatOrNull
101+
@Deprecated("Use new API", ReplaceWith("asFloatOrNull"))
102+
inline val JsonElement.jsonPrimitiveFloatOrNull get() = asFloatOrNull
70103

71104
/** if this json element is a json boolean, return its content. Otherwise, fail. */
72-
inline val JsonElement.jsonPrimitiveBoolean get() = jsonPrimitive.boolean
105+
@Deprecated("Use new API", ReplaceWith("asBoolean"))
106+
inline val JsonElement.jsonPrimitiveBoolean get() = asBoolean
73107

74108
/** if this json element is a json boolean, return its content. Otherwise, return `null`. */
75-
inline val JsonElement.jsonPrimitiveBooleanOrNull get() = jsonPrimitiveOrNull?.booleanOrNull
109+
@Deprecated("Use new API", ReplaceWith("asBooleanOrNull"))
110+
inline val JsonElement.jsonPrimitiveBooleanOrNull get() = asBooleanOrNull
76111

77112
/** if this json element is a json primitive, return its raw content. Otherwise, fail. */
78-
inline val JsonElement.jsonPrimitiveContent get() = jsonPrimitive.content
113+
@Deprecated("Use new API", ReplaceWith("asContentString"))
114+
inline val JsonElement.jsonPrimitiveContent get() = asContentString
79115

80116
/** if this json element is a json primitive, return its raw content. Otherwise, return `null`. */
81-
inline val JsonElement.jsonPrimitiveContentOrNull get() = jsonPrimitiveOrNull?.contentOrNull
117+
@Deprecated("Use new API", ReplaceWith("asContentStringOrNull"))
118+
inline val JsonElement.jsonPrimitiveContentOrNull get() = asContentStringOrNull
119+
120+
//
121+
122+
/** if this json element is a json string, return its content. Otherwise, fail. */
123+
inline val JsonElement.asString: String get() = asJsonPrimitive.string
124+
125+
/** if this json element is a json string, return its content. Otherwise, return `null`. */
126+
inline val JsonElement.asStringOrNull: String? get() = asJsonPrimitiveOrNull?.stringOrNull
127+
128+
/** if this json element is a json number, return its content as [Int]. Otherwise, fail. */
129+
inline val JsonElement.asInt: Int get() = asJsonPrimitive.int
130+
131+
/** if this json element is a json number, return its content as [Int]. Otherwise, return `null`. */
132+
inline val JsonElement.asIntOrNull: Int? get() = asJsonPrimitiveOrNull?.intOrNull
133+
134+
/** if this json element is a json number, return its content as [Long]. Otherwise, fail. */
135+
inline val JsonElement.asLong: Long get() = asJsonPrimitive.long
136+
137+
/** if this json element is a json number, return its content as [Long]. Otherwise, return `null`. */
138+
inline val JsonElement.asLongOrNull: Long? get() = asJsonPrimitiveOrNull?.longOrNull
139+
140+
/** if this json element is a json number, return its content as [Double]. Otherwise, fail. */
141+
inline val JsonElement.asDouble: Double get() = asJsonPrimitive.double
142+
143+
/** if this json element is a json number, return its content as [Double]. Otherwise, return `null`. */
144+
inline val JsonElement.asDoubleOrNull: Double? get() = asJsonPrimitiveOrNull?.doubleOrNull
145+
146+
/** if this json element is a json number, return its content as [Float]. Otherwise, fail. */
147+
inline val JsonElement.asFloat: Float get() = asJsonPrimitive.float
148+
149+
/** if this json element is a json number, return its content as [Float]. Otherwise, return `null`. */
150+
inline val JsonElement.asFloatOrNull: Float? get() = asJsonPrimitiveOrNull?.floatOrNull
151+
152+
/** if this json element is a json boolean, return its content. Otherwise, fail. */
153+
inline val JsonElement.asBoolean: Boolean get() = asJsonPrimitive.boolean
154+
155+
/** if this json element is a json boolean, return its content. Otherwise, return `null`. */
156+
inline val JsonElement.asBooleanOrNull: Boolean? get() = asJsonPrimitiveOrNull?.booleanOrNull
157+
158+
/** if this json element is a json primitive, return its raw content. Otherwise, fail. */
159+
inline val JsonElement.asContentString: String get() = asJsonPrimitive.content
160+
161+
/** if this json element is a json primitive, return its raw content. Otherwise, return `null`. */
162+
inline val JsonElement.asContentStringOrNull: String? get() = asJsonPrimitiveOrNull?.contentOrNull
82163

83164
//
84165

@@ -92,6 +173,12 @@ fun JsonElement?.orJsonNull(): JsonElement {
92173
return this ?: JsonNull
93174
}
94175

176+
/** if this json element reference is not null, return it. Otherwise, return [JsonNull]. */
177+
inline val JsonPrimitive?.orJsonNull get() = this ?: JsonNull
178+
179+
/** if this json element reference is not null, return it. Otherwise, return [JsonNull]. */
180+
inline val JsonElement?.orJsonNull get() = this ?: JsonNull
181+
95182
//
96183

97184
/** Return a [JsonArray] instance by copying [this] list */

0 commit comments

Comments
 (0)