#parser #wasm

无 std lite-parser

简单的解析库。支持 Wasm / 无 std。

3 个不稳定版本

0.2.0 2022年7月5日
0.1.2 2020年6月16日
0.1.1 2020年6月3日

无标准库 中排名第 233

Download history 23025/week @ 2024-03-17 22357/week @ 2024-03-24 33345/week @ 2024-03-31 32781/week @ 2024-04-07 25923/week @ 2024-04-14 23946/week @ 2024-04-21 17554/week @ 2024-04-28 17273/week @ 2024-05-05 22389/week @ 2024-05-12 19349/week @ 2024-05-19 27287/week @ 2024-05-26 22415/week @ 2024-06-02 16594/week @ 2024-06-09 18933/week @ 2024-06-16 23219/week @ 2024-06-23 6630/week @ 2024-06-30

每月下载量 66,214
6 个 Crates 中使用(通过 lite-json

Apache-2.0 许可

13KB
341

lite-json

Crates.io GitHub

使用 Rust 编写的简单 JSON 解析器。支持 Wasm / 无 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 Crates 添加到 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);
}

依赖项