Skip to content

Commit c379d33

Browse files
authored
Merge pull request #81 from maciejhirsz/0.10.0
0.10.0 - closes #80, closes #79, closes #77, closes #76, closes #69.
2 parents d77a5c6 + 531d967 commit c379d33

21 files changed

+2986
-1670
lines changed

Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
[package]
22
name = "json"
3-
version = "0.9.1"
3+
version = "0.10.0"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"
77
documentation = "http://terhix.com/doc/json/"
88
license = "MIT/Apache-2.0"
9-
10-
[dependencies]
11-
itoa = "0.1"
12-
ftoa = "0.1"

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ can even outperform parsing to structs.
125125
This crate implements the standard according to the [
126126
RFC 7159](https://tools.ietf.org/html/rfc7159) and
127127
[ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)
128-
documents. It makes some compromises when it comes to number precision, but they are
129-
both within the specification and advised practices for best interoperability,
130-
specifically those discussed in the RFC.
128+
documents. For the best interoperability numbers are treated stored as 64bit precision
129+
mantissa with 16 bit decimal exponent for floating point representation.
131130

132131
## License
133132

src/codegen.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::ptr;
22
use std::io::Write;
3-
use std::num::FpCategory;
43
use JsonValue;
4+
use number::Number;
55

6-
extern crate itoa;
7-
extern crate ftoa;
6+
use util::print_dec;
87

98
const QU: u8 = b'"';
109
const BS: u8 = b'\\';
@@ -54,10 +53,13 @@ pub trait Generator {
5453

5554
fn write_min(&mut self, slice: &[u8], min: u8);
5655

56+
#[inline(always)]
5757
fn new_line(&mut self) {}
5858

59+
#[inline(always)]
5960
fn indent(&mut self) {}
6061

62+
#[inline(always)]
6163
fn dedent(&mut self) {}
6264

6365
#[inline(never)]
@@ -95,27 +97,19 @@ pub trait Generator {
9597
}
9698

9799
#[inline(always)]
98-
fn write_number(&mut self, num: f64) {
99-
match num.classify() {
100-
FpCategory::Normal |
101-
FpCategory::Subnormal => {
102-
if num.fract() == 0.0 && num.abs() < 1e19 {
103-
itoa::write(self.get_writer(), num as i64).unwrap();
104-
} else {
105-
ftoa::write(self.get_writer(), num).unwrap();
106-
}
107-
},
108-
FpCategory::Zero => {
109-
if num.is_sign_negative() {
110-
self.write(b"-0");
111-
} else {
112-
self.write_char(b'0');
113-
}
114-
},
115-
FpCategory::Nan |
116-
FpCategory::Infinite => {
117-
self.write(b"null");
118-
}
100+
fn write_number(&mut self, num: &Number) {
101+
if num.is_nan() {
102+
self.write(b"null");
103+
return;
104+
}
105+
let (positive, mantissa, exponent) = num.as_parts();
106+
unsafe {
107+
print_dec::write(
108+
self.get_writer(),
109+
positive,
110+
mantissa,
111+
exponent
112+
).unwrap();
119113
}
120114
}
121115

@@ -124,7 +118,7 @@ pub trait Generator {
124118
JsonValue::Null => self.write(b"null"),
125119
JsonValue::Short(ref short) => self.write_string(short.as_str()),
126120
JsonValue::String(ref string) => self.write_string(string),
127-
JsonValue::Number(ref number) => self.write_number(*number),
121+
JsonValue::Number(ref number) => self.write_number(number),
128122
JsonValue::Boolean(true) => self.write(b"true"),
129123
JsonValue::Boolean(false) => self.write(b"false"),
130124
JsonValue::Array(ref array) => {

src/error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub enum Error {
1515
UnexpectedEndOfJson,
1616
FailedUtf8Parsing,
1717
WrongType(String),
18-
UndefinedField(String),
1918
}
2019

2120
impl Error {
@@ -38,7 +37,6 @@ impl fmt::Display for Error {
3837
UnexpectedEndOfJson => write!(f, "Unexpected end of JSON"),
3938
FailedUtf8Parsing => write!(f, "Failed to parse UTF-8 bytes"),
4039
WrongType(ref s) => write!(f, "Wrong type, expected: {}", s),
41-
UndefinedField(ref s) => write!(f, "Undefined field: {}", s)
4240
}
4341
}
4442
}
@@ -51,7 +49,6 @@ impl error::Error for Error {
5149
UnexpectedEndOfJson => "Unexpected end of JSON",
5250
FailedUtf8Parsing => "Failed to read bytes as UTF-8 from JSON",
5351
WrongType(_) => "Wrong type",
54-
UndefinedField(_) => "Undefined field",
5552
}
5653
}
5754
}

0 commit comments

Comments
 (0)