Skip to content

Commit ed5af70

Browse files
committed
std: add json::to_str and json::to_json iface.
1 parent e45ed32 commit ed5af70

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

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)