->jsexpr: Generics for converting data to JSON
This library provides a generic interface (with defaults) for converting common Racket data to JSON.
1 Reference
| (require json/to-jsexpr) | package: to-jsexpr-lib |
interface
procedure
(to-jsexpr? v) → boolean?
v : any/c
A struct implementing gen:to-jsexpr must provide an implementation for ->jsexpr.
procedure
v : to-jsexpr?
If v is a hash, its keys must all be symbols. Otherwise, a contract error is raised. For any container types listed above, the ->jsexpr is applied recursively to v’s children.
When you can’t make a value conform to gen:to-jsexpr, use add-to-jsexpr-fallback! to register a fallback handler for those types of values.
procedure
(add-to-jsexpr-fallback! predicate convert) → void?
predicate : (-> any/c boolean?) convert : (-> any/c jsexpr?)
syntax
(define-struct->jsexpr struct-id maybe-convention)
maybe-convention =
| #:convention convention-id
> (require json/to-jsexpr)
> (struct example1 (field-a field-b) #:methods gen:to-jsexpr [(define-struct->jsexpr example1)]) > (->jsexpr (example1 'a 42)) '#hasheq((fieldA . "a") (fieldB . 42))
> (struct example2 (field-a field-b) #:methods gen:to-jsexpr [(define-struct->jsexpr example2 #:convention snake_case-convention)]) > (->jsexpr (example2 'a 42)) '#hasheq((field_a . "a") (field_b . 42))
syntax
syntax
syntax