#json-parser #json #parser #wasm #simple

no-std lite-json

简单的JSON解析器。Wasm / no_std已就绪。

5个版本

0.2.0 2022年7月5日
0.1.3 2020年10月26日
0.1.2 2020年6月16日
0.1.1 2020年6月3日
0.1.0 2019年11月29日

#34 in 无标准库

Download history 22531/week @ 2024-02-29 20633/week @ 2024-03-07 20710/week @ 2024-03-14 25931/week @ 2024-03-21 27395/week @ 2024-03-28 31799/week @ 2024-04-04 28717/week @ 2024-04-11 25331/week @ 2024-04-18 19946/week @ 2024-04-25 19311/week @ 2024-05-02 18823/week @ 2024-05-09 18163/week @ 2024-05-16 28634/week @ 2024-05-23 24697/week @ 2024-05-30 16767/week @ 2024-06-06 19759/week @ 2024-06-13

93,482 每月下载量
用于 5 个包(4直接)

Apache-2.0

56KB
1.5K SLoC

lite-json

Crates.io GitHub

使用Rust编写的简单JSON解析器。Wasm / no_std已就绪。

如何在Cargo.toml中添加

std

[dependencies]
lite-json = "0.2.0"

no_std

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

示例用法

创建JSON

此示例将创建一个lite-json结构,然后将其打印为JSON字符串。

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);
}

这将输出

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

解析JSON

此示例将解析JSON字符串到lite-json结构。

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);
}

使用选项解析JSON

解析器选项允许您设置解析嵌套对象的深度限制。此代码将导致错误,因为最大嵌套级别设置为1,但由于存在嵌套数组,我们的JSON深度为2

注意:此示例需要将lite-parser包添加到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);
}

依赖项

~42KB