Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c32286d1
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c32286d1
编写于
11月 24, 2014
作者:
B
Barosl Lee
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
libserialize: Code cleanup
上级
fec0f16c
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
65 addition
and
69 deletion
+65
-69
src/libserialize/json.rs
src/libserialize/json.rs
+65
-69
未找到文件。
src/libserialize/json.rs
浏览文件 @
c32286d1
...
...
@@ -91,7 +91,7 @@
//! fn main() {
//! let object = TestStruct {
//! data_int: 1,
//! data_str: "
toto
".to_string(),
//! data_str: "
homura
".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//!
...
...
@@ -178,7 +178,7 @@
//! // Serialize using `ToJson`
//! let input_data = TestStruct {
//! data_int: 1,
//! data_str: "
toto
".to_string(),
//! data_str: "
madoka
".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//! let json_obj: Json = input_data.to_json();
...
...
@@ -443,7 +443,9 @@ fn emit_bool(&mut self, v: bool) -> EncodeResult {
fn
emit_f64
(
&
mut
self
,
v
:
f64
)
->
EncodeResult
{
write!
(
self
.writer
,
"{}"
,
fmt_number_or_null
(
v
))
}
fn
emit_f32
(
&
mut
self
,
v
:
f32
)
->
EncodeResult
{
self
.emit_f64
(
v
as
f64
)
}
fn
emit_f32
(
&
mut
self
,
v
:
f32
)
->
EncodeResult
{
self
.emit_f64
(
v
as
f64
)
}
fn
emit_char
(
&
mut
self
,
v
:
char
)
->
EncodeResult
{
escape_char
(
self
.writer
,
v
)
...
...
@@ -452,7 +454,9 @@ fn emit_str(&mut self, v: &str) -> EncodeResult {
escape_str
(
self
.writer
,
v
)
}
fn
emit_enum
(
&
mut
self
,
_
name
:
&
str
,
f
:
|
&
mut
Encoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
fn
emit_enum
(
&
mut
self
,
_
name
:
&
str
,
f
:
|
&
mut
Encoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
f
(
self
)
}
...
...
@@ -663,7 +667,7 @@ fn emit_enum(&mut self,
fn
emit_enum_variant
(
&
mut
self
,
name
:
&
str
,
_
:
uint
,
_
id
:
uint
,
cnt
:
uint
,
f
:
|
&
mut
PrettyEncoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
if
cnt
==
0
{
...
...
@@ -1962,30 +1966,22 @@ fn pop(&mut self) -> Json {
(
$name:ident
,
$ty:ty
)
=>
{
fn
$name
(
&
mut
self
)
->
DecodeResult
<
$ty
>
{
match
self
.pop
()
{
Json
::
I64
(
f
)
=>
{
match
num
::
cast
(
f
)
{
Json
::
I64
(
f
)
=>
match
num
::
cast
(
f
)
{
Some
(
f
)
=>
Ok
(
f
),
None
=>
Err
(
ExpectedError
(
"Number"
.to_string
(),
format!
(
"{}"
,
f
))),
}
}
Json
::
U64
(
f
)
=>
{
match
num
::
cast
(
f
)
{
},
Json
::
U64
(
f
)
=>
match
num
::
cast
(
f
)
{
Some
(
f
)
=>
Ok
(
f
),
None
=>
Err
(
ExpectedError
(
"Number"
.to_string
(),
format!
(
"{}"
,
f
))),
}
}
Json
::
F64
(
f
)
=>
{
Err
(
ExpectedError
(
"Integer"
.to_string
(),
format!
(
"{}"
,
f
)))
}
Json
::
String
(
s
)
=>
{
},
Json
::
F64
(
f
)
=>
Err
(
ExpectedError
(
"Integer"
.to_string
(),
format!
(
"{}"
,
f
))),
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
// is going to have a string here, as per JSON spec.
match
std
::
str
::
from_str
(
s
.as_slice
())
{
Json
::
String
(
s
)
=>
match
std
::
str
::
from_str
(
s
.as_slice
())
{
Some
(
f
)
=>
Ok
(
f
),
None
=>
Err
(
ExpectedError
(
"Number"
.to_string
(),
s
)),
}
},
value
=>
Err
(
ExpectedError
(
"Number"
.to_string
(),
format!
(
"{}"
,
value
)))
value
=>
Err
(
ExpectedError
(
"Number"
.to_string
(),
format!
(
"{}"
,
value
)))
,
}
}
}
...
...
@@ -2484,20 +2480,20 @@ fn test_from_str_trait() {
#[test]
fn
test_write_null
()
{
assert_eq!
(
Null
.to_string
()
.into_string
()
,
"null"
);
assert_eq!
(
Null
.to_pretty_str
()
.into_string
()
,
"null"
);
assert_eq!
(
Null
.to_string
(),
"null"
);
assert_eq!
(
Null
.to_pretty_str
(),
"null"
);
}
#[test]
fn
test_write_i64
()
{
assert_eq!
(
U64
(
0
)
.to_string
()
.into_string
()
,
"0"
);
assert_eq!
(
U64
(
0
)
.to_pretty_str
()
.into_string
()
,
"0"
);
assert_eq!
(
U64
(
0
)
.to_string
(),
"0"
);
assert_eq!
(
U64
(
0
)
.to_pretty_str
(),
"0"
);
assert_eq!
(
U64
(
1234
)
.to_string
()
.into_string
()
,
"1234"
);
assert_eq!
(
U64
(
1234
)
.to_pretty_str
()
.into_string
()
,
"1234"
);
assert_eq!
(
U64
(
1234
)
.to_string
(),
"1234"
);
assert_eq!
(
U64
(
1234
)
.to_pretty_str
(),
"1234"
);
assert_eq!
(
I64
(
-
5678
)
.to_string
()
.into_string
()
,
"-5678"
);
assert_eq!
(
I64
(
-
5678
)
.to_pretty_str
()
.into_string
()
,
"-5678"
);
assert_eq!
(
I64
(
-
5678
)
.to_string
(),
"-5678"
);
assert_eq!
(
I64
(
-
5678
)
.to_pretty_str
(),
"-5678"
);
assert_eq!
(
U64
(
7650007200025252000
)
.to_string
(),
"7650007200025252000"
);
assert_eq!
(
U64
(
7650007200025252000
)
.to_pretty_str
(),
"7650007200025252000"
);
...
...
@@ -2505,54 +2501,54 @@ fn test_write_i64() {
#[test]
fn
test_write_f64
()
{
assert_eq!
(
F64
(
3.0
)
.to_string
()
.into_string
()
,
"3.0"
);
assert_eq!
(
F64
(
3.0
)
.to_pretty_str
()
.into_string
()
,
"3.0"
);
assert_eq!
(
F64
(
3.0
)
.to_string
(),
"3.0"
);
assert_eq!
(
F64
(
3.0
)
.to_pretty_str
(),
"3.0"
);
assert_eq!
(
F64
(
3.1
)
.to_string
()
.into_string
()
,
"3.1"
);
assert_eq!
(
F64
(
3.1
)
.to_pretty_str
()
.into_string
()
,
"3.1"
);
assert_eq!
(
F64
(
3.1
)
.to_string
(),
"3.1"
);
assert_eq!
(
F64
(
3.1
)
.to_pretty_str
(),
"3.1"
);
assert_eq!
(
F64
(
-
1.5
)
.to_string
()
.into_string
()
,
"-1.5"
);
assert_eq!
(
F64
(
-
1.5
)
.to_pretty_str
()
.into_string
()
,
"-1.5"
);
assert_eq!
(
F64
(
-
1.5
)
.to_string
(),
"-1.5"
);
assert_eq!
(
F64
(
-
1.5
)
.to_pretty_str
(),
"-1.5"
);
assert_eq!
(
F64
(
0.5
)
.to_string
()
.into_string
()
,
"0.5"
);
assert_eq!
(
F64
(
0.5
)
.to_pretty_str
()
.into_string
()
,
"0.5"
);
assert_eq!
(
F64
(
0.5
)
.to_string
(),
"0.5"
);
assert_eq!
(
F64
(
0.5
)
.to_pretty_str
(),
"0.5"
);
assert_eq!
(
F64
(
f64
::
NAN
)
.to_string
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
NAN
)
.to_pretty_str
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
NAN
)
.to_string
(),
"null"
);
assert_eq!
(
F64
(
f64
::
NAN
)
.to_pretty_str
(),
"null"
);
assert_eq!
(
F64
(
f64
::
INFINITY
)
.to_string
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
INFINITY
)
.to_pretty_str
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
INFINITY
)
.to_string
(),
"null"
);
assert_eq!
(
F64
(
f64
::
INFINITY
)
.to_pretty_str
(),
"null"
);
assert_eq!
(
F64
(
f64
::
NEG_INFINITY
)
.to_string
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
NEG_INFINITY
)
.to_pretty_str
()
.into_string
()
,
"null"
);
assert_eq!
(
F64
(
f64
::
NEG_INFINITY
)
.to_string
(),
"null"
);
assert_eq!
(
F64
(
f64
::
NEG_INFINITY
)
.to_pretty_str
(),
"null"
);
}
#[test]
fn
test_write_str
()
{
assert_eq!
(
String
(
""
.to_string
())
.to_string
()
.into_string
()
,
"
\"\"
"
);
assert_eq!
(
String
(
""
.to_string
())
.to_pretty_str
()
.into_string
()
,
"
\"\"
"
);
assert_eq!
(
String
(
""
.to_string
())
.to_string
(),
"
\"\"
"
);
assert_eq!
(
String
(
""
.to_string
())
.to_pretty_str
(),
"
\"\"
"
);
assert_eq!
(
String
(
"
foo"
.to_string
())
.to_string
()
.into_string
(),
"
\"
foo
\"
"
);
assert_eq!
(
String
(
"
foo"
.to_string
())
.to_pretty_str
()
.into_string
(),
"
\"
foo
\"
"
);
assert_eq!
(
String
(
"
homura"
.to_string
())
.to_string
(),
"
\"
homura
\"
"
);
assert_eq!
(
String
(
"
madoka"
.to_string
())
.to_pretty_str
(),
"
\"
madoka
\"
"
);
}
#[test]
fn
test_write_bool
()
{
assert_eq!
(
Boolean
(
true
)
.to_string
()
.into_string
()
,
"true"
);
assert_eq!
(
Boolean
(
true
)
.to_pretty_str
()
.into_string
()
,
"true"
);
assert_eq!
(
Boolean
(
true
)
.to_string
(),
"true"
);
assert_eq!
(
Boolean
(
true
)
.to_pretty_str
(),
"true"
);
assert_eq!
(
Boolean
(
false
)
.to_string
()
.into_string
()
,
"false"
);
assert_eq!
(
Boolean
(
false
)
.to_pretty_str
()
.into_string
()
,
"false"
);
assert_eq!
(
Boolean
(
false
)
.to_string
(),
"false"
);
assert_eq!
(
Boolean
(
false
)
.to_pretty_str
(),
"false"
);
}
#[test]
fn
test_write_array
()
{
assert_eq!
(
Array
(
vec!
[])
.to_string
()
.into_string
()
,
"[]"
);
assert_eq!
(
Array
(
vec!
[])
.to_pretty_str
()
.into_string
()
,
"[]"
);
assert_eq!
(
Array
(
vec!
[])
.to_string
(),
"[]"
);
assert_eq!
(
Array
(
vec!
[])
.to_pretty_str
(),
"[]"
);
assert_eq!
(
Array
(
vec!
[
Boolean
(
true
)])
.to_string
()
.into_string
()
,
"[true]"
);
assert_eq!
(
Array
(
vec!
[
Boolean
(
true
)])
.to_string
(),
"[true]"
);
assert_eq!
(
Array
(
vec!
[
Boolean
(
true
)])
.to_pretty_str
()
.into_string
()
,
Array
(
vec!
[
Boolean
(
true
)])
.to_pretty_str
(),
"
\
[
\n
\
true
\n
\
...
...
@@ -2564,10 +2560,10 @@ fn test_write_array() {
Null
,
Array
(
vec!
[
String
(
"foo
\n
bar"
.to_string
()),
F64
(
3.5
)])]);
assert_eq!
(
long_test_array
.to_string
()
.into_string
()
,
assert_eq!
(
long_test_array
.to_string
(),
"[false,null,[
\"
foo
\\
nbar
\"
,3.5]]"
);
assert_eq!
(
long_test_array
.to_pretty_str
()
.into_string
()
,
long_test_array
.to_pretty_str
(),
"
\
[
\n
\
false,
\n
\
...
...
@@ -2582,13 +2578,13 @@ fn test_write_array() {
#[test]
fn
test_write_object
()
{
assert_eq!
(
mk_object
(
&
[])
.to_string
()
.into_string
()
,
"{}"
);
assert_eq!
(
mk_object
(
&
[])
.to_pretty_str
()
.into_string
()
,
"{}"
);
assert_eq!
(
mk_object
(
&
[])
.to_string
(),
"{}"
);
assert_eq!
(
mk_object
(
&
[])
.to_pretty_str
(),
"{}"
);
assert_eq!
(
mk_object
(
&
[
(
"a"
.to_string
(),
Boolean
(
true
))
])
.to_string
()
.into_string
()
,
])
.to_string
(),
"{
\"
a
\"
:true}"
);
assert_eq!
(
...
...
@@ -2607,7 +2603,7 @@ fn test_write_object() {
]);
assert_eq!
(
complex_obj
.to_string
()
.into_string
()
,
complex_obj
.to_string
(),
"{
\
\"
b
\"
:[
\
{
\"
c
\"
:
\"\\
f
\\
r
\"
},
\
...
...
@@ -2616,7 +2612,7 @@ fn test_write_object() {
}"
);
assert_eq!
(
complex_obj
.to_pretty_str
()
.into_string
()
,
complex_obj
.to_pretty_str
(),
"
\
{
\n
\
\"
b
\"
: [
\n
\
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录