Changeset 46

Show
Ignore:
Timestamp:
06/25/08 01:50:06 (4 months ago)
Author:
baxissimo
Message:

Fixed indentation of JSon dump output.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doost/util/serializer/archive/JsonArchive.d

    r45 r46  
    9696        STORAGETYPE rest = STORAGETYPE.init; 
    9797        uint indent = 0; 
     98        bool line_begin = true; 
    9899 
    99100        //fields of specific class with values: content[name of fields] 
     
    153154 
    154155    /*************************************************************************** 
     156        Return the indentation string for the current indentation level. 
     157        Does not use or modify the line_begin flag. 
     158     **************************************************************************/ 
     159    STORAGETYPE indentString() { 
     160        STORAGETYPE indent_str = STORAGETYPE.init; 
     161        for(int i=0; i<indent; i++) { 
     162            indent_str ~= Skip.indent; 
     163        } 
     164        return indent_str; 
     165    } 
     166 
     167    /*************************************************************************** 
     168        Write the current indentation string to the storage. 
     169        Postcondition: line_begin flag set to false. 
     170     **************************************************************************/ 
     171    bool dumpIndent() { 
     172        line_begin = false; 
     173        storage.put(indentString()); 
     174        return true; 
     175    } 
     176 
     177    /*************************************************************************** 
    155178     **************************************************************************/ 
    156179    bool loadString(VALUE)(ref VALUE value) { 
     
    176199     **************************************************************************/ 
    177200    bool dumpString(VALUE)(ref VALUE value) { 
     201        if (line_begin) dumpIndent(); 
    178202        storage.put( String.sdelimiter ~ 
    179203                            to!(STORAGETYPE)(escape(value)) ~ 
     
    220244     **************************************************************************/ 
    221245    bool dumpBool(VALUE)(ref VALUE value) { 
     246        if (line_begin) dumpIndent(); 
    222247        storage.put((value == true) ? Bool.positive 
    223248                            : Bool.negative); 
     
    239264     **************************************************************************/ 
    240265    bool dumpNumber(VALUE)(ref VALUE value) { 
     266        if (line_begin) dumpIndent(); 
    241267        storage.put(to!(STORAGETYPE)(value)); 
    242268        return true; 
     
    258284     **************************************************************************/ 
    259285    bool dumpChar(VALUE)(ref VALUE value) { 
     286        if (line_begin) dumpIndent(); 
    260287        storage.put("" ~ String.cdelimiter ~ value ~ String.cdelimiter); 
    261288        return true; 
     
    277304     **************************************************************************/ 
    278305    bool dumpElement(VALUE, SEP)(ref VALUE value, SEP sep) { 
     306        if (line_begin) dumpIndent(); 
    279307        storage.put(rest); 
    280308        bool res = traverse(value); 
     
    308336     **************************************************************************/ 
    309337    bool dumpArray(VALUE)(ref VALUE value) { 
     338        if (line_begin) dumpIndent(); 
    310339        storage.put(Array.begin); 
    311340 
     
    342371        storage.put(rest); 
    343372        traverse(key); 
     373        if (line_begin) dumpIndent(); 
    344374        storage.put(Skip.space ~ kvsep ~ Skip.space); 
    345375        traverse(value); 
    346         rest = esep ~ Skip.newline; 
     376        rest = esep ~ Skip.newline ~ indentString(); 
     377        
    347378        return true; 
    348379    } 
     
    383414        ValueType val; 
    384415 
    385         storage.put(Udt.begin); 
     416        if (line_begin) dumpIndent(); 
     417        storage.put(Udt.begin ~ Skip.newline); 
     418        line_begin = true; 
     419        indent++; 
    386420 
    387421        rest = STORAGETYPE.init; 
    388         foreach(k, v; value) 
     422        foreach(k, v; value) { 
    389423            dumpAssociativeElement(k, v, Udt.kvseparator, Udt.separator); 
    390  
     424        } 
     425        indent--; 
     426        storage.put(Skip.newline); 
     427        line_begin = true; 
     428        if (line_begin) dumpIndent(); 
    391429        storage.put(Udt.end); 
    392430 
     
    441479    bool dumpRef(VALUE)(ref VALUE value) { 
    442480        if (value is null) { 
     481            if (line_begin) dumpIndent(); 
    443482            storage.put(Reference.defnull); 
    444483            return true; 
     
    448487            uint symbol = registry.get!(TypeDescription, VALUE).symbol(value); 
    449488            if (symbol > 0) { 
     489                if (line_begin) dumpIndent(); 
    450490                storage.put(Reference.defref ~ to!(STORAGETYPE)(symbol)); 
    451491                return true; 
     
    454494 
    455495        static if (!is(VALUE == class)) { 
     496            if (line_begin) dumpIndent(); 
    456497            storage.put(Reference.pointer); 
    457498        } 
     
    528569     **************************************************************************/ 
    529570    bool dumpFields(VALUE)(ref VALUE value) { 
    530         storage.put( String.sdelimiter 
     571        if (line_begin) dumpIndent(); 
     572        storage.put(String.sdelimiter 
    531573                        ~   VALUE.stringof 
    532574                        ~   String.sdelimiter 
     
    536578                        ~   Udt.begin 
    537579                        ~   Skip.newline); 
     580        line_begin = true; 
     581        indent++; 
    538582 
    539583        if (Udt.ver in memberStructFields) { 
    540  
    541             storage.put( String.sdelimiter 
     584            if (line_begin) dumpIndent(); 
     585            storage.put(String.sdelimiter 
    542586                            ~   Udt.ver 
    543587                            ~   String.sdelimiter 
     
    548592                            ~   Udt.separator 
    549593                            ~   Skip.newline); 
     594            line_begin = true; 
    550595        } 
    551596 
     
    553598        processMemberStructFields(value); 
    554599 
    555         storage.put(Skip.newline 
    556                         ~  Udt.end 
    557                         ~  Skip.newline); 
     600        indent--; 
     601        storage.put(Skip.newline); 
     602        line_begin = true; 
     603        if (line_begin) dumpIndent(); 
     604        storage.put(Udt.end ~  Skip.newline); 
     605        line_begin = true; 
    558606        return true; 
    559607    } 
     
    683731     **************************************************************************/ 
    684732    bool dumpUdt(VALUE)(ref VALUE value) { 
     733        if (line_begin) dumpIndent(); 
    685734        storage.put( Udt.begin ~ Skip.newline); 
     735        indent++; 
     736        line_begin = true; 
    686737 
    687738        processMemberStruct(value); 
     
    690741            foreach(i, BASE; BaseTypeTuple!(VALUE)) { 
    691742                if (is (BASE == interface) || BASE.tupleof.length == 0) continue; 
     743                if (line_begin) dumpIndent(); 
    692744                storage.put(Udt.separator ~ Skip.newline); 
     745                line_begin = true; 
    693746                BASE val = cast(BASE)value; 
    694747                processMemberStruct(val); 
     
    696749        } 
    697750 
     751        indent--; 
     752        if (line_begin) dumpIndent(); 
    698753        storage.put(Udt.end); 
    699754