@@ -16,45 +16,46 @@ void indent(int hierarchy) {
1616 // XXX: 实现setIndentation // XXX vs无高亮?
1717}
1818
19- void printfJsonVal (const struct JsonVal * val , const int hierarchy ) {
19+ void printfJsonVal (const struct JsonVal * val , const int hierarchy , const int color ) {
2020 switch (val -> type ){
2121 case NUMBER :
22- printNumber (val -> val ); break ;
22+ printNumber (val -> val , color ); break ;
2323 case STRING :
24- printString (val -> val ); break ;
24+ printString (val -> val , color ); break ;
2525 case NONE :
26- printNONE (); break ;
26+ printNONE (color ); break ;
2727 case BOOL :
28- printBool (val -> val ); break ;
28+ printBool (val -> val , color ); break ;
2929 case ARRAY :
30- printfArray (val -> arr , hierarchy ); break ;
30+ printfArray (val -> arr , hierarchy , color ); break ;
3131 case OBJECT :
32- printfObject (val -> obj , hierarchy ); break ;
32+ printfObject (val -> obj , hierarchy , color ); break ;
3333 default :
3434 break ;
3535 }
3636}
3737
38- void printJsonVal (const struct JsonVal * val ) {
38+ void printJsonVal (const struct JsonVal * val , const int color ) {
3939 switch (val -> type ) {
4040 case NUMBER :
41- printNumber (val -> val ); break ;
41+ printNumber (val -> val , color ); break ;
4242 case STRING :
43- printString (val -> val ); break ;
43+ printString (val -> val , color ); break ;
4444 case NONE :
45- printNONE (); break ;
45+ printNONE (color ); break ;
4646 case BOOL :
47- printBool (val -> val ); break ;
47+ printBool (val -> val , color ); break ;
4848 case ARRAY :
49- printArray (val -> arr ); break ;
49+ printArray (val -> arr , color ); break ;
5050 case OBJECT :
51- printObject (val -> obj ); break ;
51+ printObject (val -> obj , color ); break ;
5252 default :
5353 break ;
5454 }
5555}
5656
57- void printString (const struct JsonString * str ) {
57+ void printString (const struct JsonString * str , const int color ) {
58+ if (color ) fputs (ANSI_COLOR_GREEN , f );
5859 fputc ('"' , f );
5960 char * reader = str -> str ;
6061 while (* reader ) {
@@ -67,15 +68,20 @@ void printString(const struct JsonString* str) {
6768
6869 reader ++ ;
6970 }
71+ fputc ('"' , f );if (color ) fputs (ANSI_COLOR_RESET , f );
7072
71- fputc ('"' , f );
7273}
7374
74- void printfObject (const struct JsonObj * obj , const int hierarchy ) {
75+ void printfObject (const struct JsonObj * obj , const int hierarchy , const int color ) {
7576 fputs ("{\n" , f );
7677 for (int i = 0 ; i < obj -> size ; i ++ ) {
77- indent (hierarchy + 1 ); printString (obj -> key + i );
78- fputs (": " , f ); printfJsonVal (obj -> value + i , hierarchy + 1 );
78+ indent (hierarchy + 1 );
79+ if (color ) {
80+ fputs (ANSI_COLOR_MAGENTA , f );
81+ printString (obj -> key + i , 0 );
82+ fputs (ANSI_COLOR_RESET , f );
83+ }else printString (obj -> key + i , 0 );
84+ fputs (": " , f ); printfJsonVal (obj -> value + i , hierarchy + 1 , color );
7985 if (i != obj -> size - 1 ) fputs ("," , f );
8086 fputs ("\n" , f );
8187 }
@@ -84,41 +90,53 @@ void printfObject(const struct JsonObj* obj, const int hierarchy) {
8490
8591
8692
87- void printfArray (const struct JsonArray * array , int hierarchy ) {
93+ void printfArray (const struct JsonArray * array , int hierarchy , const int color ) {
8894 fputs ("[\n" , f );
8995 for (int i = 0 ; i < array -> length ; i ++ ) {
90- indent (hierarchy + 1 ); printfJsonVal (& array -> arr [i ], hierarchy + 1 );
96+ indent (hierarchy + 1 ); printfJsonVal (& array -> arr [i ], hierarchy + 1 , color );
9197 if (i != array -> length - 1 ) fputs ("," , f );
9298 fputs ("\n" , f );
9399 }
94100 indent (hierarchy ); fputs ("]" , f );
95101}
96102
97103
98- void printNumber (const struct JsonString * num ) {
104+ void printNumber (const struct JsonString * num , const int color ) {
105+ if (color ) fputs (ANSI_COLOR_CYAN , f );
99106 fputs (num -> str , f );
107+ if (color ) fputs (ANSI_COLOR_RESET , f );
100108}
101109
102- void printBool (const struct JsonString * bl ) {
110+ void printBool (const struct JsonString * bl , const int color ) {
111+ if (color ) fputs (ANSI_COLOR_YELLOW , f );
103112 fputs (bl -> str , f );
113+ if (color ) fputs (ANSI_COLOR_RESET , f );
114+
104115}
105116
106- void printNONE () {
117+ void printNONE (const int color ) {
118+ if (color ) fputs (ANSI_COLOR_YELLOW , f );
107119 fputs ("null" , f );
120+ if (color ) fputs (ANSI_COLOR_RESET , f );
108121}
109122
110- void printObject (const struct JsonObj * obj ){
123+ void printObject (const struct JsonObj * obj , const int color ){
111124 fputc ('{' , f );
112125 for (int i = 0 ; i < obj -> size ; i ++ ) {
113- printString (obj -> key + i ); putc (':' , f );
114- printJsonVal (obj -> value + i );
126+ if (color ) {
127+ fputs (ANSI_COLOR_MAGENTA , f );
128+ printString (obj -> key + i , color );
129+ fputs (ANSI_COLOR_RESET , f );
130+ }else printString (obj -> key + i , color );
131+ putc (':' , f );
132+ printJsonVal (obj -> value + i , color );
115133 if (i != obj -> size - 1 ) fputc (',' , f );
116134 } fputc ('}' , f );
117135}
118- void printArray (const struct JsonArray * array ){
136+ void printArray (const struct JsonArray * array , const int color ){
119137 fputc ('[' , f );
120138 for (int i = 0 ; i < array -> length ; i ++ ) {
121- printJsonVal (array -> arr + i );
139+ printJsonVal (array -> arr + i , color );
122140 if (i != array -> length - 1 ) fputc (',' , f );
123141 }fputc (']' , f );
124142}
0 commit comments