Crates.io | ason |
lib.rs | ason |
version | 0.2.2 |
source | src |
created_at | 2024-03-19 06:16:51.967953 |
updated_at | 2024-06-24 09:39:15.133574 |
description | A data format designed to be easy for humans to read and write. |
homepage | https://hemashushu.github.io/works/xiaoxuan-script-object-notation |
repository | https://github.com/hemashushu/xiaoxuan-script-object-notation |
max_upload_size | |
id | 1178879 |
size | 215,893 |
XiaoXuan Script Object Notation (ANON) is a data format that is easy for humans to read and write. It is similar to JSON, but has many improvements and enhancements.
ANON is mainly used as a configuration file for applications, but can also be used for data transmission.
Features
Compatible with most of the syntax of JSON and JSON5. If you are already familiar with JSON, you do not need to learn a new data format.
Simple, rigorous and consistent syntax. The syntax of ASON is close to that of programming languages. For example, numbers can specify data types, multiple formats of comments and strings are supported, key names do not need to be enclosed in double quotes, trailing commas can be omitted, and so on. These features can help users accurately express the data they need, and are also facilitate the modification and maintenance of ASON documents.
Table of Content
A typical ASON document contains only one ASON object. The following is a example of an ASON document:
{
string: "hello world"
raw_string: r"[a-z]+"
number: 123
number_with_data_type: 123_456_789@long
float: 3.14
double: 6.626e-34@double
bool: true
date: d"2023-03-24 12:30:00+08:00"
variant_with_value: Option::Some(123)
variant_without_value: Option::None
array: [1,2,3,]
tuple: (1, "foo", true)
object: {
id: 123
name: "leaf"
}
}
It is worth mentioning that an ASON document does not necessarily have to be an ASON object, it can also be other ASON values, such as an array, a tuple, or even a single number or string, all of which are valid ASON values.
The ASON format is very similar to the JSON format, but there are a few significant differences:
Inf
, -Inf
, -0
and NaN
.null
value is not allowed, and is replaced by a new type called Variant
.Date
, Tuple
, and Variant
data types are added.The file extension for ASON document is *.ason
. Filename example:
sample.ason
, package.ason
Currently, only the Rust implementation of ASON serialization and deserialization library is provided.
Run the command cargo add ason
in your project directory to add the ASON library to your project.
This library provides two functions:
fn parse(s: &str) -> Result<AsonNode, ParseError>
for deserialization;fn write(n: &AsonNode) -> String
for serialization.Deserialization
Suppose you have the following ASON text, which may come from a file or from the internet:
{
id: 123
name: "foo"
}
The following code shows how to parse this text into an ASON object and check the value of each member:
let text = "..."; // the ASON text above
let node = parse(text).unwrap();
if let AsonNode::Object(obj) = node {
assert_eq!(
&obj[0],
&NameValuePair {
name: "id".to_string(),
value: Box::new(AsonNode::Number(NumberLiteral::Int(123)))
}
);
assert_eq!(
&obj[1],
&NameValuePair {
name: "name".to_string(),
value: Box::new(AsonNode::String_("foo".to_string()))
}
);
}
Serialization
Suppose there is an object with two fields, their names and values are:
The following code demonstrates how to convert this object into ASON text:
let node = AsonNode::Object(vec![
NameValuePair {
name: "name".to_string(),
value: Box::new(AsonNode::String_("foo".to_string())),
},
NameValuePair {
name: "version".to_string(),
value: Box::new(AsonNode::String_("0.1.0".to_string())),
},
]);
let text = write(&node);
println!("{}", text);
The output text should be:
{
name: "foo"
version: "0.1.0"
dependencies: [
{
name: "random"
version: Option::None
}
{
name: "regex"
version: Option::Some("1.0.1")
}
]
}
The Rust ASON library also provides a utility "ason" which can be used to read and validate, or format an ASON document.
First install the utility with the following command:
$ cargo install ason
This command will add the executable ason
to the ~/.cargo/bin
directory.
This usage of utility is:
$ ason <file_name>
For example:
$ ason test.ason
If the document "test.ason" has no errors, the program prints the formatted document to the terminal. The output can be redirected to a new file, e.g.:
$ ason test.ason > new.ason
See the document for more information.
Check out LICENSE and LICENSE.additional.