generic-json

Crates.iogeneric-json
lib.rsgeneric-json
version0.7.0
sourcesrc
created_at2021-09-23 13:24:14.782495
updated_at2022-01-14 13:35:32.858723
descriptionGeneric JSON traits
homepage
repositoryhttps://github.com/timothee-haudebourg/generic-json
max_upload_size
id455343
size65,092
Timothée Haudebourg (timothee-haudebourg)

documentation

https://docs.rs/generic-json

README

Generic JSON traits

Documentation Crate informations Repository

JSON is an ubiquitous format used in many applications. There is no single way of storing JSON values depending on the context, sometimes leading some applications to use multiples representations of JSON values in the same place. This can cause a problem for JSON processing libraries that should not care about the actual internal representation of JSON values, but are forced to stick to a particular format, leading to unwanted and costly conversions between the different formats.

This crate abstracts the JSON data structures defined in different library dealing with JSON such as json, serde_json, etc. The goal is to remove hard dependencies to these libraries when possible, and allow downstream users to choose its preferred library. It basically defines a trait Json and a ValueRef type abstracting away the implementation details.

The Json trait must be implemented by the JSON value type and defines what opaque types are used to represent each component of a JSON value. It also provides a function returning the value as a ValueRef enum type. Its simplified definition is as follows:

/// JSON model.
pub trait Json: Sized + 'static {
    /// Literal number type.
    type Number;

    /// String type.
    type String;

    /// Array type.
    type Array;

    /// Object key type.
    type Key;

    /// Object type.
    type Object;

    /// Returns a reference to this value as a `ValueRef`.
    fn value(&self) -> ValueRef<'_, Self>;

    /// Metadata type attached to each value.
    type MetaData;

    fn metadata(&self) -> &Self::MetaData;
}

The ValueRef exposes the structure of a reference to a JSON value:

pub enum ValueRef<'v, T: Json> {
    Null,
    Bool(bool),
    Number(&'v T::Number),
    String(&'v T::String),
    Array(&'v T::Array),
    Object(&'v T::Object)
}

In the same way, this crate also defines a ValueMut type for mutable references. This allows each implementor to have their own inner representation of values while allowing interoperability.

Foreign implementations

This library optionally provides implementations of the Json trait for the following foreign types, enabled by their associated feature.

Type Feature gate
serde_json::Value serde_json-impl
ijson::IValue ijson-impl

Trait aliases

When the nightly feature is enabled, this crate also defines some trait aliases that define common requirements for JSON data types. For instance the JsonClone trait alias ensures that every component of the JSON value implements Clone.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 30

cargo fmt