-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.rkt
32 lines (27 loc) · 1017 Bytes
/
json.rkt
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
#lang racket/base
(require (for-syntax racket/base)
json
racket/contract/base
racket/contract/region
racket/function
racket/string
"try.rkt")
(provide valid-json?
(all-from-out json))
;;======================================================================
;; See the Racket-standard 'json' module for more.
;;======================================================================
;; Verify that some JSON is valid. For strings it does
;; string->jsexpr and (if decoding succeeds) returns the result.
;; If decoding fails then valid-json? returns #f
(define/contract (valid-json? j)
(-> any/c (or/c #f jsexpr?))
(cond [(not (jsexpr? j)) #f]
[(non-empty-string? j)
(try [ (define result (string->jsexpr j))
(displayln result)
(cond [(eof-object? result) #f]
[else result])
]
[catch (identity (lambda (e) #f))])]
[else #t]))