4.71. kink/json/JSON¶
Decoding from / encoding to JSON.
This mod supports JSON specified in RFC 8259.
https://tools.ietf.org/html/rfc8259
4.71.1. JSON.parse(Json ...[$config={}])¶
JSON.parse decodes a JSON value from the Json str.
See kink/json/JSON_VAL mod for JSON values.
Preconditions:
• Json must be a str
• $config must be a fun which takes a parse_config arg
If Json is a valid JSON str, the invocation of .parse continues to the success continuation configured by `on_success`.
If Json is not a valid JSON, the invocation of .parse continues to the error continuation configured by `on_error`.
Example:
:JSON.require_from('kink/json/')
stdout.print_line(JSON.parse('{"name": "Hakuho", "birthyear": 1985}').repr)
# => Flat_map("birthyear" 1985; "name" "Hakuho")
:analyze_json <- {(:Json)
JSON.parse(Json){(:P)
P.on_success{(:Val)
'parsed as {}'.format(Val.repr)
}
P.on_error{(:Msg :Pos)
'error: {} at {}'.format(Msg Pos)
}
}
}
stdout.print_line(analyze_json('42'))
# => parsed as 42
stdout.print_line(analyze_json('broken!'))
# => error: unexpected character at 0
§§ Scale of the number
The scale of the number in the result JSON values is normalized to the minimum non-negative number which can represent the number in the JSON.
Example:
:JSON.require_from('kink/json/')
stdout.print_line(JSON.parse('42.000').repr) # => 42
stdout.print_line(JSON.parse('1.23e+5').repr) # => 12300
4.71.2. type parse_config¶
Config val type of JSON.parse.
4.71.2.1. C.on_success($success_cont)¶
`on_success` sets $success_cont as the success continuation of the invocation of JSON.parse.
Precondition:
• $success_cont must be a fun which takes a JSON value
If this method is not called, VAL.identity is used as the success continuation.
4.71.2.2. C.on_error($error_cont)¶
`on_error` sets $error_cont as the error continuation of the invocation of JSON.parse.
Precondition:
• $error_cont must be a fun which takes (Msg, Pos), where Msg is an error message, and Pos is the pos index of the error in the JSON str.
If this method is not called, the default erro continuation, which raises an exception for the error, is used.
4.71.3. JSON.stringify(Json_val ...[$config={}])¶
JSON.stringify converts a Json_val to JSON.
Preconditions:
• Json_val must be a JSON value. See kink/json/JSON_VAL
• $config must be a fun which takes a stringify_config
Example:
:JSON.require_from('kink/json/')
:FLAT_MAP.require_from('kink/container/')
:Json_val <- FLAT_MAP.of('nums' [1 2 3])
stdout.print_line(JSON.stringify(Json_val))
# Output:
# {
# "nums": [
# 1,
# 2,
# 3
# ]
# }
stdout.print_line(
JSON.stringify(Json_val){(:S)
S.compact
}
)
# Output:
# {"nums":[1,2,3]}
4.71.4. type stringify_config¶
Config val of JSON.stringify.
By default, JSON.stringify outputs JSON with the following setup:
• pretty print mode: put a newline for each entry of arrays and objects
• indent by 2 whitespace characters for each nest
• one whitespace character after ":"
4.71.4.1. C.single_line¶
Activate single line mode, which does not put newline for entries of arrays and objects.
4.71.4.2. C.indent(Indent)¶
Add Indent as an indent for each nest.
Precondition:
• Indent must be a str
The default value is ' ' (two whitespace characters).
4.71.4.3. C.after_colon(After_colon)¶
Put After_colon after ":".
Precondition:
• After_colon must be a str
The default value is ' ' (one whitespace character).
4.71.4.4. C.after_comma(After_comma)¶
Put After_comma after ",", when single line mode is activated.
Precondition:
• After_comma must be a str
The default value is ' ' (one whitespace character).
4.71.4.5. C.compact¶
Omit whitespace and newline characters from the output.
Calling this method is equivalent to calling the following:
• .single_line
• .after_colon('')
• .after_comma('')