scoop:scoop.json:0.1.0
src/json.scoop
Selected production source · src/json.scoop
1
package scoop.json3
import scoop.collections.Map4
import scoop.collections.LinkedHashMap6
/**7
* JSON encoding and decoding through the ordinary [ValueEncoder] and [ValueDecoder] protocols.8
*9
* Use the explicit codec overloads for custom representations, or the companion overloads when the10
* type provides a codec. Malformed input and incompatible values report their path through11
* [DecodingException] or [EncodingException].12
*/13
public object Json {14
/** Parses one complete, strict JSON document into an immutable value tree. */15
public fun parse(text: String): JsonValue = JsonValue(JsonParser(text).parse())17
/** Renders an existing value tree directly, preserving number tokens and member order. */18
public fun stringify(value: JsonValue): String = renderJson(value.node)20
/**21
* Encodes one value as a complete JSON document. The companion overload requires the type22
* companion to implement [ValueEncoder]; the explicit overload uses the supplied codec.23
*/24
public fun <T> encodeToString(value: T): String25
where T.Companion : ValueEncoder<T> = encodeToString(value, T.Companion)27
/**28
* Parses one complete JSON document and decodes it using the selected codec. Invalid syntax,29
* trailing input, and a value incompatible with the codec fail with [DecodingException].30
*/31
public fun <T> decodeFromString(text: String): T32
where T.Companion : ValueDecoder<T> = decodeFromString(text, T.Companion)34
/**35
* Encodes one value as a complete JSON document. The companion overload requires the type36
* companion to implement [ValueEncoder]; the explicit overload uses the supplied codec.37
*/38
public fun <T> encodeToString(value: T, codec: ValueEncoder<T>): String =39
renderJson(encodeToValue(value, codec).node)41
/**42
* Parses one complete JSON document and decodes it using the selected codec. Invalid syntax,43
* trailing input, and a value incompatible with the codec fail with [DecodingException].44
*/45
public fun <T> decodeFromString(text: String, codec: ValueDecoder<T>): T =46
decodeFromValue(parse(text), codec)48
/** Encodes a value tree using the value's companion codec. */49
public fun <T> encodeToValue(value: T): JsonValue50
where T.Companion : ValueEncoder<T> = encodeToValue(value, T.Companion)52
/** Decodes a value tree as a new root using the requested type's companion codec. */53
public fun <T> decodeFromValue(value: JsonValue): T54
where T.Companion : ValueDecoder<T> = decodeFromValue(value, T.Companion)56
/**57
* Runs the codec into a JSON value tree that can be embedded in another JSON document without58
* converting through text.59
*/60
public fun <T> encodeToValue(value: T, codec: ValueEncoder<T>): JsonValue {61
val encoder = JsonEncoder("")62
codec.encode(value, encoder)63
return JsonValue(encoder.result())64
}66
/**67
* Decodes an existing JSON value tree using the supplied codec, preserving path information in68
* decoding errors.69
*/70
public fun <T> decodeFromValue(value: JsonValue, codec: ValueDecoder<T>): T =71
codec.decode(JsonDecoder(value.node, ""))73
/** Encodes String-key maps as JSON objects in map iteration order, using an explicit value codec. */74
public fun <V> objectEncoder(codec: ValueEncoder<V>): ValueEncoder<Map<String, V>> =75
JsonObjectEncoder<V>(codec)77
/** Decodes JSON objects into independent insertion-ordered maps, using an explicit value codec. */78
public fun <V> objectDecoder(codec: ValueDecoder<V>): ValueDecoder<LinkedHashMap<String, V>> =79
JsonObjectDecoder<V>(codec)81
/** Builds an object encoder using the value companion. */82
public fun <V> objectEncoder(): ValueEncoder<Map<String, V>>83
where V.Companion : ValueEncoder<V> = objectEncoder(V.Companion)85
/** Builds an object decoder using the value companion. */86
public fun <V> objectDecoder(): ValueDecoder<LinkedHashMap<String, V>>87
where V.Companion : ValueDecoder<V> = objectDecoder(V.Companion)88
}90
internal enum JsonNode {91
Null,92
BooleanValue(Boolean),93
Number(String),94
Text(String),95
Record(Map<String, JsonNode>),96
Sequence(ArrayList<JsonNode>)97
}100
internal fun childPath(path: String, name: String): String {101
val result = StringBuilder().append(path).append("/")102
for (character in name) {103
when (character) {104
case '~' -> { result.append("~0") }105
case '/' -> { result.append("~1") }106
else -> { result.append(character) }107
}108
}109
return result.toString()110
}