diff options
author | Eduardo Julian | 2021-09-09 00:29:12 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-09-09 00:29:12 -0400 |
commit | ef77466323f85a3d1b65b46a3deb93652ef22085 (patch) | |
tree | c2715b8cf6e7864fef87e22ee6e206c7c1758849 /stdlib/source/library/lux/data/format/json.lux | |
parent | 085c9a6ef151531cb01b842ed2f4366a49b78367 (diff) |
The old record syntax has been re-purposed as variant syntax.
Diffstat (limited to 'stdlib/source/library/lux/data/format/json.lux')
-rw-r--r-- | stdlib/source/library/lux/data/format/json.lux | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/stdlib/source/library/lux/data/format/json.lux b/stdlib/source/library/lux/data/format/json.lux index a7f22c7c0..052ce1c18 100644 --- a/stdlib/source/library/lux/data/format/json.lux +++ b/stdlib/source/library/lux/data/format/json.lux @@ -67,40 +67,47 @@ (-> (List [String JSON]) JSON) (|>> (dictionary.of_list text.hash) #..Object)) -(syntax: .public (json [token <code>.any]) - (let [(^open "[0]") ..monad - wrapper (function (_ x) (` (..json (~ x))))] - (case token - (^template [<ast_tag> <ctor> <json_tag>] - [[_ (<ast_tag> value)] - (in (list (` (: JSON (<json_tag> (~ (<ctor> value)))))))]) - ([#.Bit code.bit #..Boolean] - [#.Frac code.frac #..Number] - [#.Text code.text #..String]) - - [_ (#.Tag ["" "null"])] - (in (list (` (: JSON #..Null)))) - - [_ (#.Tuple members)] - (in (list (` (: JSON (#..Array ((~! row) (~+ (list\each wrapper members)))))))) - - [_ (#.Record pairs)] - (do [! ..monad] - [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'))))))))) - - _ - (in (list token))))) +(def: jsonP + (<code>.Parser JSON) + (<>.rec + (function (_ jsonP) + ($_ <>.or + (<code>.local_tag! "null") + <code>.bit + <code>.frac + <code>.text + (<>\each row.of_list + (<code>.tuple (<>.some jsonP))) + (<>\each (dictionary.of_list text.hash) + (<code>.variant (<>.some (<>.and <code>.text jsonP)))) + )))) + +(def: (jsonF token) + (-> JSON Code) + (case token + (#Null _) + (` #..Null) + + (^template [<ctor> <json_tag>] + [(<json_tag> value) + (` (<json_tag> (~ (<ctor> value))))]) + ([code.bit #..Boolean] + [code.frac #..Number] + [code.text #..String]) + + (#Array members) + (` (#..Array ((~! row.row) (~+ (row.list (row\each jsonF members)))))) + + (#Object pairs) + (` (#..Object ((~! dictionary.of_list) + (~! text.hash) + (list (~+ (|> pairs + dictionary.entries + (list\each (function (_ [key_name value]) + (` [(~ (code.text key_name)) (~ (jsonF value))]))))))))))) + +(syntax: .public (json [token ..jsonP]) + (in (list (` (: JSON (~ (jsonF token))))))) (def: .public (fields json) (-> JSON (Try (List String))) |