Crate keon

Crate keon 

Source
Expand description

§KEON

KEON is a human readable object notation / serialization format that syntactic similar to Rust and completely supports Serde’s data model. For more details, like motivations, please see the repository.

📝 Cheat sheet
Unit()
Booleanstrue , false
Numbers42 , 0x1123 , -1 , 3.14 , inf , NaN
Chars'A' , '✱' , '\n' , '\u{3000}'
Strings"Hello" , `"raw string ^o^/"`
Bytesb"Hello" , b`"raw bytes ^o^/"` , b64"Sy0tQWV0aGlheA"
Options? , ? Thing
Tuples(T,) , (T, U, V)
Lists["abc", "def"]
Maps{ 1 => 2, 3 => 4 }
Structs(Struct) { field1: "value1", field2: "value2" }
VariantsEnum::Variant , Variant

And the Paragraphs, leave anything after the start sign of each line intact:

As is newline
| #include<iostream>
` using namespace std;
` int main() {
`     cout << "..." << endl;
`     return 0;
` }
Space-joined line
(will trim spaces)
| To be,
| or not to be,
| that is the question.
Joined line
(will trim spaces)
| 我能吞下
< 玻璃而不
< 伤身体。

The start signs can be mixed, but the first must be the vertical-bar |.

§Example

(Save) {                            // <- optional struct name.
    greeting: "Hello world!",
    keybinds: {
        Action::Up => 'W',          // <- optional enum name.
        Action::Down => 'S',
        Action::Left => 'A',
        Action::Right => 'D',
    },
    difficulty_factor: 4.3,
    respawn_point: (                // <- can use parentheses `(` for tuple `)`.
        1107.1487,
        1249.0458,
    ),
    inventory: [
        Item::Water,
        Item::CannedFood,
        Item::IdCard(101),          // <- newtype variant / tuple variant.
        Item::RocketLauncher {
            damage: 250,
            explosion_radius: 60.0,
        },
    ],
}

§Our advantages

  • Less syntactic noise, more intuitive look.
  • Allow comments and trailing commas.
  • Write KEON almost like you write Rust:
    • Humanized optional type annotation.
    • Arbitrary type as maps keys.
    • Use braces {} to represent maps and structs (RON doesn’t).
    • Distinguishable between tuples and lists (though they’re all seq in Serde).
  • Supports use Base64, Base32 and Base16 to represent bytes.
  • Provides Paragraph may be helpful when writing something by hand.

§Quick usage

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct MyData {
    msg: String,
    float: f32,
}

fn main() {
    let dat: MyData = keon::from_str(r#"{ msg: "Hello!", float: 1123. }"#).unwrap();

    println!("KEON: {}", keon::to_string(&dat).unwrap());

    println!("Pretty KEON: {}", keon::to_string_pretty(&dat).unwrap());
}

Re-exports§

pub use de::from_str;
pub use de::Deserializer;
pub use error::Error;
pub use error::ErrorKind;
pub use error::Result;
pub use ser::to_string;
pub use ser::to_string_pretty;
pub use ser::to_writer;
pub use ser::to_writer_pretty;
pub use ser::BytesFlavor;
pub use ser::SerializeConfig;
pub use ser::Serializer;
pub use value::Number;
pub use value::Value;

Modules§

de
error
ser
value