mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 02:28:00 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9b57f60c2a | |||
| 8038832a79 | |||
| 072ff149f5 | |||
| 8f08baf43a | |||
| b3b3b7d565 | |||
| 1a8a11e924 | |||
| f3f098e7f5 | |||
| 09822c99cc | |||
| 966b104d48 | |||
| 59e0d5e081 | |||
| c687ee60ff | |||
| af6fbba9b8 | |||
| a577574cfe |
@@ -0,0 +1,2 @@
|
||||
target
|
||||
Cargo.lock
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "serde-syntex-example"
|
||||
version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["serde_codegen"]
|
||||
nightly = ["serde_macros"]
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = { version = "^0.6.4", optional = true }
|
||||
syntex = "^0.22.0"
|
||||
|
||||
[dependencies]
|
||||
serde = "^0.6.1"
|
||||
serde_json = "^0.6.0"
|
||||
serde_macros = { version = "^0.6.1", optional = true }
|
||||
@@ -0,0 +1,20 @@
|
||||
This example demonstrates how to use Serde with Syntex. On stable or nightly
|
||||
with Syntex, it can be built with:
|
||||
|
||||
```
|
||||
% multirust run stable cargo run
|
||||
Running `target/debug/serde-syntex-example`
|
||||
{"x":1,"y":2}
|
||||
Point { x: 1, y: 2 }
|
||||
|
||||
% multirust run nightly cargo run
|
||||
Running `target/debug/serde-syntex-example`
|
||||
{"x":1,"y":2}
|
||||
Point { x: 1, y: 2 }
|
||||
```
|
||||
|
||||
On nightly, it can use a plugin with:
|
||||
|
||||
```
|
||||
% multirust run nightly cargo run --features nightly --no-default-features
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
#[cfg(not(feature = "serde_macros"))]
|
||||
mod inner {
|
||||
extern crate syntex;
|
||||
extern crate serde_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
let src = Path::new("src/main.rs.in");
|
||||
let dst = Path::new(&out_dir).join("main.rs");
|
||||
|
||||
let mut registry = syntex::Registry::new();
|
||||
|
||||
serde_codegen::register(&mut registry);
|
||||
registry.expand("", &src, &dst).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde_macros")]
|
||||
mod inner {
|
||||
pub fn main() {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
inner::main();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#![cfg_attr(nightly, feature(custom_derive, plugin))]
|
||||
#![cfg_attr(nightly, plugin(serde_macros))]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
#[cfg(feature = "serde_macros")]
|
||||
include!("main.rs.in");
|
||||
|
||||
#[cfg(not(feature = "serde_macros"))]
|
||||
include!(concat!(env!("OUT_DIR"), "/main.rs"));
|
||||
@@ -0,0 +1,16 @@
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let point = Point { x: 1, y: 2 };
|
||||
let serialized = serde_json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized);
|
||||
|
||||
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
println!("{:?}", deserialized);
|
||||
}
|
||||
+5
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.6.1"
|
||||
version = "0.6.7"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
@@ -14,3 +14,7 @@ num = "^0.1.27"
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
num-impls = ["num-bigint", "num-complex", "num-rational"]
|
||||
num-bigint = ["num/bigint"]
|
||||
num-complex = ["num/complex"]
|
||||
num-rational = ["num/rational"]
|
||||
|
||||
@@ -895,3 +895,87 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
||||
deserializer.visit_enum("Result", VARIANTS, Visitor(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Deserialize for ::num::bigint::BigInt {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
use ::num::Num;
|
||||
use ::num::bigint::BigInt;
|
||||
|
||||
struct BigIntVisitor;
|
||||
|
||||
impl Visitor for BigIntVisitor {
|
||||
type Value = BigInt;
|
||||
|
||||
fn visit_str<E>(&mut self, s: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
match BigInt::from_str_radix(s, 10) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Error::invalid_value(&err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(BigIntVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Deserialize for ::num::bigint::BigUint {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
use ::num::Num;
|
||||
use ::num::bigint::BigUint;
|
||||
|
||||
struct BigUintVisitor;
|
||||
|
||||
impl Visitor for BigUintVisitor {
|
||||
type Value = ::num::bigint::BigUint;
|
||||
|
||||
fn visit_str<E>(&mut self, s: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
match BigUint::from_str_radix(s, 10) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Error::invalid_value(&err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(BigUintVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-complex")]
|
||||
impl<T> Deserialize for ::num::complex::Complex<T>
|
||||
where T: Deserialize + Clone + ::num::Num
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let (re, im) = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(::num::complex::Complex::new(re, im))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-rational")]
|
||||
impl<T> Deserialize for ::num::rational::Ratio<T>
|
||||
where T: Deserialize + Clone + ::num::Integer + PartialOrd
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let (numer, denom) = try!(Deserialize::deserialize(deserializer));
|
||||
if denom == ::num::Zero::zero() {
|
||||
Err(Error::invalid_value("denominator is zero"))
|
||||
} else {
|
||||
Ok(::num::rational::Ratio::new_raw(numer, denom))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,11 @@ pub trait Error: Sized {
|
||||
Error::syntax("incorrect type")
|
||||
}
|
||||
|
||||
/// Raised when a `Deserialize` was passed an incorrect value.
|
||||
fn invalid_value(msg: &str) -> Self {
|
||||
Error::syntax(msg)
|
||||
}
|
||||
|
||||
/// Raised when a `Deserialize` type unexpectedly hit the end of the stream.
|
||||
fn end_of_stream() -> Self;
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
//! [github repository](https://github.com/serde-rs/serde)
|
||||
|
||||
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
||||
#![cfg_attr(feature = "nightly", feature(collections, core, enumset, nonzero, step_trait, zero_one))]
|
||||
#![cfg_attr(feature = "nightly", feature(collections, enumset, nonzero, step_trait, zero_one))]
|
||||
|
||||
#![deny(missing_docs)]
|
||||
|
||||
|
||||
@@ -700,3 +700,37 @@ impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Serialize for ::num::bigint::BigInt {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
self.to_str_radix(10).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Serialize for ::num::bigint::BigUint {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
self.to_str_radix(10).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-complex")]
|
||||
impl<T> Serialize for ::num::complex::Complex<T>
|
||||
where T: Serialize + Clone + ::num::Num
|
||||
{
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
(&self.re, &self.im).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-rational")]
|
||||
impl<T> Serialize for ::num::rational::Ratio<T>
|
||||
where T: Serialize + Clone + ::num::Integer + PartialOrd
|
||||
{
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
(self.numer(), self.denom()).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_codegen"
|
||||
version = "0.6.2"
|
||||
version = "0.6.8"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -15,12 +15,12 @@ nightly = ["quasi_macros"]
|
||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
||||
|
||||
[build-dependencies]
|
||||
quasi_codegen = { verision = "^0.3.6", optional = true }
|
||||
syntex = { version = "^0.17.0", optional = true }
|
||||
quasi_codegen = { verision = "^0.3.12", optional = true }
|
||||
syntex = { version = "^0.25.0", optional = true }
|
||||
|
||||
[dependencies]
|
||||
aster = { version = "^0.6.0", default-features = false }
|
||||
quasi = { verision = "^0.3.5", default-features = false }
|
||||
quasi_macros = { version = "^0.3.5", optional = true }
|
||||
syntex = { version = "^0.17.0", optional = true }
|
||||
syntex_syntax = { version = "^0.19.1", optional = true }
|
||||
aster = { version = "^0.9.3", default-features = false }
|
||||
quasi = { verision = "^0.3.12", default-features = false }
|
||||
quasi_macros = { version = "^0.3.12", optional = true }
|
||||
syntex = { version = "^0.25.0", optional = true }
|
||||
syntex_syntax = { version = "^0.25.0", optional = true }
|
||||
|
||||
@@ -5,7 +5,6 @@ use aster;
|
||||
use syntax::ast::{
|
||||
self,
|
||||
EnumDef,
|
||||
Expr,
|
||||
Ident,
|
||||
Item,
|
||||
MetaItem,
|
||||
@@ -13,7 +12,6 @@ use syntax::ast::{
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::owned_slice::OwnedSlice;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use attr;
|
||||
@@ -65,7 +63,6 @@ pub fn expand_derive_deserialize(
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
#[automatically_derived]
|
||||
impl $impl_generics ::serde::de::Deserialize for $ty $where_clause {
|
||||
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
||||
where __D: ::serde::de::Deserializer,
|
||||
@@ -188,7 +185,7 @@ fn deserialize_visitor(
|
||||
let mut trait_generics = trait_generics.clone();
|
||||
let mut ty_params = forward_ty_params.clone();
|
||||
ty_params.extend(trait_generics.ty_params.into_vec());
|
||||
trait_generics.ty_params = OwnedSlice::from_vec(ty_params);
|
||||
trait_generics.ty_params = P::from_vec(ty_params);
|
||||
|
||||
(
|
||||
builder.item().tuple_struct("__Visitor")
|
||||
|
||||
@@ -14,7 +14,7 @@ extern crate syntex_syntax as syntax;
|
||||
extern crate syntax;
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
extern crate rustc;
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
||||
@@ -60,7 +60,7 @@ pub fn register(reg: &mut syntex::Registry) {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
pub fn register(reg: &mut rustc::plugin::Registry) {
|
||||
pub fn register(reg: &mut rustc_plugin::Registry) {
|
||||
reg.register_syntax_extension(
|
||||
syntax::parse::token::intern("derive_Serialize"),
|
||||
syntax::ext::base::MultiDecorator(
|
||||
|
||||
@@ -4,7 +4,6 @@ use syntax::ast::{
|
||||
Ident,
|
||||
MetaItem,
|
||||
Item,
|
||||
Expr,
|
||||
};
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
@@ -60,7 +59,6 @@ pub fn expand_derive_serialize(
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
#[automatically_derived]
|
||||
impl $impl_generics ::serde::ser::Serialize for $ty $where_clause {
|
||||
fn serialize<__S>(&self, serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||
where __S: ::serde::ser::Serializer,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_macros"
|
||||
version = "0.6.1"
|
||||
version = "0.6.6"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -18,4 +18,4 @@ serde_codegen = { version = "*", path = "../serde_codegen", default-features = f
|
||||
[dev-dependencies]
|
||||
num = "^0.1.27"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde", features = ["nightly"] }
|
||||
serde = { version = "*", path = "../serde", features = ["nightly", "num-impls"] }
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#![feature(plugin_registrar, rustc_private)]
|
||||
|
||||
extern crate serde_codegen;
|
||||
extern crate rustc;
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[plugin_registrar]
|
||||
#[doc(hidden)]
|
||||
pub fn plugin_registrar(reg: &mut rustc::plugin::Registry) {
|
||||
pub fn plugin_registrar(reg: &mut rustc_plugin::Registry) {
|
||||
serde_codegen::register(reg);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![feature(test, custom_attribute, custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
extern crate test;
|
||||
|
||||
|
||||
@@ -11,15 +11,15 @@ keywords = ["serialization"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
syntex = { version = "^0.17.0" }
|
||||
syntex_syntax = { version = "^0.19.1" }
|
||||
syntex = { version = "^0.25.0" }
|
||||
syntex_syntax = { version = "^0.25.0" }
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
||||
|
||||
[dev-dependencies]
|
||||
num = "^0.1.27"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde" }
|
||||
syntex = "^0.17.0"
|
||||
serde = { version = "*", path = "../serde", features = ["num-impls"] }
|
||||
syntex = "^0.25.0"
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use serde::de::{Deserializer, Visitor};
|
||||
|
||||
use token::{Token, assert_de_tokens};
|
||||
@@ -555,4 +560,33 @@ declare_tests! {
|
||||
Token::Unit,
|
||||
],
|
||||
}
|
||||
test_num_bigint {
|
||||
BigInt::from_i64(123).unwrap() => vec![Token::Str("123")],
|
||||
BigInt::from_i64(-123).unwrap() => vec![Token::Str("-123")],
|
||||
}
|
||||
test_num_biguint {
|
||||
BigUint::from_i64(123).unwrap() => vec![Token::Str("123")],
|
||||
}
|
||||
test_num_complex {
|
||||
Complex::new(1, 2) => vec![
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_num_ratio {
|
||||
Ratio::new(1, 2) => vec![
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use token::Token;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
@@ -259,4 +264,33 @@ declare_ser_tests! {
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_num_bigint {
|
||||
BigInt::from_i64(123).unwrap() => &[Token::Str("123")],
|
||||
BigInt::from_i64(-123).unwrap() => &[Token::Str("-123")],
|
||||
}
|
||||
test_num_biguint {
|
||||
BigUint::from_i64(123).unwrap() => &[Token::Str("123")],
|
||||
}
|
||||
test_num_complex {
|
||||
Complex::new(1, 2) => &[
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_num_ratio {
|
||||
Ratio::new(1, 2) => &[
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user