Skip to content

jsonkit.1.0.0

Choose a tag to compare

@pedrobslisboa pedrobslisboa released this 30 Jul 18:12
· 16 commits to main since this release

Rename the project from melange-json to jsonkit. The derivers, the runtime and the JSON Schema support all behave the same on native OCaml and on Melange, so the packages no longer lead with Melange branding: the native package takes the plain name, and the Melange package carries the suffix.

  • melange-json-native → jsonkit, melange-json-native.ppx → jsonkit.ppx
  • melange-json → jsonkit-melange, melange-json.ppx → jsonkit-melange.ppx
  • module Melange_json → Jsonkit

The exposed module is Jsonkit on both backends, so sources shared between a
native and a Melange build need no per-backend conditionals. These are new
opam packages, so numbering restarts: jsonkit 1.0.0 supersedes
melange-json 2.0.0 and contains everything in it, despite the lower number.

The melange-json packages will not be updated further. (#107)

Changes:

  • PPX: Creates a linter to warn about the wrong usage of [@json.name] on record fields
    and [@json.key] on variant constructors.
  • PPX: Adds a linter that catches namespaced json.* field attributes
    ([@json.name], [@json.key], [@json.default], etc.) on
    [@@deriving json, jsonschema] types, to use [@name],
    [@key], [@default], [@option] instead.
  • jsonschema: Remove the ~variant_as_string flag; use
    [@@jsonschema.compact_variants] to render payload-free constructors as
    plain string constants.
  • PPX: Respect the [@json.name "..."] attribute in of_json error
    messages.
  • PPX: Ignore extra JSON object fields by default in the native
    PPX, matching the Melange PPX. Add [@json.disallow_extra_fields] for
    records and inline records that should reject unknown keys.
  • PPX: [@@deriving jsonschema] now generates
    "additionalProperties": true for records by default, matching the JSON
    derivers which ignore unknown object keys. Add
    [@@jsonschema.disallow_extra_fields] (and
    [@jsonschema.disallow_extra_fields] on inline records in variants) to opt
    back into strict objects ("additionalProperties": false).
    [@@jsonschema.allow_extra_fields] is still accepted for backwards
    compatibility but is now a no-op; combining it with disallow_extra_fields
    is rejected.
  • Library: Add Jsonkit.unknown_variant_case, a record type with
    fields tag : string and payload : Jsonkit.t list option,
    meant to be referenced as the argument of a catch-all constructor (see
    next entry). Companion JSON-schema literal
    Jsonkit.unknown_variant_case_jsonschema plugs into
    ppx_deriving_jsonschema.
  • PPX: Add [@json.catch_all] attribute, marking a constructor as the
    catch-all for any unrecognised string tag. The constructor's argument
    is Jsonkit.unknown_variant_case; payload distinguishes bare
    strings (None) from array forms (Some xs), preserving the wire
    shape for round-trip-faithful decoding/encoding even when a future
    producer adds payload-bearing variants. Works on both classic variants
    and polymorphic variants. Pairs naturally with
    [@@json.compact_variants].
  • PPX: Add [@@json.compact_variants] attribute for variant and polyvariant
    types. Encodes constructors without arguments as plain JSON strings and
    constructors with arguments as JSON arrays ["ConstructorName", arg1, ...].
    The decoder now also accepts ["Tag"] (singleton array) as equivalent to
    "Tag" for nullary constructors, making the compact form a strict superset
    of the standard array encoding.
  • Support Ptype_open, e.g. type u = X.(x) [@@deriving json]
    (#60)
  • Add support for [@drop_default] with [@default] to omit fields
    from JSON output when their value matches the default. Equality is
    resolved via equal_<type> functions in scope by default, but a custom comparison
    function can be provided directly, e.g. [@json.drop_default Int.equal]
    (#77)
  • Add Jsonkit.equal for comparing two JSON values, and
    [@drop_default_if_json_equal] as an alternative to [@json.drop_default]
    that compares values at the JSON level
    (#77)
  • PPX: Fix unused-var warning with polyvars without own cases
    (#38)
  • PPX: Add support for labeled tuples, e.g. [%to_json: a:string * b:int],
    encoded as JSON objects like records