mirror of
https://github.com/enso-org/enso.git
synced 2024-12-25 05:02:40 +03:00
Update Json to_text (#1368)
rewrite Json.to_text using fewer allocations
This commit is contained in:
parent
4cc36e8c81
commit
8591784b0a
@ -64,7 +64,7 @@ type Json
|
|||||||
|
|
||||||
## Renders this JSON into an RFC-8259 compliant text.
|
## Renders this JSON into an RFC-8259 compliant text.
|
||||||
to_text : Text
|
to_text : Text
|
||||||
to_text = Internal.render_helper this
|
to_text = Internal.render_helper "" this
|
||||||
|
|
||||||
## Recursively unwraps the JSON value into primitive values.
|
## Recursively unwraps the JSON value into primitive values.
|
||||||
unwrap : Any
|
unwrap : Any
|
||||||
|
@ -113,22 +113,34 @@ mk_consumer =
|
|||||||
Consumer child val
|
Consumer child val
|
||||||
|
|
||||||
## Helper method for printing JSON values to Text.
|
## Helper method for printing JSON values to Text.
|
||||||
render_helper json = case json of
|
render_helper builder json = case json of
|
||||||
Object fields ->
|
Object fields ->
|
||||||
fs = fields.to_vector.map item->
|
r = Ref.new ""
|
||||||
key = item.at 0
|
render_key_value acc key value =
|
||||||
value = item.at 1
|
separator = Ref.get r
|
||||||
value_json = here.render_helper value
|
Ref.put r ","
|
||||||
key_json = Printer.json_escape [key]
|
val = here.render_helper "" value
|
||||||
key_json + ":" + value_json
|
acc + separator + (Printer.json_escape [key]) + ":" + val
|
||||||
"{" + (fs.join ",") + "}"
|
arr = fields.fold_with_key "" render_key_value
|
||||||
|
builder + "{" + arr + "}"
|
||||||
Array items ->
|
Array items ->
|
||||||
its = items.map here.render_helper
|
r = Ref.new ""
|
||||||
"[" + (its.join ",") + "]"
|
render_array_element acc element =
|
||||||
String value -> Printer.json_escape [value]
|
separator = Ref.get r
|
||||||
Number value -> value.to_text
|
Ref.put r ","
|
||||||
Boolean value -> if value then "true" else "false"
|
val = here.render_helper "" element
|
||||||
Null -> "null"
|
acc + separator + val
|
||||||
|
arr = items.fold "" render_array_element
|
||||||
|
builder + "[" + arr + "]"
|
||||||
|
String value ->
|
||||||
|
builder + (Printer.json_escape [value])
|
||||||
|
Number value ->
|
||||||
|
builder + value.to_text
|
||||||
|
Boolean value ->
|
||||||
|
val = if value then "true" else "false"
|
||||||
|
builder + val
|
||||||
|
Null ->
|
||||||
|
builder + "null"
|
||||||
|
|
||||||
## Helper method for converting JSON objects into arbitrary types.
|
## Helper method for converting JSON objects into arbitrary types.
|
||||||
|
|
||||||
|
@ -163,8 +163,8 @@ type Map
|
|||||||
each_with_key function =
|
each_with_key function =
|
||||||
go map = case map of
|
go map = case map of
|
||||||
Bin _ k v l r ->
|
Bin _ k v l r ->
|
||||||
function k v
|
|
||||||
go l
|
go l
|
||||||
|
function k v
|
||||||
go r
|
go r
|
||||||
Nothing
|
Nothing
|
||||||
Tip -> Nothing
|
Tip -> Nothing
|
||||||
@ -174,7 +174,7 @@ type Map
|
|||||||
|
|
||||||
> Example
|
> Example
|
||||||
Summing all of the values in the map `m`.
|
Summing all of the values in the map `m`.
|
||||||
m.fold (+)
|
m.fold 0 (+)
|
||||||
fold : Any -> (Any -> Any -> Any) -> Any
|
fold : Any -> (Any -> Any -> Any) -> Any
|
||||||
fold init function =
|
fold init function =
|
||||||
go map init = case map of
|
go map init = case map of
|
||||||
@ -189,7 +189,7 @@ type Map
|
|||||||
|
|
||||||
> Example
|
> Example
|
||||||
Sum the keys and values in the map `m`.
|
Sum the keys and values in the map `m`.
|
||||||
m.fold_with_key (l -> k -> v -> l + k + v)
|
m.fold_with_key 0 (l -> k -> v -> l + k + v)
|
||||||
fold_with_key : Any -> (Any -> Any -> Any -> Any) -> Any
|
fold_with_key : Any -> (Any -> Any -> Any -> Any) -> Any
|
||||||
fold_with_key init function =
|
fold_with_key init function =
|
||||||
go map init = case map of
|
go map init = case map of
|
||||||
@ -235,4 +235,3 @@ type Map
|
|||||||
Tip -> Nothing
|
Tip -> Nothing
|
||||||
to_vector_with_builder this
|
to_vector_with_builder this
|
||||||
builder.to_vector
|
builder.to_vector
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user