7.7
JSONSourcery
JSONSourcery is a library built on top of the json base package in order to add automatic and manual JSON serialization in order to enable Racket values to be used directly for computation with less boilerplate needed to convert to JSON as well as adding clarifying syntax macros.
(require json-sourcery) | package: json-sourcery |
1 JSON Serializers
syntax
(json-serializer-val predicate transformer)
predicate : (-> any? boolean)
transformer : (-> any? any?)
Create a JSON serializer for anything that passes the given predicate
using the given transformer
syntax
(json-serializer-obj predicate (name accessor) ...)
predicate : (-> any? boolean)
name : id
accessor : (-> any? any?)
Create a JSON serializer for anything that passes the given predicate
using the given names and accessors to create a JSON object jsexpr
syntax
(json-serializer-struct [struct-name struct?])
Create a JSON serializer for the given struct automaticially using all fields of the structure to
create a json-serializer-obj
procedure
(json-serializer? x) → boolean?
x : any?
Predicate for JSON serializers
2 Serialization
procedure
(json-serializable? expr serializers) → boolean?
expr : any? serializers : (listof json-serializer?)
Determines if the given expression is serializable with the given serializers. Errors with
malformed serializers with the apropriate predicate use of accessor error
procedure
(serialize-json expr serializers) → jsexpr?
expr : any? serializers : (listof json-serializer?)
Attempts to serialize the expression with the given serializers. Guaranteed to succeed on any
expression that passes json-serializable?
If a unserializable value is encountered, the entire function will return the symbol ’failure.
Errors with malformed serializers with the apropriate predicate use of accessor error.
procedure
(serialize-failure? expr) → boolean?
expr : any?
Semantic predicate for checking for failures (not errors) in serialize-json
3 JSON Object Shortcuts
A shortcut for creating JSON serializable objects with the given list of json-kv’s
procedure
key : string? value : json-serializable?
A semantic shortcut for creating JSON serializable pair in a list for a JSON object.
Equivalent to list.