Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c952c044
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,发现更多精彩内容 >>
提交
c952c044
编写于
2月 08, 2013
作者:
J
John Clements
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
re-special-case Option in JSON encoding
上级
f91160b6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
64 addition
and
9 deletion
+64
-9
src/libstd/json.rs
src/libstd/json.rs
+64
-9
未找到文件。
src/libstd/json.rs
浏览文件 @
c952c044
...
...
@@ -126,16 +126,42 @@ fn emit_enum(&self, _name: &str, f: fn()) {
}
fn
emit_enum_variant
(
&
self
,
name
:
&
str
,
_
id
:
uint
,
_
cnt
:
uint
,
f
:
fn
())
{
// emitting enums as arrays where the first
// element provides the enum variant name
self
.wr
.write_char
(
'['
);
self
.wr
.write_str
(
escape_str
(
name
));
f
();
self
.wr
.write_char
(
']'
);
// encoding of enums is special-cased for Option. Specifically:
// Some(34) => 34
// None => null
// other enums are encoded as vectors:
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
// the default expansion for enums is more verbose than I'd like;
// specifically, the inner pair of brackets seems superfluous,
// BUT the design of the enumeration framework and the requirements
// of the special-case for Option mean that a first argument must
// be encoded "naked"--with no commas--and that the option name
// can't be followed by just a comma, because there might not
// be any elements in the tuple.
// FIXME : this would be more precise and less frightening
// with fully-qualified option names. To get that information,
// we'd have to change the expansion of auto-encode to pass those along.
if
(
name
==
~
"Some"
)
{
f
();
}
else
if
(
name
==
~
"None"
)
{
self
.wr
.write_str
(
~
"null"
);
}
else
{
self
.wr
.write_char
(
'['
);
self
.wr
.write_str
(
escape_str
(
name
));
self
.wr
.write_char
(
','
);
self
.wr
.write_char
(
'['
);
f
();
self
.wr
.write_char
(
']'
);
self
.wr
.write_char
(
']'
);
}
}
fn
emit_enum_variant_arg
(
&
self
,
_
idx
:
uint
,
f
:
fn
())
{
self
.wr
.write_char
(
','
);
fn
emit_enum_variant_arg
(
&
self
,
idx
:
uint
,
f
:
fn
())
{
if
(
idx
!=
0
)
{
self
.wr
.write_char
(
','
);}
f
();
}
...
...
@@ -1286,7 +1312,36 @@ fn test_write_enum () {
}
}
check_equal
(
str
::
from_bytes
(
bw
.bytes.data
),
~
"[
\"
frog
\"
,
\"
Henry
\"
,349]"
);
~
"[
\"
frog
\"
,[
\"
Henry
\"
,349]]"
);
}
#[test]
fn
test_write_some
()
{
let
bw
=
@
io
::
BytesWriter
{
bytes
:
dvec
::
DVec
(),
pos
:
0
};
let
bww
:
@
io
::
Writer
=
(
bw
as
@
io
::
Writer
);
let
encoder
=
(
@
Encoder
(
bww
)
as
@
serialize
::
Encoder
);
do
encoder
.emit_enum
(
~
"Option"
)
{
do
encoder
.emit_enum_variant
(
~
"Some"
,
37
,
1242
)
{
do
encoder
.emit_enum_variant_arg
(
0
)
{
encoder
.emit_owned_str
(
~
"jodhpurs"
)
}
}
}
check_equal
(
str
::
from_bytes
(
bw
.bytes.data
),
~
"
\"
jodhpurs
\"
"
);
}
#[test]
fn
test_write_none
()
{
let
bw
=
@
io
::
BytesWriter
{
bytes
:
dvec
::
DVec
(),
pos
:
0
};
let
bww
:
@
io
::
Writer
=
(
bw
as
@
io
::
Writer
);
let
encoder
=
(
@
Encoder
(
bww
)
as
@
serialize
::
Encoder
);
do
encoder
.emit_enum
(
~
"Option"
)
{
do
encoder
.emit_enum_variant
(
~
"None"
,
37
,
1242
)
{
}
}
check_equal
(
str
::
from_bytes
(
bw
.bytes.data
),
~
"null"
);
}
#[test]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录