aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/json.lux
blob: 71cde90f702b625881acdf42983506d052bdd1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(.module: {#.doc "Functionality for reading and writing values in the JSON format.

                  For more information, please see: http://www.json.org/"}
  [lux (#- Array)]
  (lux (control [monad (#+ do Monad)]
                [equivalence (#+ Equivalence)]
                codec
                ["p" parser "parser/" Monad<Parser>])
       (data [bool]
             [text "text/" Equivalence<Text> Monoid<Text>]
             (text ["l" lexer])
             [number "frac/" Codec<Text,Frac> "nat/" Codec<Text,Nat>]
             [maybe]
             ["e" error]
             [sum]
             [product]
             (collection [list "list/" Fold<List> Monad<List>]
                         [row (#+ Row row) "row/" Monad<Row>]
                         ["dict" dictionary (#+ Dictionary)]))
       [macro (#+ Monad<Meta> with-gensyms)]
       (macro ["s" syntax (#+ syntax:)]
              [code])))

(do-template [<name> <type>]
  [(type: #export <name> <type>)]

  [Null    Any]
  [Boolean Bool]
  [Number  Frac]
  [String  Text]
  )

(type: #export #rec JSON
  (#Null    Null)
  (#Boolean Boolean)
  (#Number  Number)
  (#String  String)
  (#Array   (Row JSON))
  (#Object  (Dictionary String JSON)))

(do-template [<name> <type>]
  [(type: #export <name> <type>)]

  [Array   (Row JSON)]
  [Object  (Dictionary String JSON)]
  )

(type: #export (Reader a)
  {#.doc "JSON reader."}
  (p.Parser (List JSON) a))

(syntax: #export (json token)
  {#.doc (doc "A simple way to produce JSON literals."
              (json true)
              (json 123.456)
              (json "Some text")
              (json #null)
              (json ["this" "is" "an" "array"])
              (json {"this" "is"
                     "an" "object"}))}
  (let [(^open) Monad<Meta>
        wrapper (function (_ x) (` (..json (~ x))))]
    (case token
      (^template [<ast-tag> <ctor> <json-tag>]
        [_ (<ast-tag> value)]
        (wrap (list (` (: JSON (<json-tag> (~ (<ctor> value))))))))
      ([#.Bool code.bool            #Boolean]
       [#.Frac code.frac            #Number]
       [#.Text code.text            #String])

      [_ (#.Tag ["" "null"])]
      (wrap (list (` (: JSON #Null))))

      [_ (#.Tuple members)]
      (wrap (list (` (: JSON (#Array (row (~+ (list/map wrapper members))))))))

      [_ (#.Record pairs)]
      (do Monad<Meta>
        [pairs' (monad.map @
                           (function (_ [slot value])
                             (case slot
                               [_ (#.Text key-name)]
                               (wrap (` [(~ (code.text key-name)) (~ (wrapper value))]))

                               _
                               (macro.fail "Wrong syntax for JSON object.")))
                           pairs)]
        (wrap (list (` (: JSON (#Object (dict.from-list text.Hash<Text> (list (~+ pairs')))))))))
      
      _
      (wrap (list token))
      )))

(def: #export (get-fields json)
  {#.doc "Get all the fields in a JSON object."}
  (-> JSON (e.Error (List String)))
  (case json
    (#Object obj)
    (#e.Success (dict.keys obj))

    _
    (#e.Error ($_ text/compose "Cannot get the fields of a non-object."))))

(def: #export (get key json)
  {#.doc "A JSON object field getter."}
  (-> String JSON (e.Error JSON))
  (case json
    (#Object obj)
    (case (dict.get key obj)
      (#.Some value)
      (#e.Success value)

      #.None
      (#e.Error ($_ text/compose "Missing field \"" key "\" on object.")))

    _
    (#e.Error ($_ text/compose "Cannot get field \"" key "\" of a non-object."))))

(def: #export (set key value json)
  {#.doc "A JSON object field setter."}
  (-> String JSON JSON (e.Error JSON))
  (case json
    (#Object obj)
    (#e.Success (#Object (dict.put key value obj)))

    _
    (#e.Error ($_ text/compose "Cannot set field \"" key "\" of a non-object."))))

(do-template [<name> <tag> <type> <desc>]
  [(def: #export (<name> key json)
     {#.doc (code.text ($_ text/compose "A JSON object field getter for " <desc> "."))}
     (-> Text JSON (e.Error <type>))
     (case (get key json)
       (#e.Success (<tag> value))
       (#e.Success value)

       (#e.Success _)
       (#e.Error ($_ text/compose "Wrong value type at key: " key))

       (#e.Error error)
       (#e.Error error)))]

  [get-boolean #Boolean Boolean "booleans"]
  [get-number  #Number  Number  "numbers"]
  [get-string  #String  String  "strings"]
  [get-array   #Array   Array   "arrays"]
  [get-object  #Object  Object  "objects"]
  )

(structure: #export _ (Equivalence JSON)
  (def: (= x y)
    (case [x y]
      [#Null #Null]
      true

      (^template [<tag> <struct>]
        [(<tag> x') (<tag> y')]
        (:: <struct> = x' y'))
      ([#Boolean bool.Equivalence<Bool>]
       [#Number  number.Equivalence<Frac>]
       [#String  text.Equivalence<Text>])

      [(#Array xs) (#Array ys)]
      (and (n/= (row.size xs) (row.size ys))
           (list/fold (function (_ idx prev)
                        (and prev
                             (maybe.default false
                                            (do maybe.Monad<Maybe>
                                              [x' (row.nth idx xs)
                                               y' (row.nth idx ys)]
                                              (wrap (= x' y'))))))
                      true
                      (list.indices (row.size xs))))
      
      [(#Object xs) (#Object ys)]
      (and (n/= (dict.size xs) (dict.size ys))
           (list/fold (function (_ [xk xv] prev)
                        (and prev
                             (case (dict.get xk ys)
                               #.None   false
                               (#.Some yv) (= xv yv))))
                      true
                      (dict.entries xs)))
      
      _
      false)))

############################################################
############################################################
############################################################

(def: unconsumed-input-error Text "Unconsumed JSON.")

(def: #export (run json parser)
  (All [a] (-> JSON (Reader a) (e.Error a)))
  (case (p.run (list json) parser)
    (#e.Success [remainder output])
    (case remainder
      #.Nil
      (#e.Success output)

      _
      (#e.Error unconsumed-input-error))
    
    (#e.Error error)
    (#e.Error error)))

(def: #export (fail error)
  (All [a] (-> Text (Reader a)))
  (function (_ inputs)
    (#e.Error error)))

(def: #export any
  {#.doc "Just returns the JSON input without applying any logic."}
  (Reader JSON)
  (<| (function (_ inputs))
      (case inputs
        #.Nil
        (#e.Error "Empty JSON stream.")
        
        (#.Cons head tail)
        (#e.Success [tail head]))))

(do-template [<name> <type> <tag> <desc>]
  [(def: #export <name>
     {#.doc (code.text ($_ text/compose "Reads a JSON value as " <desc> "."))}
     (Reader <type>)
     (do p.Monad<Parser>
       [head any]
       (case head
         (<tag> value)
         (wrap value)

         _
         (fail ($_ text/compose "JSON value is not " <desc> ".")))))]

  [null    Any #Null    "null"]
  [boolean Bool #Boolean "boolean"]
  [number  Frac #Number  "number"]
  [string  Text #String  "string"]
  )

(do-template [<test> <check> <type> <eq> <encoder> <tag> <desc> <pre>]
  [(def: #export (<test> test)
     {#.doc (code.text ($_ text/compose "Asks whether a JSON value is a " <desc> "."))}
     (-> <type> (Reader Bool))
     (do p.Monad<Parser>
       [head any]
       (case head
         (<tag> value)
         (wrap (:: <eq> = test (<pre> value)))

         _
         (fail ($_ text/compose "JSON value is not " <desc> ".")))))

   (def: #export (<check> test)
     {#.doc (code.text ($_ text/compose "Ensures a JSON value is a " <desc> "."))}
     (-> <type> (Reader Any))
     (do p.Monad<Parser>
       [head any]
       (case head
         (<tag> value)
         (let [value (<pre> value)]
           (if (:: <eq> = test value)
             (wrap [])
             (fail ($_ text/compose "Value mismatch: " (<encoder> test) "=/=" (<encoder> value)))))

         _
         (fail ($_ text/compose "JSON value is not a " <desc> ".")))))]

  [boolean? boolean! Bool bool.Equivalence<Bool>   (:: bool.Codec<Text,Bool> encode)   #Boolean "boolean" id]
  [number?  number!  Frac number.Equivalence<Frac> (:: number.Codec<Text,Frac> encode) #Number  "number"  id]
  [string?  string!  Text text.Equivalence<Text>   text.encode                         #String  "string"  id]
  )

(def: #export (nullable parser)
  (All [a] (-> (Reader a) (Reader (Maybe a))))
  (p.alt null
         parser))

(def: #export (array parser)
  {#.doc "Parses a JSON array, assuming that every element can be parsed the same way."}
  (All [a] (-> (Reader a) (Reader a)))
  (do p.Monad<Parser>
    [head any]
    (case head
      (#Array values)
      (case (p.run (row.to-list values) parser)
        (#e.Error error)
        (fail error)

        (#e.Success [remainder output])
        (case remainder
          #.Nil
          (wrap output)

          _
          (fail unconsumed-input-error)))

      _
      (fail "JSON value is not an array."))))

(def: #export (object parser)
  {#.doc "Parses a JSON object, assuming that every element can be parsed the same way."}
  (All [a] (-> (Reader a) (Reader (Dictionary Text a))))
  (do p.Monad<Parser>
    [head any]
    (case head
      (#Object object)
      (case (do e.Monad<Error>
              []
              (|> (dict.entries object)
                  (monad.map @ (function (_ [key val])
                                 (do @
                                   [val (run val parser)]
                                   (wrap [key val]))))
                  (:: @ map (dict.from-list text.Hash<Text>))))
        (#e.Success table)
        (wrap table)

        (#e.Error error)
        (fail error))

      _
      (fail "JSON value is not an array."))))

(def: #export (field field-name parser)
  {#.doc "Parses a field inside a JSON object."}
  (All [a] (-> Text (Reader a) (Reader a)))
  (do p.Monad<Parser>
    [head any]
    (case head
      (#Object object)
      (case (dict.get field-name object)
        (#.Some value)
        (case (run value parser)
          (#e.Success output)
          (function (_ tail)
            (#e.Success [(#.Cons (#Object (dict.remove field-name object))
                                 tail)
                         output]))

          (#e.Error error)
          (fail error))

        _
        (fail ($_ text/compose "JSON object does not have field \"" field-name "\".")))

      _
      (fail "JSON value is not an object."))))

############################################################
############################################################
############################################################

(def: (show-null _) (-> Null Text) "null")
(do-template [<name> <type> <codec>]
  [(def: <name> (-> <type> Text) <codec>)]

  [show-boolean Boolean (:: bool.Codec<Text,Bool> encode)]
  [show-number  Number (:: number.Codec<Text,Frac> encode)]
  [show-string  String text.encode])

(def: (show-array show-json elems)
  (-> (-> JSON Text) (-> Array Text))
  ($_ text/compose "["
      (|> elems (row/map show-json) row.to-list (text.join-with ","))
      "]"))

(def: (show-object show-json object)
  (-> (-> JSON Text) (-> Object Text))
  ($_ text/compose "{"
      (|> object
          dict.entries
          (list/map (function (_ [key value]) ($_ text/compose (show-string key) ":" (show-json value))))
          (text.join-with ","))
      "}"))

(def: (show-json json)
  (-> JSON Text)
  (case json
    (^template [<tag> <show>]
      (<tag> value)
      (<show> value))
    ([#Null    show-null]
     [#Boolean show-boolean]
     [#Number  show-number]
     [#String  show-string]
     [#Array   (show-array show-json)]
     [#Object  (show-object show-json)])
    ))

(def: space~
  (l.Lexer Text)
  (l.some l.space))

(def: data-sep
  (l.Lexer [Text Any Text])
  ($_ p.seq space~ (l.this ",") space~))

(def: null~
  (l.Lexer Null)
  (do p.Monad<Parser>
    [_ (l.this "null")]
    (wrap [])))

(do-template [<name> <token> <value>]
  [(def: <name>
     (l.Lexer Boolean)
     (do p.Monad<Parser>
       [_ (l.this <token>)]
       (wrap <value>)))]

  [t~ "true"  true]
  [f~ "false" false]
  )

(def: boolean~
  (l.Lexer Boolean)
  (p.either t~ f~))

(def: number~
  (l.Lexer Number)
  (do p.Monad<Parser>
    [signed? (l.this? "-")
     digits (l.many l.decimal)
     decimals (p.default "0"
                         (do @
                           [_ (l.this ".")]
                           (l.many l.decimal)))
     exp (p.default ""
                    (do @
                      [mark (l.one-of "eE")
                       signed?' (l.this? "-")
                       offset (l.many l.decimal)]
                      (wrap ($_ text/compose mark (if signed?' "-" "") offset))))]
    (case (frac/decode ($_ text/compose (if signed? "-" "") digits "." decimals exp))
      (#e.Error message)
      (p.fail message)
      
      (#e.Success value)
      (wrap value))))

(def: escaped~
  (l.Lexer Text)
  ($_ p.either
      (p.after (l.this "\\t") (parser/wrap "\t"))
      (p.after (l.this "\\b") (parser/wrap "\b"))
      (p.after (l.this "\\n") (parser/wrap "\n"))
      (p.after (l.this "\\r") (parser/wrap "\r"))
      (p.after (l.this "\\f") (parser/wrap "\f"))
      (p.after (l.this "\\\"") (parser/wrap "\""))
      (p.after (l.this "\\\\") (parser/wrap "\\"))))

(def: string~
  (l.Lexer String)
  (<| (l.enclosed ["\"" "\""])
      (loop [_ []])
      (do p.Monad<Parser>
        [chars (l.some (l.none-of "\\\""))
         stop l.peek])
      (if (text/= "\\" stop)
        (do @
          [escaped escaped~
           next-chars (recur [])]
          (wrap ($_ text/compose chars escaped next-chars)))
        (wrap chars))))

(def: (kv~ json~)
  (-> (-> Any (l.Lexer JSON)) (l.Lexer [String JSON]))
  (do p.Monad<Parser>
    [key string~
     _ space~
     _ (l.this ":")
     _ space~
     value (json~ [])]
    (wrap [key value])))

(do-template [<name> <type> <open> <close> <elem-parser> <prep>]
  [(def: (<name> json~)
     (-> (-> Any (l.Lexer JSON)) (l.Lexer <type>))
     (do p.Monad<Parser>
       [_ (l.this <open>)
        _ space~
        elems (p.sep-by data-sep <elem-parser>)
        _ space~
        _ (l.this <close>)]
       (wrap (<prep> elems))))]

  [array~  Array  "[" "]" (json~ [])  row.from-list]
  [object~ Object "{" "}" (kv~ json~) (dict.from-list text.Hash<Text>)]
  )

(def: (json~' _)
  (-> Any (l.Lexer JSON))
  ($_ p.alt null~ boolean~ number~ string~ (array~ json~') (object~ json~')))

(structure: #export _ (Codec Text JSON)
  (def: encode show-json)
  (def: decode (function (_ input) (l.run input (json~' [])))))