tinyjson is a library to parse/generate JSON format document

Rust stable toolchain (no dependency)

tinyjson

tinyjson is a library to parse/generate JSON format document.

Goals:

  • Using Stable APIs; using no experimental APIs, no compiler plugin
  • Reasonable simple JSON object interface
  • No crate dependencies at runtime
  • Well tested with famous JSON test suites
    • JSON checker in json.org
    • JSONTestSuite
    • JSON-Schema-Test-Suite
  • My Rust practice :)

Requirements

Rust stable toolchain (no dependency).

Installation

Add this crate to dependencies section of your Cargo.toml

Usage

Parse JSON

String is parsed to JsonValue struct via FromStr.

str::parse() is available. It parses the target as JSON and creates tinyjson::JsonValue object. It represents tree structure of parsed JSON. JsonValue is an enum struct and allocated on stack. So it doesn't require additional heap allocation.

Access to JSON Value

JsonValue is an enum value. So we can access it with match statement.

Each JSON types correspond to Rust types as follows:

JSON Rust
Number f64
Boolean bool
String String
Null ()
Array Vec<JsonValue>
Object HashMap<String, JsonValue>

JSON is a tree structure and it's boring to write nested match statement. So JsonValue implements std::ops::Index and std::ops::IndexMut traits in order to access to its nested values quickly.

Index access with &str key is available when the value is an object. And index access with usize is available when the value is an array. They return the &JsonValue value if target value was found. And modifying inner value directly with index access at right hand side of = is also available. Note that it can modify value of objects but cannot add new key. In both cases, it will call panic! when the value for key or the element of index was not found.

get() and get_mut() methods are provided to dereference the enum value (e.g. JsonValue::Number(4.2) -> 4.2). get() method returns its dereferenced raw value. It returns Option<&T> (T is corresponding value that you expected). If None is returned, it means its type mismatched with your expected one. Which type get() should dereference is inferred from how the returned value will be handled. So you don't need to specify it explicitly.

JsonValue implements TryInto. It can convert JsonValue into inner value.

Equality of JsonValue

JsonValue derives PartialEq traits hence it can be checked with == operator.

If you want to check its type only, there are is_xxx() shortcut methods in JsonValue instead of using match statement explicitly.

Generate JSON

stringify() method can be used to create JSON string.

Examples

Working examples are put in examples directory. They can be run with cargo run --example.

TODO

  • Parser
  • Generator
  • Equality of JsonValue
  • Index access to JsonValue (array, object)
  • Tests
  • Fuzzing

Repository

https://github.com/rhysd/tinyjson

Development

Tools:

  • clippy
  • rustfmt
  • cargo-fuzz

License

the MIT License

Information - Updated Jun 22, 2022

Stars: 73
Forks: 7
Issues: 0

Repositories & Extras

serde-json for no_std programs

MIT license (LICENSE-MIT or

serde-json for no_std programs
JSON

591

JSON parser which picks up values directly without performing tokenization in Rust

This JSON parser is implemented based on an abstract that utilizes in memory indexing and parsing

JSON parser which picks up values directly without performing tokenization in Rust
JSON

3.0K

Serde JSON  

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically

Serde JSON  

JSON-E Rust data-struct paramter crate for lightweight embedded content with objects and much more

What makes JSON-e unique is that it extensive documentation and ease of use

JSON-E Rust data-struct paramter crate for lightweight embedded content with objects and much more

jsonpointer_flatten

Rust library to flatten a JSON object using JSON Pointer field addressing as defined in

jsonpointer_flatten

JSON-RPC library designed for async/await in Rust

Designed to be the successor to tracking issue for next stable release (0

JSON-RPC library designed for async/await in Rust

Automatically generate type definitions and JSON encoders and JSON decoders for your Elm frontend from...

Automatically generate type definitions and JSON encoders and JSON decoders for your Elm frontend from your Rust backend types

Automatically generate type definitions and JSON encoders and JSON decoders for your Elm frontend from...
JSON

140

json_typegen - Rust types from JSON samples

json_typegen is a collection of tools for generating types from

json_typegen - Rust types from JSON samples

JSON File Parser

A CLI application that reads from a stream of JSON files, and computes some data-quality metrics

JSON File Parser
Facebook Instagram Twitter GitHub Dribbble
Privacy