Git Product home page Git Product logo

serde_any's Introduction

Serde Any

Dynamic serialization and deserialization with the format chosen at runtime. This crate allows you to serialize and deserialize data using serde without having to choose the data format up-front. The format can be either chosen at runtime, inferred from the file name, or guessed by attemting each supported format until one succeeds.

Supported formats

By default, JSON, YAML, TOML, RON, XML, and URL encoding formats are supported

[dependencies]
serde_any = "0.5"

The list of supported formats can be controlled via feature flags

[dependencies]
serde_any = { version = "0.5", default-features = false, features = ["yaml", "toml"] }

Deserialization

Structs that implement serde::de::Deserialize can be deserialized from a given file

let m: MyStruct = serde_any::from_file("my_data.json")?;

If support for multiple formats is desired, data can be deserialized from a fixed file stem. The following will look for my_data.json, my_data.yaml, my_data.yml, my_data.toml, my_data.ron, my_data.xml.

let m: MyStruct = serde_any::from_file_stem("my_data")?;

Deserialization can also be done from a string, a byte array, or a reader, with any format

let m1: MyStruct = serde_any::from_str_any(r#"{"name": "value"}"#)?;
let m2: MyStruct = serde_any::from_slice_any(b"name: value")?;

The serde_any crate also provides similar deserialization function with a fixed format

let m1: MyStruct = serde_any::from_str(r#"{"name": "value"}"#, serde_any::Format::Json)?;
let m2: MyStruct = serde_any::from_slice(b"name: value", serde_any::Format::Yaml)?;
let m3: MyStruct = serde_any::from_reader(File::open("some_data_file.toml")?, serde_any::Format::Toml)?;

Serialization

Structs that implement serde::ser::Serialize can be serialized into a file

    let m = MyStruct { ... };
    serde_any::to_file("my_data.json")?;

or to a string, a byte array, or a writer, with a fixed format

let m = MyStruct { ... };
let json_string = serde_any::to_string(&m, Format::Json)?;
let yaml_byte_vec = serde_any::to_vec(&m, Format::Yaml)?;

let writer = File::create("some_data_file.toml")?;
serde_any::to_writer(writer, Format::Toml)?;

All serialization functions have pretty printing variants with a _pretty suffix.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

serde_any's People

Contributors

noughmad avatar simonsan avatar

Watchers

James Cloos avatar  avatar  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.