Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c9188c83
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,发现更多精彩内容 >>
提交
c9188c83
编写于
3月 26, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: fix json PrettyEncoder and add tests
上级
6cf99fa5
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
220 addition
and
33 deletion
+220
-33
src/libstd/json.rs
src/libstd/json.rs
+220
-33
未找到文件。
src/libstd/json.rs
浏览文件 @
c9188c83
...
...
@@ -140,9 +140,9 @@ fn emit_enum_variant(&self, name: &str, _id: uint, _cnt: uint, f: &fn()) {
// we'd have to change the expansion of auto-encode to pass
// those along.
if
(
name
==
~
"Some"
)
{
if
name
==
~
"Some"
{
f
();
}
else
if
(
name
==
~
"None"
)
{
}
else
if
name
==
~
"None"
{
self
.wr
.write_str
(
~
"null"
);
}
else
{
self
.wr
.write_char
(
'['
);
...
...
@@ -250,27 +250,57 @@ fn emit_borrowed(&self, f: &fn()) { f() }
fn
emit_owned
(
&
self
,
f
:
&
fn
())
{
f
()
}
fn
emit_managed
(
&
self
,
f
:
&
fn
())
{
f
()
}
fn
emit_enum
(
&
self
,
name
:
&
str
,
f
:
&
fn
())
{
if
name
!=
"option"
{
fail
!
(
~
"only supports option enum"
)
}
f
()
}
fn
emit_enum_variant
(
&
self
,
_
name
:
&
str
,
id
:
uint
,
_
cnt
:
uint
,
f
:
&
fn
())
{
if
id
==
0
{
fn
emit_enum
(
&
self
,
_
name
:
&
str
,
f
:
&
fn
())
{
f
()
}
fn
emit_enum_variant
(
&
self
,
name
:
&
str
,
_
id
:
uint
,
cnt
:
uint
,
f
:
&
fn
())
{
if
name
==
~
"Some"
{
f
();
}
else
if
name
==
~
"None"
{
self
.emit_nil
();
}
else
{
f
()
self
.wr
.write_char
(
'['
);
self
.indent
+=
2
;
self
.wr
.write_char
(
'\n'
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
escape_str
(
name
));
if
cnt
==
0
{
self
.wr
.write_str
(
",
\n
"
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"[]
\n
"
);
}
else
{
self
.wr
.write_str
(
",
\n
"
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"[
\n
"
);
self
.indent
+=
2
;
f
();
self
.wr
.write_char
(
'\n'
);
self
.indent
-=
2
;
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"]
\n
"
);
}
self
.indent
-=
2
;
self
.wr
.write_char
(
']'
);
}
}
fn
emit_enum_variant_arg
(
&
self
,
_
idx
:
uint
,
f
:
&
fn
())
{
fn
emit_enum_variant_arg
(
&
self
,
idx
:
uint
,
f
:
&
fn
())
{
if
idx
!=
0
{
self
.wr
.write_str
(
",
\n
"
);
}
self
.wr
.write_str
(
spaces
(
self
.indent
));
f
()
}
fn
emit_borrowed_vec
(
&
self
,
_
len
:
uint
,
f
:
&
fn
())
{
self
.wr
.write_char
(
'['
);
self
.indent
+=
2
;
f
();
self
.indent
-=
2
;
self
.wr
.write_char
(
']'
);
fn
emit_borrowed_vec
(
&
self
,
len
:
uint
,
f
:
&
fn
())
{
if
len
==
0
{
self
.wr
.write_str
(
"[]"
);
}
else
{
self
.wr
.write_char
(
'['
);
self
.indent
+=
2
;
f
();
self
.wr
.write_char
(
'\n'
);
self
.indent
-=
2
;
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_char
(
']'
);
}
}
fn
emit_owned_vec
(
&
self
,
len
:
uint
,
f
:
&
fn
())
{
self
.emit_borrowed_vec
(
len
,
f
)
...
...
@@ -292,11 +322,17 @@ fn emit_rec(&self, f: &fn()) {
self
.wr
.write_char
(
'{'
);
self
.indent
+=
2
;
f
();
self
.wr
.write_char
(
'\n'
);
self
.indent
-=
2
;
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_char
(
'}'
);
}
fn
emit_struct
(
&
self
,
_
name
:
&
str
,
_
len
:
uint
,
f
:
&
fn
())
{
self
.emit_rec
(
f
)
fn
emit_struct
(
&
self
,
_
name
:
&
str
,
len
:
uint
,
f
:
&
fn
())
{
if
len
==
0
{
self
.wr
.write_str
(
"{}"
);
}
else
{
self
.emit_rec
(
f
)
}
}
fn
emit_field
(
&
self
,
name
:
&
str
,
idx
:
uint
,
f
:
&
fn
())
{
if
idx
==
0
{
...
...
@@ -1248,6 +1284,34 @@ fn test_write_list() {
])),
~
"[false,null,[
\"
foo
\\
nbar
\"
,3.5]]"
);
}
#[test]
fn
test_write_list_pretty
()
{
assert_eq!
(
to_pretty_str
(
&
List
(
~
[])),
~
"[]"
);
assert_eq!
(
to_pretty_str
(
&
List
(
~
[
Boolean
(
true
)])),
~
"
\
[
\n
\
true
\n
\
]"
);
assert_eq!
(
to_pretty_str
(
&
List
(
~
[
Boolean
(
false
),
Null
,
List
(
~
[
String
(
~
"foo
\n
bar"
),
Number
(
3.5
f
)])
])),
~
"
\
[
\n
\
false,
\n
\
null,
\n
\
[
\n
\
\"
foo
\\
nbar
\"
,
\n
\
3.5
\n
\
]
\n
\
]"
);
}
#[test]
fn
test_write_object
()
{
assert_eq!
(
to_str
(
&
mk_object
(
~
[])),
~
"{}"
);
...
...
@@ -1255,6 +1319,62 @@ fn test_write_object() {
to_str
(
&
mk_object
(
~
[(
~
"a"
,
Boolean
(
true
))])),
~
"{
\"
a
\"
:true}"
);
assert_eq!
(
to_str
(
&
mk_object
(
~
[
(
~
"b"
,
List
(
~
[
mk_object
(
~
[(
~
"c"
,
String
(
~
"
\x0c\r
"
))]),
mk_object
(
~
[(
~
"d"
,
String
(
~
""
))])
]))
])),
~
"{
\
\"
b
\"
:[
\
{
\"
c
\"
:
\"\\
f
\\
r
\"
},
\
{
\"
d
\"
:
\"\"
}
\
]
\
}"
);
let
a
=
mk_object
(
~
[
(
~
"a"
,
Boolean
(
true
)),
(
~
"b"
,
List
(
~
[
mk_object
(
~
[(
~
"c"
,
String
(
~
"
\x0c\r
"
))]),
mk_object
(
~
[(
~
"d"
,
String
(
~
""
))])
]))
]);
// We can't compare the strings directly because the object fields be
// printed in a different order.
let
b
=
from_str
(
to_str
(
&
a
))
.unwrap
();
assert_eq!
(
a
,
b
);
}
#[test]
fn
test_write_object_pretty
()
{
assert_eq!
(
to_pretty_str
(
&
mk_object
(
~
[])),
~
"{
\n
}"
);
assert_eq!
(
to_pretty_str
(
&
mk_object
(
~
[(
~
"a"
,
Boolean
(
true
))])),
~
"
\
{
\n
\
\"
a
\"
: true
\n
\
}"
);
assert_eq!
(
to_pretty_str
(
&
mk_object
(
~
[
(
~
"b"
,
List
(
~
[
mk_object
(
~
[(
~
"c"
,
String
(
~
"
\x0c\r
"
))]),
mk_object
(
~
[(
~
"d"
,
String
(
~
""
))])
]))
])),
~
"
\
{
\n
\
\"
b
\"
: [
\n
\
{
\n
\
\"
c
\"
:
\"\\
f
\\
r
\"\n
\
},
\n
\
{
\n
\
\"
d
\"
:
\"\"\n
\
}
\n
\
]
\n
\
}"
);
let
a
=
mk_object
(
~
[
(
~
"a"
,
Boolean
(
true
)),
(
~
"b"
,
List
(
~
[
...
...
@@ -1268,24 +1388,71 @@ fn test_write_object() {
assert_eq!
(
a
,
b
);
}
#[auto_encode]
enum
Animal
{
Dog
,
Frog
(
~
str
,
int
)
}
#[test]
fn
test_write_enum
()
{
fn
test_write_enum_no_args
()
{
let
animal
=
Dog
;
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
&
Encoder
(
wr
)
as
&
serialize
::
Encoder
;
do
encoder
.emit_enum
(
~
"animal"
)
{
do
encoder
.emit_enum_variant
(
~
"frog"
,
37
,
1242
)
{
// name of frog:
do
encoder
.emit_enum_variant_arg
(
0
)
{
encoder
.emit_owned_str
(
~
"Henry"
)
}
// mass of frog in grams:
do
encoder
.emit_enum_variant_arg
(
1
)
{
encoder
.emit_int
(
349
);
}
}
}
let
encoder
=
Encoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"[
\"
Dog
\"
,[]]"
);
}
#[test]
fn
test_write_enum_no_args_pretty
()
{
let
animal
=
Dog
;
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
PrettyEncoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"
\
[
\n
\
\"
Dog
\"
,
\n
\
[]
\n
\
]"
);
}
#[test]
fn
test_write_enum_multiple_args
()
{
let
animal
=
Frog
(
~
"Henry"
,
349
);
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
Encoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"[
\"
frog
\"
,[
\"
Henry
\"
,349]]"
);
assert_eq!
(
s
,
~
"[
\"
Frog
\"
,[
\"
Henry
\"
,349]]"
);
}
#[test]
fn
test_write_enum_multiple_args_pretty
()
{
let
animal
=
Frog
(
~
"Henry"
,
349
);
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
PrettyEncoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"
\
[
\n
\
\"
Frog
\"
,
\n
\
[
\n
\
\"
Henry
\"
,
\n
\
349
\n
\
]
\n
\
]"
);
}
#[test]
...
...
@@ -1298,6 +1465,16 @@ fn test_write_some() {
assert_eq!
(
s
,
~
"
\"
jodhpurs
\"
"
);
}
#[test]
fn
test_write_some_pretty
()
{
let
value
=
Some
(
~
"jodhpurs"
);
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
PrettyEncoder
(
wr
);
value
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"
\"
jodhpurs
\"
"
);
}
#[test]
fn
test_write_none
()
{
let
value
:
Option
<~
str
>
=
None
;
...
...
@@ -1308,6 +1485,16 @@ fn test_write_none() {
assert_eq!
(
s
,
~
"null"
);
}
#[test]
fn
test_write_none_pretty
()
{
let
value
:
Option
<~
str
>
=
None
;
let
s
=
do
io
::
with_str_writer
|
wr
|
{
let
encoder
=
Encoder
(
wr
);
value
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"null"
);
}
#[test]
fn
test_trailing_characters
()
{
assert_eq!
(
from_str
(
~
"nulla"
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录