Skip to content
Snippets Groups Projects
Commit 4cfc0479 authored by Éloïs's avatar Éloïs
Browse files

[ref] all: add trait TojsonObject

parent b2a56f6b
No related branches found
No related tags found
1 merge request!93Migrate network documents to pest
...@@ -316,6 +316,7 @@ dependencies = [ ...@@ -316,6 +316,7 @@ dependencies = [
"pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
......
...@@ -20,6 +20,7 @@ pest = "2.0" ...@@ -20,6 +20,7 @@ pest = "2.0"
pest_derive = "2.0" pest_derive = "2.0"
serde = "1.0.*" serde = "1.0.*"
serde_derive = "1.0.*" serde_derive = "1.0.*"
serde_json = "1.0.*"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.5.1" pretty_assertions = "0.5.1"
......
...@@ -39,6 +39,7 @@ extern crate pretty_assertions; ...@@ -39,6 +39,7 @@ extern crate pretty_assertions;
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json;
pub mod blockstamp; pub mod blockstamp;
mod currencies_codes; mod currencies_codes;
...@@ -50,6 +51,7 @@ use dup_crypto::hashs::Hash; ...@@ -50,6 +51,7 @@ use dup_crypto::hashs::Hash;
use dup_crypto::keys::*; use dup_crypto::keys::*;
use pest::iterators::Pair; use pest::iterators::Pair;
use pest::Parser; use pest::Parser;
use serde::Serialize;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt::{Debug, Display, Error, Formatter}; use std::fmt::{Debug, Display, Error, Formatter};
use std::io::Cursor; use std::io::Cursor;
...@@ -315,3 +317,19 @@ pub trait DocumentParser<S, D, E> { ...@@ -315,3 +317,19 @@ pub trait DocumentParser<S, D, E> {
/// Parse a source and return a document or an error. /// Parse a source and return a document or an error.
fn parse(source: S) -> Result<D, E>; fn parse(source: S) -> Result<D, E>;
} }
/// Jsonify a document
pub trait ToJsonObject {
type JsonObject: Serialize;
/// Transforms an object into a json object
fn to_json_object(&self) -> Self::JsonObject;
/// Convert to JSON String
fn to_json_string(&self) -> Result<String, serde_json::Error> {
Ok(serde_json::to_string(&self.to_json_object())?)
}
/// Convert to JSON String pretty
fn to_json_string_pretty(&self) -> Result<String, serde_json::Error> {
Ok(serde_json::to_string_pretty(&self.to_json_object())?)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment