DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for JSON, RON and Rust
Henry Barreto
Henry Barreto

Posted on

JSON, RON and Rust

The most developers from a broad of programming languages know what is JSON (JavaScript Object Notation) and its utility in day life. However, as the name shows, in JavaScript JSON has the better integration than other languages which needs some more steps to use the practicality of the JSON what makes it no so practical.

A good example is Rust, strongly typed, to suffer a bit with the lack of types in a JSON object, although the community solved the problem of parsing JSON to Rust's "format" through the crate serde_json.

This is an example form the serde_json

use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u8,
    phones: Vec<String>,
}

fn typed_example() -> Result<()> {
    // Some JSON input data as a &str. Maybe this comes from the user.
    let data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    // Parse the string of data into a Person object. This is exactly the
    // same function as the one that produced serde_json::Value above, but
    // now we are asking it for a Person as output.
    let p: Person = serde_json::from_str(data)?;

    // Do things just like with any other Rust data structure.
    println!("Please call {} at the number {}", p.name, p.phones[0]);

    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

In the code above is defined a struct Person with datatype required in the JSON string data variable.

How it has seen, the parsing with JSON works, but that conversion has some "translate" issues or improvements like the no difference between structs and maps, random order, quoted fields, allowing comments inside, what an either RON solver or implement.

What is a RON?

A RON is a ""version of JSON"" for Rust. RON stands for Rust Object Notation, it is a simple readable data serialization format that looks similar to Rust syntax.

That is the difference between a JSON and a RON.

JSON

{
   "materials": {
        "metal": {
            "reflectivity": 1.0
        },
        "plastic": {
            "reflectivity": 0.5
        }
   },
   "entities": [
        {
            "name": "hero",
            "material": "metal"
        },
        {
            "name": "monster",
            "material": "plastic"
        }
   ]
}
Enter fullscreen mode Exit fullscreen mode

RON

Scene( // class name is optional
    materials: { // this is a map
        "metal": (
            reflectivity: 1.0,
        ),
        "plastic": (
            reflectivity: 0.5,
        ),
    },
    entities: [ // this is an array
        (
            name: "hero",
            material: "metal",
        ),
        (
            name: "monster",
            material: "plastic",
        ),
    ],
)
Enter fullscreen mode Exit fullscreen mode

The new format uses (..) brackets for heterogeneous structures (classes), while preserving the {..} for maps, and [..] for homogeneous structures (arrays). This distinction allows us to solve the biggest problem with JSON.

In the RON's repository has some examples to view and try out, further information about the crate, tooling around and much more what you need to know about it. Despite the crate simplicity, the documentation helps too.

Thanks for reading.

Top comments (1)

Collapse
iamgabrielsoft profile image
iamgabrielsoft

Thanks for the info...

Cool article πŸ‘

🌚 Life is too short to browse without dark mode