Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialisation/parsing of custom structures in JSON #60

Merged
merged 6 commits into from
Feb 6, 2022

Conversation

robotlolita
Copy link
Contributor

@robotlolita robotlolita commented Feb 5, 2022

This patch adds traits for types to describe how they can be serialised and reified when using a JSON encoding. The approach here favours control over reification to avoid issues with arbitrary code being able to construct capability-bearing types by simply stating their names; here the person describing the JSON types must provide a mapping for them that describes the lowering and the reification passes.

In order to support custom serialisation, the package forks JSON into standard JSON (json object), and "extended" JSON (extended-json) object. The extended JSON object stores non-standard types as the dictionary {"@type": "unique-tag", "value": ... }, it then uses the mapping contained in the extended JSON type to both serialise and reify these types.

Moving the extended JSON portion to a separate type avoids issues with JSON payloads that may use this convention in a different way, and also discourages a single object with all mappings.

Example usage:

let My-domain = #json-serialisation bare
  | tag: "project" type: #project
  | tag: "package" type: #pkg;
let Json = #extended-json with-serialisation: My-domain;

assert (Json parse: (Json serialise: new project("title", new pkg("name", "filename"))))
  === new project("title", new pkg("name", "filename"));

@robotlolita robotlolita added the c:stdlib Changes to the standard library label Feb 5, 2022
@robotlolita robotlolita added this to the Crochet 0.14 milestone Feb 5, 2022
@robotlolita robotlolita marked this pull request as ready for review February 5, 2022 23:46
@robotlolita robotlolita merged commit ea3770f into main Feb 6, 2022
@robotlolita robotlolita deleted the patch/json-reify branch February 6, 2022 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:stdlib Changes to the standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant