mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 15:51:12 +00:00
Serialization with serde.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Polkadot customizable serde serializer.
|
||||
//!
|
||||
//! The idea is that we can later change the implementation
|
||||
//! to something more compact, but for now we're using JSON.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
pub use serde_json::{from_str, from_slice, from_reader, Result};
|
||||
|
||||
const PROOF: &str = "Serializers are infallible; qed";
|
||||
|
||||
/// Serialize the given data structure as a pretty-printed String of JSON.
|
||||
pub fn to_string_pretty<T: serde::Serialize + ?Sized>(value: &T) -> String {
|
||||
serde_json::to_string_pretty(value).expect(PROOF)
|
||||
}
|
||||
|
||||
/// Serialize the given data structure as a JSON byte vector.
|
||||
pub fn to_vec<T: serde::Serialize + ?Sized>(value: &T) -> Vec<u8> {
|
||||
serde_json::to_vec(value).expect(PROOF)
|
||||
}
|
||||
|
||||
/// Serialize the given data structure as JSON into the IO stream.
|
||||
pub fn to_writer<W: ::std::io::Write, T: serde::Serialize + ?Sized>(writer: W, value: &T) -> Result<()> {
|
||||
serde_json::to_writer(writer, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user