Code Monkey home page Code Monkey logo

ron's Introduction

Rusty Object Notation

CI codecov Crates.io MSRV Docs Matrix

RON is a simple readable data serialization format that looks similar to Rust syntax. It's designed to support all of Serde's data model, so structs, enums, tuples, arrays, generic maps, and primitive values.

Example

GameConfig( // optional struct name
    window_size: (800, 600),
    window_title: "PAC-MAN",
    fullscreen: false,
    
    mouse_sensitivity: 1.4,
    key_bindings: {
        "up": Up,
        "down": Down,
        "left": Left,
        "right": Right,
        
        // Uncomment to enable WASD controls
        /*
        "W": Up,
        "A": Down,
        "S": Left,
        "D": Right,
        */
    },
    
    difficulty_options: (
        start_difficulty: Easy,
        adaptive: false,
    ),
)

Why RON?

Example in JSON

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

Same example in 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",
        ),
    ],
)

Note the following advantages of RON over JSON:

  • trailing commas allowed
  • single- and multi-line comments
  • field names aren't quoted, so it's less verbose
  • optional struct names improve readability
  • enums are supported (and less verbose than their JSON representation)

Limitations

RON is not designed to be a fully self-describing format (unlike JSON) and is thus not guaranteed to work when deserialize_any is used instead of its typed alternatives. In particular, the following Serde attributes only have limited support:

  • #[serde(tag = "tag")], i.e. internally tagged enums 1
  • #[serde(tag = "tag", content = "content")], i.e. adjacently tagged enums 1
  • #[serde(untagged)], i.e. untagged enums 1
  • #[serde(flatten)], i.e. flattening of structs into maps 2

While data structures with any of these attributes should roundtrip through RON, their textual representation may not always match your expectation. For instance, flattened structs are only serialised as maps and deserialised from maps.

RON syntax overview

  • Numbers: 42, 3.14, 0xFF, 0b0110
  • Strings: "Hello", "with\\escapes\n", r#"raw string, great for regex\."#
  • Booleans: true, false
  • Chars: 'e', '\n'
  • Optionals: Some("string"), Some(Some(1.34)), None
  • Tuples: ("abc", 1.23, true), ()
  • Lists: ["abc", "def"]
  • Structs: ( foo: 1.0, bar: ( baz: "I'm nested" ) )
  • Maps: { "arbitrary": "keys", "are": "allowed" }

Note: Serde's data model represents fixed-size Rust arrays as tuple (instead of as list)

Quickstart

Cargo.toml

[dependencies]
ron = "0.8"
serde = { version = "1", features = ["derive"] }

main.rs

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct MyStruct {
    boolean: bool,
    float: f32,
}

fn main() {
    let x: MyStruct = ron::from_str("(boolean: true, float: 1.23)").unwrap();
    
    println!("RON: {}", ron::to_string(&x).unwrap());

    println!("Pretty RON: {}", ron::ser::to_string_pretty(
        &x, ron::ser::PrettyConfig::default()).unwrap(),
    );
}

Tooling

Editor Plugin
IntelliJ intellij-ron
VS Code a5huynh/vscode-ron
Sublime Text RON
Atom language-ron
Vim ron-rs/ron.vim
EMACS emacs-ron

Specification

There is a very basic, work in progress specification available on the wiki page. A more formal and complete grammar is available here.

License

RON is dual-licensed under Apache-2.0 and MIT.

Any contribution intentionally submitted for inclusion in the work must be provided under the same dual-license terms.

Footnotes

  1. Deserialising an internally, adjacently, or un-tagged enum requires detecting serde's internal serde::__private::de::content::Content content type so that RON can describe the deserialised data structure in serde's internal JSON-like format. This detection only works for the automatically-derived Deserialize impls on enums. See #451 for more details. 2 3

  2. Deserialising a flattened struct from a map requires that the struct's Visitor::expecting implementation formats a string starting with "struct ". This is the case for automatically-derived Deserialize impls on structs. See #455 for more details.

ron's People

Contributors

torkleyy avatar bors[bot] avatar juntyr avatar kvark avatar ebkalderon avatar cad97 avatar elrnv avatar cpud36 avatar dependabot-preview[bot] avatar victorkoenders avatar plecra avatar bpfoley avatar cmaher avatar rhuagh avatar vessd avatar peter-scholtens avatar ludicast avatar jakubvaltar avatar xaeroxe avatar fengalin avatar davidkorczynski avatar cswinter avatar divinegod avatar kestrer avatar lachlansneff avatar michael-wit avatar nnethercote avatar nvzqz avatar nuc1eon avatar pyfisch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.