diff options
author | Eduardo Julian | 2021-08-24 05:23:45 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-08-24 05:23:45 -0400 |
commit | 36303d6cb2ce3ab9e36d045b9516c997bd461862 (patch) | |
tree | b9d2f1495143054d61d9af129f36833624db9dac /stdlib/source/library/lux/data/format/json.lux | |
parent | ec1f31b5a1492d5e0ab260397291d4449483bbd9 (diff) |
Outsourced the syntax for labelled type definitions to macros.
Diffstat (limited to 'stdlib/source/library/lux/data/format/json.lux')
-rw-r--r-- | stdlib/source/library/lux/data/format/json.lux | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/stdlib/source/library/lux/data/format/json.lux b/stdlib/source/library/lux/data/format/json.lux index bcfd44f80..6582b7402 100644 --- a/stdlib/source/library/lux/data/format/json.lux +++ b/stdlib/source/library/lux/data/format/json.lux @@ -41,12 +41,13 @@ ) (type: .public #rec JSON - (#Null Null) - (#Boolean Boolean) - (#Number Number) - (#String String) - (#Array (Row JSON)) - (#Object (Dictionary String JSON))) + (Variant + (#Null Null) + (#Boolean Boolean) + (#Number Number) + (#String String) + (#Array (Row JSON)) + (#Object (Dictionary String JSON)))) (template [<name> <type>] [(type: .public <name> @@ -80,19 +81,19 @@ (in (list (` (: JSON #..Null)))) [_ (#.Tuple members)] - (in (list (` (: JSON (#..Array ((~! row) (~+ (list\map wrapper members)))))))) + (in (list (` (: JSON (#..Array ((~! row) (~+ (list\each wrapper members)))))))) [_ (#.Record pairs)] (do {! ..monad} - [pairs' (monad.map ! - (function (_ [slot value]) - (case slot - [_ (#.Text key_name)] - (in (` [(~ (code.text key_name)) (~ (wrapper value))])) - - _ - (meta.failure "Wrong syntax for JSON object."))) - pairs)] + [pairs' (monad.each ! + (function (_ [slot value]) + (case slot + [_ (#.Text key_name)] + (in (` [(~ (code.text key_name)) (~ (wrapper value))])) + + _ + (meta.failure "Wrong syntax for JSON object."))) + pairs)] (in (list (` (: JSON (#..Object ((~! dictionary.of_list) (~! text.hash) (list (~+ pairs'))))))))) @@ -107,7 +108,7 @@ (#try.Success (dictionary.keys obj)) _ - (#try.Failure ($_ text\compose "Cannot get the fields of a non-object.")))) + (#try.Failure ($_ text\composite "Cannot get the fields of a non-object.")))) (def: .public (field key json) (-> String JSON (Try JSON)) @@ -118,10 +119,10 @@ (#try.Success value) #.None - (#try.Failure ($_ text\compose "Missing field '" key "' on object."))) + (#try.Failure ($_ text\composite "Missing field '" key "' on object."))) _ - (#try.Failure ($_ text\compose "Cannot get field '" key "' on a non-object.")))) + (#try.Failure ($_ text\composite "Cannot get field '" key "' on a non-object.")))) (def: .public (has key value json) (-> String JSON JSON (Try JSON)) @@ -130,7 +131,7 @@ (#try.Success (#Object (dictionary.has key value obj))) _ - (#try.Failure ($_ text\compose "Cannot set field '" key "' on a non-object.")))) + (#try.Failure ($_ text\composite "Cannot set field '" key "' on a non-object.")))) (template [<name> <tag> <type>] [(def: .public (<name> key json) @@ -140,7 +141,7 @@ (#try.Success value) (#try.Success _) - (#try.Failure ($_ text\compose "Wrong value type at key: " key)) + (#try.Failure ($_ text\composite "Wrong value type at key: " key)) (#try.Failure error) (#try.Failure error)))] @@ -216,7 +217,7 @@ (|> raw (text.split_at 1) maybe.trusted product.right)))))) (def: escape "\") -(def: escaped_dq (text\compose ..escape text.double_quote)) +(def: escaped_dq (text\composite ..escape text.double_quote)) (def: string_format (-> String Text) @@ -240,14 +241,14 @@ (def: (array_format format) (-> (-> JSON Text) (-> Array Text)) - (|>> (row\map format) + (|>> (row\each format) row.list (text.interposed ..value_separator) (text.enclosed [..array_start ..array_end]))) (def: (kv_format format [key value]) (-> (-> JSON Text) (-> [String JSON] Text)) - ($_ text\compose + ($_ text\composite (..string_format key) ..entry_separator (format value) @@ -256,7 +257,7 @@ (def: (object_format format) (-> (-> JSON Text) (-> Object Text)) (|>> dictionary.entries - (list\map (..kv_format format)) + (list\each (..kv_format format)) (text.interposed ..value_separator) (text.enclosed [..object_start ..object_end]))) @@ -326,8 +327,8 @@ [mark (<text>.one_of "eE") signed?' (<>.parses? (<text>.this "-")) offset (<text>.many <text>.decimal)] - (in ($_ text\compose mark (if signed?' "-" "") offset))))] - (case (f\decoded ($_ text\compose (if signed? "-" "") digits "." decimals exp)) + (in ($_ text\composite mark (if signed?' "-" "") offset))))] + (case (f\decoded ($_ text\composite (if signed? "-" "") digits "." decimals exp)) (#try.Failure message) (<>.failure message) @@ -347,7 +348,7 @@ (<>\in text.carriage_return)) (<>.after (<text>.this "\f") (<>\in text.form_feed)) - (<>.after (<text>.this (text\compose "\" text.double_quote)) + (<>.after (<text>.this (text\composite "\" text.double_quote)) (<>\in text.double_quote)) (<>.after (<text>.this "\\") (<>\in "\")))) @@ -357,13 +358,13 @@ (<| (<text>.enclosed [text.double_quote text.double_quote]) (loop [_ []]) (do {! <>.monad} - [chars (<text>.some (<text>.none_of (text\compose "\" text.double_quote))) + [chars (<text>.some (<text>.none_of (text\composite "\" text.double_quote))) stop <text>.next]) (if (text\= "\" stop) (do ! [escaped escaped_parser next_chars (recur [])] - (in ($_ text\compose chars escaped next_chars))) + (in ($_ text\composite chars escaped next_chars))) (in chars)))) (def: (kv_parser json_parser) |