Skip to content

Commit 0878399

Browse files
committed
---
yaml --- r: 16080 b: refs/heads/try c: ed5af70 h: refs/heads/master v: v3
1 parent 8c98f20 commit 0878399

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: e45ed323c99cf105b807ca79c5561570ec5c93c8
5+
refs/heads/try: ed5af70a3634915004f24c391a33cbebb8beb7bd
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libstd/json.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export to_str;
1616
export from_reader;
1717
export from_str;
1818
export eq;
19+
export to_json;
1920

2021
export num;
2122
export string;
@@ -498,6 +499,110 @@ fn eq(value0: json, value1: json) -> bool {
498499
}
499500
}
500501

502+
iface to_json { fn to_json() -> json; }
503+
504+
impl of to_json for json {
505+
fn to_json() -> json { self }
506+
}
507+
508+
impl of to_json for i8 {
509+
fn to_json() -> json { num(self as float) }
510+
}
511+
512+
impl of to_json for i16 {
513+
fn to_json() -> json { num(self as float) }
514+
}
515+
516+
impl of to_json for i32 {
517+
fn to_json() -> json { num(self as float) }
518+
}
519+
520+
impl of to_json for i64 {
521+
fn to_json() -> json { num(self as float) }
522+
}
523+
524+
impl of to_json for u8 {
525+
fn to_json() -> json { num(self as float) }
526+
}
527+
528+
impl of to_json for u16 {
529+
fn to_json() -> json { num(self as float) }
530+
}
531+
532+
impl of to_json for u32 {
533+
fn to_json() -> json { num(self as float) }
534+
}
535+
536+
impl of to_json for u64 {
537+
fn to_json() -> json { num(self as float) }
538+
}
539+
540+
impl of to_json for float {
541+
fn to_json() -> json { num(self) }
542+
}
543+
544+
impl of to_json for f32 {
545+
fn to_json() -> json { num(self as float) }
546+
}
547+
548+
impl of to_json for f64 {
549+
fn to_json() -> json { num(self as float) }
550+
}
551+
552+
impl of to_json for () {
553+
fn to_json() -> json { null }
554+
}
555+
556+
impl of to_json for bool {
557+
fn to_json() -> json { boolean(self) }
558+
}
559+
560+
impl of to_json for str {
561+
fn to_json() -> json { string(self) }
562+
}
563+
564+
impl <A: to_json copy, B: to_json copy> of to_json for (A, B) {
565+
fn to_json() -> json {
566+
let (a, b) = self;
567+
list([a.to_json(), b.to_json()])
568+
}
569+
}
570+
571+
impl <A: to_json copy, B: to_json copy, C: to_json copy>
572+
of to_json for (A, B, C) {
573+
fn to_json() -> json {
574+
let (a, b, c) = self;
575+
list([a.to_json(), b.to_json(), c.to_json()])
576+
}
577+
}
578+
579+
impl <A: to_json> of to_json for [A] {
580+
fn to_json() -> json { list(self.map { |elt| elt.to_json() }) }
581+
}
582+
583+
impl <A: to_json copy> of to_json for hashmap<str, A> {
584+
fn to_json() -> json {
585+
let d = map::str_hash();
586+
for self.each() { |key, value|
587+
d.insert(key, value.to_json());
588+
}
589+
dict(d)
590+
}
591+
}
592+
593+
impl <A: to_json> of to_json for option<A> {
594+
fn to_json() -> json {
595+
alt self {
596+
none { null }
597+
some(value) { value.to_json() }
598+
}
599+
}
600+
}
601+
602+
impl of to_str::to_str for json {
603+
fn to_str() -> str { to_str(self) }
604+
}
605+
501606
#[cfg(test)]
502607
mod tests {
503608
fn mk_dict(items: [(str, json)]) -> json {

0 commit comments

Comments
 (0)