#json #parser #simple #wasm

no-std lite-json

Simple JSON parser. Wasm / no_std ready.

5 releases

0.2.0 Jul 5, 2022
0.1.3 Oct 26, 2020
0.1.2 Jun 16, 2020
0.1.1 Jun 3, 2020
0.1.0 Nov 29, 2019

#34 in No standard library

Download history 21191/week @ 2023-10-23 27008/week @ 2023-10-30 22136/week @ 2023-11-06 26546/week @ 2023-11-13 27163/week @ 2023-11-20 28147/week @ 2023-11-27 25949/week @ 2023-12-04 20309/week @ 2023-12-11 18026/week @ 2023-12-18 5994/week @ 2023-12-25 11492/week @ 2024-01-01 17131/week @ 2024-01-08 24274/week @ 2024-01-15 21640/week @ 2024-01-22 20388/week @ 2024-01-29 19031/week @ 2024-02-05

85,663 downloads per month
Used in 5 crates (4 directly)

Apache-2.0

56KB
1.5K SLoC

lite-json

Crates.io GitHub

Simple JSON parser written with Rust. Wasm / no_std ready.

How to Add in Cargo.toml

std

[dependencies]
lite-json = "0.2.0"

no_std

[dependencies]
lite-json = { version = "0.2.0", default-features = false, defaults = ["no_std"] }

Example Usage

Creating JSON

This example will create a lite-json structure, then print it as a JSON string.

use lite_json::Serialize;
use lite_json::json::{JsonValue, NumberValue};

fn main()
{
	// We will create a bunch of elements that we will put into a JSON Object.
	let mut object_elements = vec!();

	// Create a boolean value and add it to our vector.
	let boolean_value = true;
	let object_key = "boolean".chars().collect();
	object_elements.push((object_key, JsonValue::Boolean(boolean_value)));

	// Create an array value and add it to our vector.
	let array_value = vec!(JsonValue::Boolean(true), JsonValue::Boolean(false), JsonValue::Boolean(true));
	let object_key = "array".chars().collect();
	object_elements.push((object_key, JsonValue::Array(array_value)));

	// Create a string value and add it to our vector.
	let string_value = "Hello World!".chars().collect();
	let object_key = "string".chars().collect();
	object_elements.push((object_key, JsonValue::String(string_value)));

	// Create a number value and add it to our vector.
	let number_value = NumberValue
	{
		integer: 1234,
		fraction: 0,
		fraction_length: 0,
		exponent: 0,
	};
	let object_key = "number".chars().collect();
	object_elements.push((object_key, JsonValue::Number(number_value)));

	// Create a null value and add it to our vector.
	let object_key = "null".chars().collect();
	object_elements.push((object_key, JsonValue::Null));

	// Create the object value from the vector of elements.
	let object_value = JsonValue::Object(object_elements);

	// Convert the object to a JSON string.
	let json = object_value.format(4);
	let json_output = std::str::from_utf8(&json).unwrap();

	println!("{}", json_output);
}

This will output:

{
    "boolean": true,
    "array": [
        true,
        false,
        true
    ],
    "string": "Hello World!",
    "number": 1234,
    "null": null
}

Parsing JSON

This example will parse a JSON string into a lite-json structure.

use lite_json::json_parser::parse_json;

fn main()
{
	// This is the JSON string we will use.
	let json_string =
	r#"
		{
			"boolean": true,
			"array":
			[
				true,
				false,
				true
			],
			"string": "Hello World!",
			"number": 1234,
			"null": null
		}
	"#;

	// Parse the JSON and print the resulting lite-json structure.
	let json_data = parse_json(json_string).expect("Invalid JSON specified!");
	println!("{:?}", json_data);
}

Parsing JSON with Options

The parser options allows you to set the max depth of parsing nested objects. This code will result in an error because the max nest level is set to 1, but the depth of our JSON is 2 due to the presence of a nested array.

Note: This example requires the lite-parser crate to be added to Cargo.toml.

use lite_json::json_parser::parse_json_with_options;
use lite_parser::parser::ParserOptions;

fn main()
{
	// This is the JSON string we will use.
	let json_string =
	r#"
		{
			"boolean": true,
			"array":
			[
				true,
				false,
				true
			],
			"string": "Hello World!",
			"number": 1234,
			"null": null
		}
	"#;

	let parser_options = ParserOptions
	{
		max_nest_level: Some(1)
	};

	// Parse the JSON and print the resulting lite-json structure.
	let json_data = parse_json_with_options(json_string, parser_options).expect("Invalid JSON specified!");
	println!("{:?}", json_data);
}

Dependencies

~43KB