Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i128 support #498

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ matrix:
- cargo test --features arbitrary_precision
- cargo test --features raw_value

- rust: 1.15.0
script:
# preserve_order is not supported on 1.15.0
- cargo build
- cargo build --features arbitrary_precision

- rust: stable
- rust: beta
- rust: 1.18.0
- rust: 1.26.0

- rust: nightly
env: CLIPPY
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["json", "serde", "serialization"]
categories = ["encoding"]
readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
build = "build.rs"

[badges]
travis-ci = { repository = "serde-rs/json" }
Expand All @@ -18,7 +19,7 @@ appveyor = { repository = "serde-rs/json" }
[dependencies]
serde = "1.0.60"
indexmap = { version = "1.0", optional = true }
itoa = "0.4.3"
itoa = {version= "0.4.3", features = ["std", "i128"]}
ryu = "0.2"

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Serde JSON   [![Build Status]][travis] [![Latest Version]][crates.io] [![Rustc Version 1.15+]][rustc]
# Serde JSON   [![Build Status]][travis] [![Latest Version]][crates.io] [![Rustc Version 1.26+]][rustc]

[Build Status]: https://api.travis-ci.org/serde-rs/json.svg?branch=master
[travis]: https://travis-ci.org/serde-rs/json
[Latest Version]: https://img.shields.io/crates/v/serde_json.svg
[crates.io]: https://crates.io/crates/serde\_json
[Rustc Version 1.15+]: https://img.shields.io/badge/rustc-1.15+-lightgray.svg
[rustc]: https://blog.rust-lang.org/2017/02/02/Rust-1.15.html
[Rustc Version 1.26+]: https://img.shields.io/badge/rustc-1.26+-lightgray.svg
[rustc]: https://blog.rust-lang.org/2018/05/10/Rust-1.26.html

**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**

Expand Down
50 changes: 50 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::env;
use std::process::Command;
use std::str::{self, FromStr};

fn main() {
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};

if minor < 26 {
panic!("serde_json requires rustc 1.26+");
}
}

fn rustc_minor_version() -> Option<u32> {
// Logic borrowed from https://github.com/serde-rs/serde/blob/master/serde/build.rs
let rustc = match env::var_os("RUSTC") {
Some(rustc) => rustc,
None => return None,
};

let output = match Command::new(rustc).arg("--version").output() {
Ok(output) => output,
Err(_) => return None,
};

let version = match str::from_utf8(&output.stdout) {
Ok(version) => version,
Err(_) => return None,
};

// Temporary workaround to support the old 1.26-dev compiler on docs.rs.
if version.contains("0eb87c9bf") {
return Some(25);
}

let mut pieces = version.split('.');

if pieces.next() != Some("rustc 1") {
return None;
}

let next = match pieces.next() {
Some(next) => next,
None => return None,
};

u32::from_str(next).ok()
}
155 changes: 79 additions & 76 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub enum ParserNumber {
F64(f64),
U64(u64),
I64(i64),
U128(u128),
I128(i128),
#[cfg(feature = "arbitrary_precision")]
String(String),
}
Expand All @@ -105,6 +107,8 @@ impl ParserNumber {
ParserNumber::F64(x) => visitor.visit_f64(x),
ParserNumber::U64(x) => visitor.visit_u64(x),
ParserNumber::I64(x) => visitor.visit_i64(x),
ParserNumber::U128(x) => visitor.visit_u128(x),
ParserNumber::I128(x) => visitor.visit_i128(x),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }),
}
Expand All @@ -115,6 +119,12 @@ impl ParserNumber {
ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp),
ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp),
ParserNumber::I64(x) => de::Error::invalid_type(Unexpected::Signed(x), exp),
ParserNumber::U128(x) => {
de::Error::invalid_type(Unexpected::Other(format!("integer `{}`", x).as_str()), exp)
}
ParserNumber::I128(x) => {
de::Error::invalid_type(Unexpected::Other(format!("integer `{}`", x).as_str()), exp)
}
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number"), exp),
}
Expand Down Expand Up @@ -274,30 +284,28 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
}

serde_if_integer128! {
fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
match try!(self.next_char_or_null()) {
b'0' => {
buf.push('0');
// There can be only one leading '0'.
match try!(self.peek_or_null()) {
b'0'...b'9' => {
Err(self.peek_error(ErrorCode::InvalidNumber))
}
_ => Ok(()),
fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
match try!(self.next_char_or_null()) {
b'0' => {
buf.push('0');
// There can be only one leading '0'.
match try!(self.peek_or_null()) {
b'0'...b'9' => {
Err(self.peek_error(ErrorCode::InvalidNumber))
}
_ => Ok(()),
}
c @ b'1'...b'9' => {
}
c @ b'1'...b'9' => {
buf.push(c as char);
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
}
Ok(())
}
_ => {
Err(self.error(ErrorCode::InvalidNumber))
}
Ok(())
}
_ => {
Err(self.error(ErrorCode::InvalidNumber))
}
}
}
Expand Down Expand Up @@ -1123,67 +1131,65 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
deserialize_prim_number!(deserialize_f32);
deserialize_prim_number!(deserialize_f64);

serde_if_integer128! {
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let mut buf = String::new();

match try!(self.parse_whitespace()) {
Some(b'-') => {
self.eat_char();
buf.push('-');
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let mut buf = String::new();

try!(self.scan_integer128(&mut buf));
match try!(self.parse_whitespace()) {
Some(b'-') => {
self.eat_char();
buf.push('-');
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};

let value = match buf.parse() {
Ok(int) => visitor.visit_i128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
try!(self.scan_integer128(&mut buf));

match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
let value = match buf.parse() {
Ok(int) => visitor.visit_i128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};

match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match try!(self.parse_whitespace()) {
Some(b'-') => {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match try!(self.parse_whitespace()) {
Some(b'-') => {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
}

let mut buf = String::new();
try!(self.scan_integer128(&mut buf));

let value = match buf.parse() {
Ok(int) => visitor.visit_u128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
let mut buf = String::new();
try!(self.scan_integer128(&mut buf));

match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
let value = match buf.parse() {
Ok(int) => visitor.visit_u128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};

match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}

Expand Down Expand Up @@ -1896,11 +1902,8 @@ where
deserialize_integer_key!(deserialize_u16 => visit_u16);
deserialize_integer_key!(deserialize_u32 => visit_u32);
deserialize_integer_key!(deserialize_u64 => visit_u64);

serde_if_integer128! {
deserialize_integer_key!(deserialize_i128 => visit_i128);
deserialize_integer_key!(deserialize_u128 => visit_u128);
}
deserialize_integer_key!(deserialize_i128 => visit_i128);
deserialize_integer_key!(deserialize_u128 => visit_u128);

#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
Expand Down
Loading