A feature-few, no-allocation JSON parser in `no_std` rust.

Related tags

Parsing microjson
Overview

Small JSON Parser in no_std

Build Status Latest Version Coverage Status

This library reads and parses JSON strings.

Its intended use case is to read a JSON payload once.

It does not serialise data.

Sample usage

Simply put this in your Cargo.toml:

[dependencies]
microjson = "0.1"

You can read strings and integers easily:

# use microjson::{JSONValue, JSONParsingError};
# fn main() -> Result<(), JSONParsingError> {
let integer = JSONValue::parse("42")?;

let value : isize = integer.read_integer()?;

let string = JSONValue::parse("\"hello there\"")?;

let value : &str = string.read_string()?;
# Ok(())
# }

You can read arrays like this:

# use microjson::{JSONValue, JSONParsingError};
# fn main() -> Result<(), JSONParsingError> {
let input = r#" [0, 1, 2, 3, 4, 5] "#;

let array = JSONValue::parse(input)?;

for (n, item) in array.iter_array()?.enumerate() {
    let value = item.read_integer()?;
    assert_eq!(value, n as isize);
}
# Ok(())
# }

And, of course, any combination of the above:

# use microjson::{JSONValue, JSONParsingError};
# fn main() -> Result<(), JSONParsingError> {
let input = r#" { "arr": [3, "foo", 3.625, false] } "#;

let object = JSONValue::parse(input)?;

assert_eq!(
    object.get_key_value("arr")?.iter_array()?.nth(2).unwrap().read_float()?,
    3.625
);
# Ok(())
# }

If you are unsure what kind of data you have, you can query the [JSONValueType].

# use microjson::{JSONValue, JSONValueType, JSONParsingError};
# fn main() -> Result<(), JSONParsingError> {
let input = r#" 3.1415 "#;

let object = JSONValue::parse(input)?;

match object.value_type {
    JSONValueType::String => {},
    JSONValueType::Number => {},
    JSONValueType::Object => {},
    JSONValueType::Array => {},
    JSONValueType::Bool => {},
    JSONValueType::Null => {},
}
# Ok(())
# }

Verifying Data

To load some JSON, you need only call

# use microjson::JSONValue;
let value = JSONValue::parse(r#" [1,2,3,5"foo"] "#);

However, this data is malformed. [JSONValue::parse] will return an Ok result, as to determine that the data was corrupt would require scanning through the entire string. The error would only be reported when you attempted to iterate to the fourth item and parse it as a value.

If you need to know that the data is sound, use [JSONValue::verify]. Alternatively, you can parse and verify in one step.

# use microjson::JSONValue;
let value = JSONValue::parse_and_verify(r#" [1,2,3,5"foo"] "#);

Features

  • All JSON types
  • Strings with escape sequences
  • Parse ints (using built in parser)
  • Parse floats (using built in parser)
  • Iterators over arrays
  • Object key lookup
  • Iterators over objects
  • Verify JSON
You might also like...
Soon to be AsciiDoc parser implemented in rust!

pagliascii "But ASCII Doc, I am Pagliascii" Soon to be AsciiDoc parser implemented in rust! This project is the current implementation of the requeste

PEG parser for YAML written in Rust 🦀

yaml-peg PEG parser (pest) for YAML written in Rust 🦀 Quick Start ⚡️ # Run cargo run -- --file example_files/test.yaml # Output { "xmas": "true",

This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

MRT/BGP data parser written in Rust.
MRT/BGP data parser written in Rust.

BGPKIT Parser BGPKIT Parser aims to provides the most ergonomic MRT/BGP message parsing Rust API. BGPKIT Parser has the following features: performant

This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST. Very early stage, do not use in production.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

A Gura parser for Rust

Gura Rust parser IMPORTANT: if you need to use Gura in a more user-friendly way, you have at your disposal Serde Gura which allows you to perform Seri

Front Matter parser for Rust.

fronma Front Matter parser for Rust. Usage Add this crate as a dependency: [dependencies] fronma = "~0.1" then use fronma::parser::parse to parse text

A Rust crate for LL(k) parser combinators.

oni-comb-rs (鬼昆布,おにこんぶ) A Rust crate for LL(k) parser combinators. Main project oni-comb-parser-rs Sub projects The following is projects implemented

gors is an experimental go toolchain written in rust (parser, compiler).

gors gors is an experimental go toolchain written in rust (parser, compiler). Install Using git This method requires the Rust toolchain to be installe

Comments
  • String lengths incorrect

    String lengths incorrect

    If a JSON payload containing simply a string and some whitespace such as "hello world" is parsed without checking, then calling read_string will return the tail of the entire input, including some whitespace and the close "

    bug 
    opened by rspencer01 1
  • String parsing incorrect

    String parsing incorrect

    We don't even try to do escape codes. Ideally we should end up with an iterator of chars for each JSONValueType::String typed data, much as arrays give iterators of JSONValues. A separate option for returning the raw string could be useful for stupid equality checks (eg checking that a parsed string is just "OK").

    bug 
    opened by rspencer01 0
  • Don't verify entire JSON by default

    Don't verify entire JSON by default

    Currently JSONValue::parse checks the entire JSON string before returning. It could just infer the type and return quickly. We can implement a check function if needed to verify the string

    enhancement 
    opened by rspencer01 0
Owner
Robert Spencer
Robert Spencer
Yet Another Parser library for Rust. A lightweight, dependency free, parser combinator inspired set of utility methods to help with parsing strings and slices.

Yap: Yet another (rust) parsing library A lightweight, dependency free, parser combinator inspired set of utility methods to help with parsing input.

James Wilson 117 Dec 14, 2022
Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Microformats 5 Jul 19, 2022
A native Rust port of Google's robots.txt parser and matcher C++ library.

robotstxt A native Rust port of Google's robots.txt parser and matcher C++ library. Native Rust port, no third-part crate dependency Zero unsafe code

Folyd 72 Dec 11, 2022
Rust parser combinator framework

nom, eating data byte by byte nom is a parser combinators library written in Rust. Its goal is to provide tools to build safe parsers without compromi

Geoffroy Couprie 7.6k Jan 7, 2023
Parsing Expression Grammar (PEG) parser generator for Rust

Parsing Expression Grammars in Rust Documentation | Release Notes rust-peg is a simple yet flexible parser generator that makes it easy to write robus

Kevin Mehall 1.2k Dec 30, 2022
A fast monadic-style parser combinator designed to work on stable Rust.

Chomp Chomp is a fast monadic-style parser combinator library designed to work on stable Rust. It was written as the culmination of the experiments de

Martin Wernstål 228 Oct 31, 2022
A parser combinator library for Rust

combine An implementation of parser combinators for Rust, inspired by the Haskell library Parsec. As in Parsec the parsers are LL(1) by default but th

Markus Westerlind 1.1k Dec 28, 2022
LR(1) parser generator for Rust

LALRPOP LALRPOP is a Rust parser generator framework with usability as its primary goal. You should be able to write compact, DRY, readable grammars.

null 2.4k Jan 7, 2023
A typed parser generator embedded in Rust code for Parsing Expression Grammars

Oak Compiled on the nightly channel of Rust. Use rustup for managing compiler channels. You can download and set up the exact same version of the comp

Pierre Talbot 138 Nov 25, 2022
Rust query string parser with nesting support

What is Queryst? This is a fork of the original, with serde and serde_json updated to 0.9 A query string parsing library for Rust inspired by https://

Stanislav Panferov 67 Nov 16, 2022