RON (Rusty Object Notation)

repository·master·Indexed 26 days ago

https://github.com/ron-rs/ron

A serialization format designed as a human-readable and expressive alternative to JSON, specifically tailored for the Rust ecosystem. It supports Rust-native concepts such as enums, structs, and ranges. The ron crate provides tools for serializing Rust types to RON strings and deserializing from strings, bytes, or readers, with support for configurable extensions like implicit_some and explicit_struct_names.

Tokens
6.2K
Snippets
13
Records
45
Agent score
87%

What's inside ron

  1. Enable RON extensions

    master
    You can enable specific RON extensions at the beginning of a RON file using the #!enable(...) syntax. Extensions allow overriding default grammar rules. The syntax requires the #! prefix followed by the enable keyword and a comma-separated list of extension names inside parentheses.
  2. Enable RON extensions

    master

    You can enable specific RON features by adding the #![enable(...)] attribute at the top of your RON document.

    Additionally, this crate ignores the following attributes used by other tools like ron2 for compatibility:

    • #![type = "path::To::Type"]
    • #![schema = "./path/to/schema.ron"]
  3. Use compact syntax for numeric ranges in structs

    master

    The RON serializer supports a compact numeric range syntax for certain struct fields. If a struct's fields match specific numeric range patterns, the serializer will emit a compact range instead of a standard struct representation.

    Supported range types and their emitted separators:

    • RangeFrom: Emits start ..
    • RangeTo or RangeToInclusive: Emits .. end or ..= end
    • Range or RangeInclusive: Emits start .. end or start ..= end

    If the fields do not strictly match these numeric patterns (e.g., if a non-numeric value is provided or a key mismatch occurs), the serializer automatically falls back to standard struct serialization.

  4. Include documentation comments in RON output

    master
    The RON serializer can include documentation comments in the serialized output. This is controlled via path_meta within the PrettyConfig. When a field has associated documentation, the serializer will prepend /// to each line of the documentation and indent it according to the current nesting level.
  5. Use raw strings in RON

    master
    Raw strings allow you to include quotation marks and backslashes without escaping them. They start with an r prefix, followed by zero or more # characters, a quotation mark, the string content, a quotation mark, and the same number of # characters used at the start.
  6. Use the implicit_some extension

    master

    Adding #![enable(implicit_some)] to your RON document enables automatic conversion of values to Some(value) if the target type is an Option.

    When enabled, RON eagerly matches None and Some(..) even for nested options like Option<Option<Option<u32>>>:

    • 5 -> Some(Some(Some(5)))
    • None -> None
    • Some(5) -> Some(Some(Some(5)))
    • Some(None) -> Some(None)
    #![enable(implicit_some)]
    (
        value: 5,
    )
  7. Configure explicit_struct_names via Options

    master

    To avoid repeating #![enable(explicit_struct_names)] in every RON file, you can configure the RON parser to expect explicit struct names by default using the Options API.

    use ron::extensions::Extensions;
    use ron::options::Options;
    
    let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES);
    let foo: Foo = options.from_str(file_contents)?;
  8. Use the unwrap_variant_newtypes extension

    master

    Adding #![enable(unwrap_variant_newtypes)] to your RON document enables automatic unwrapping of the first structural layer inside a newtype enum variant.

    Warning: When this extension is enabled, the first layer inside a newtype variant will always be unwrapped. It becomes impossible to write the explicit tuple/struct wrapper for that first layer (e.g., A(Inner(a: 4)) is no longer valid; you must use A(a: 4)).

    #![enable(unwrap_variant_newtypes)]
    (
        variant: A(a: 4, b: true),
    )
  9. Use the unwrap_newtypes extension

    master

    Adding #![enable(unwrap_newtypes)] to your RON document allows RON to automatically unwrap simple tuple newtypes. This allows you to provide the inner value directly instead of wrapping it in a tuple.

    Example: For a struct struct NewType(u32);, instead of writing (5), you can simply write 5.

    #![enable(unwrap_newtypes)]
    (
        new_type: 5,
    )
  10. Use the explicit_struct_names extension

    master

    The explicit_struct_names extension affects both serialization and deserialization:

    • Serialization: Emits struct names (e.g., Foo(bar: Bar(42))).
    • Deserialization: Requires all structs to have names attached. If a name is missing, it throws an ExpectedStructName error.

    If you are parsing many files, instead of adding the attribute to every file, use Options::with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES) to enable it globally for the parser.

    use ron::extensions::Extensions;
    use ron::options::Options;
    
    // Setup the options
    let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES);
    // Retrieve the contents of the file
    let file_contents: &str = /* ... */;
    // Parse the file's contents
    let foo: Foo = options.from_str(file_contents)?;
  11. Configure RON serialization formatting via PrettyConfig

    master

    When serializing data to RON, you can control the output format using PrettyConfig. The serializer uses these settings to determine how to handle indentation, newlines, and compact representations for different data structures.

    Key configuration behaviors observed in the serializer include:

    • Depth Limits: Indentation and newlines are applied only if the current indent level is within the configured depth_limit.
    • Compact Modes: The serializer respects compact_arrays, compact_maps, and compact_structs flags to decide whether to use multi-line formatting or single-line compact syntax.
    • Separators: A configurable separator (e.g., a space) is used between keys and values in maps and structs, and between elements in sequences, depending on whether the structure is in compact mode.