Skip to content

Commit 58a90c4

Browse files
author
Oliver Schneider
committed
make next commit more readable
1 parent 0980eaf commit 58a90c4

File tree

1 file changed

+0
-233
lines changed

1 file changed

+0
-233
lines changed

src/libserialize/json.rs

-233
Original file line numberDiff line numberDiff line change
@@ -443,20 +443,6 @@ fn fmt_number_or_null(v: f64) -> string::String {
443443
}
444444
}
445445

446-
/// A structure for implementing serialization to JSON.
447-
pub struct Encoder<'a> {
448-
writer: &'a mut (fmt::Writer+'a),
449-
is_emitting_map_key: bool,
450-
}
451-
452-
impl<'a> Encoder<'a> {
453-
/// Creates a new JSON encoder whose output will be written to the writer
454-
/// specified.
455-
pub fn new(writer: &'a mut fmt::Writer) -> Encoder<'a> {
456-
Encoder { writer: writer, is_emitting_map_key: false, }
457-
}
458-
}
459-
460446
macro_rules! emit_enquoted_if_mapkey {
461447
($enc:ident,$e:expr) => {
462448
if $enc.is_emitting_map_key {
@@ -469,225 +455,6 @@ macro_rules! emit_enquoted_if_mapkey {
469455
}
470456
}
471457

472-
impl<'a> ::Encoder for Encoder<'a> {
473-
type Error = EncoderError;
474-
475-
fn emit_nil(&mut self) -> EncodeResult {
476-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
477-
try!(write!(self.writer, "null"));
478-
Ok(())
479-
}
480-
481-
fn emit_uint(&mut self, v: uint) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
482-
fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
483-
fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
484-
fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
485-
fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
486-
487-
fn emit_int(&mut self, v: int) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
488-
fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
489-
fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
490-
fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
491-
fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
492-
493-
fn emit_bool(&mut self, v: bool) -> EncodeResult {
494-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
495-
if v {
496-
try!(write!(self.writer, "true"));
497-
} else {
498-
try!(write!(self.writer, "false"));
499-
}
500-
Ok(())
501-
}
502-
503-
fn emit_f64(&mut self, v: f64) -> EncodeResult {
504-
emit_enquoted_if_mapkey!(self, fmt_number_or_null(v))
505-
}
506-
fn emit_f32(&mut self, v: f32) -> EncodeResult {
507-
self.emit_f64(v as f64)
508-
}
509-
510-
fn emit_char(&mut self, v: char) -> EncodeResult {
511-
escape_char(self.writer, v)
512-
}
513-
fn emit_str(&mut self, v: &str) -> EncodeResult {
514-
escape_str(self.writer, v)
515-
}
516-
517-
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
518-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
519-
{
520-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
521-
f(self)
522-
}
523-
524-
fn emit_enum_variant<F>(&mut self,
525-
name: &str,
526-
_id: uint,
527-
cnt: uint,
528-
f: F) -> EncodeResult where
529-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
530-
{
531-
// enums are encoded as strings or objects
532-
// Bunny => "Bunny"
533-
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
534-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
535-
if cnt == 0 {
536-
escape_str(self.writer, name)
537-
} else {
538-
try!(write!(self.writer, "{{\"variant\":"));
539-
try!(escape_str(self.writer, name));
540-
try!(write!(self.writer, ",\"fields\":["));
541-
try!(f(self));
542-
try!(write!(self.writer, "]}}"));
543-
Ok(())
544-
}
545-
}
546-
547-
fn emit_enum_variant_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where
548-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
549-
{
550-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
551-
if idx != 0 {
552-
try!(write!(self.writer, ","));
553-
}
554-
f(self)
555-
}
556-
557-
fn emit_enum_struct_variant<F>(&mut self,
558-
name: &str,
559-
id: uint,
560-
cnt: uint,
561-
f: F) -> EncodeResult where
562-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
563-
{
564-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
565-
self.emit_enum_variant(name, id, cnt, f)
566-
}
567-
568-
fn emit_enum_struct_variant_field<F>(&mut self,
569-
_: &str,
570-
idx: uint,
571-
f: F) -> EncodeResult where
572-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
573-
{
574-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
575-
self.emit_enum_variant_arg(idx, f)
576-
}
577-
578-
fn emit_struct<F>(&mut self, _: &str, _: uint, f: F) -> EncodeResult where
579-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
580-
{
581-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
582-
try!(write!(self.writer, "{{"));
583-
try!(f(self));
584-
try!(write!(self.writer, "}}"));
585-
Ok(())
586-
}
587-
588-
fn emit_struct_field<F>(&mut self, name: &str, idx: uint, f: F) -> EncodeResult where
589-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
590-
{
591-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
592-
if idx != 0 { try!(write!(self.writer, ",")); }
593-
try!(escape_str(self.writer, name));
594-
try!(write!(self.writer, ":"));
595-
f(self)
596-
}
597-
598-
fn emit_tuple<F>(&mut self, len: uint, f: F) -> EncodeResult where
599-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
600-
{
601-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
602-
self.emit_seq(len, f)
603-
}
604-
fn emit_tuple_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where
605-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
606-
{
607-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
608-
self.emit_seq_elt(idx, f)
609-
}
610-
611-
fn emit_tuple_struct<F>(&mut self, _name: &str, len: uint, f: F) -> EncodeResult where
612-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
613-
{
614-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
615-
self.emit_seq(len, f)
616-
}
617-
fn emit_tuple_struct_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where
618-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
619-
{
620-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
621-
self.emit_seq_elt(idx, f)
622-
}
623-
624-
fn emit_option<F>(&mut self, f: F) -> EncodeResult where
625-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
626-
{
627-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
628-
f(self)
629-
}
630-
fn emit_option_none(&mut self) -> EncodeResult {
631-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
632-
self.emit_nil()
633-
}
634-
fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
635-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
636-
{
637-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
638-
f(self)
639-
}
640-
641-
fn emit_seq<F>(&mut self, _len: uint, f: F) -> EncodeResult where
642-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
643-
{
644-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
645-
try!(write!(self.writer, "["));
646-
try!(f(self));
647-
try!(write!(self.writer, "]"));
648-
Ok(())
649-
}
650-
651-
fn emit_seq_elt<F>(&mut self, idx: uint, f: F) -> EncodeResult where
652-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
653-
{
654-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
655-
if idx != 0 {
656-
try!(write!(self.writer, ","));
657-
}
658-
f(self)
659-
}
660-
661-
fn emit_map<F>(&mut self, _len: uint, f: F) -> EncodeResult where
662-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
663-
{
664-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
665-
try!(write!(self.writer, "{{"));
666-
try!(f(self));
667-
try!(write!(self.writer, "}}"));
668-
Ok(())
669-
}
670-
671-
fn emit_map_elt_key<F>(&mut self, idx: uint, f: F) -> EncodeResult where
672-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
673-
{
674-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
675-
if idx != 0 { try!(write!(self.writer, ",")) }
676-
self.is_emitting_map_key = true;
677-
try!(f(self));
678-
self.is_emitting_map_key = false;
679-
Ok(())
680-
}
681-
682-
fn emit_map_elt_val<F>(&mut self, _idx: uint, f: F) -> EncodeResult where
683-
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
684-
{
685-
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
686-
try!(write!(self.writer, ":"));
687-
f(self)
688-
}
689-
}
690-
691458
/// Another encoder for JSON, but prints out human-readable JSON instead of
692459
/// compact data
693460
pub struct PrettyEncoder<'a> {

0 commit comments

Comments
 (0)