{ "Comment":"My comment", "Count":10, "DiskParam": { "DB":10.000000, "DBAngle":1.234000 }, "Range":true, "Blades": [ { "Caption":"A", "Value":65 }, { "Caption":"B", "Value":66 }, { "Caption":"C", "Value":67 } ], "Slots": [ 0,1,2 ] }
fprintf(pOut, "{\n"); fprintf(pOut, " \"Comment\":\"%s\"", Header->Comment); fprintf(pOut, ",\n \"NumSt\":%d", Header->NumSt); // fprintf(pOut, ",\n \"DBMax\":%lf", Header->DBMax); fprintf(pOut, ",\n \"Range\":%s", Header->Range?"true":"false"); fprintf(pOut, ",\n \"Blades\":\n ["); for(int i=0; i<Header->Count; i++) { TElement &e=Element[i]; fprintf(pOut, i?",\n {":"\n {"); fprintf(pOut, "\"Caption\":\"%s\"", e.Caption); fprintf(pOut, ",\"Value\":%lf", e.BaseChar); fprintf(pOut, "}"); } fprintf(pOut, "\n ]"); // fprintf(pOut, "\n}");
Key("1"); Value("1"); Key("2"); Value("2"); Object("1"); Key("3"); Value("3"); //3,4 1 Key("4"); Value("4"); Array("1"); Key("5"); Value("5"); //5...N 1 Key("6"); Value("6"); ... Key("N"); Value("N");
#define TCF_USED 1 class TTagCloser { public: TTagCloser *Owner; static TTagCloser *Current; static int Depth; int Flags; int Count; int operator()(){Flags^=TCF_USED; return Flags&TCF_USED;} TTagCloser(){Count=Flags=0; Owner=Current; Current=this; Depth++;} ~TTagCloser(){Depth--; Current=Owner;} }; TTagCloser *TTagCloser::Current=NULL; int TTagCloser::Depth=-1;
#define TCF_OBJECT 4 #define TCF_ARRAY 2 class TJsonTagCloser:public TTagCloser { public: void WriteTab(); void WriteInt(int); void WriteDouble(double); void WriteStr(char *); TJsonTagCloser(char *Key); }; //---------------------------------------------------------------------------- TJsonTagCloser::TJsonTagCloser(char *Key):TTagCloser() { if(Owner) { if(Owner->Count) WriteStr(","); if(Owner->Flags&TCF_ARRAY) { if(!Owner->Count) WriteTab(); } else { WriteTab(); WriteStr("\""); if(Key) WriteStr(Key); WriteStr("\":"); } Owner->Count++; } }
#include <stdio.h> void TJsonTagCloser::WriteTab(){printf("\n%*s", Depth*2, "");} void TJsonTagCloser::WriteInt(int Value){printf("%d", Value);} void TJsonTagCloser::WriteDouble(double Value){printf("%lf", Value);} void TJsonTagCloser::WriteStr(char *Value){printf("%s", Value);}
class TJsonArray:public TJsonTagCloser { public: TJsonArray(char *Key); ~TJsonArray(); }; class TJsonObject:public TJsonTagCloser { public: TJsonObject(char *Key); ~TJsonObject(); }; class TJsonValue:public TJsonTagCloser { public: TJsonValue(char *Key, int Value):TJsonTagCloser(Key){WriteInt (Value);} TJsonValue(char *Key, double Value):TJsonTagCloser(Key){WriteDouble(Value);} TJsonValue(char *Key, bool Value):TJsonTagCloser(Key){WriteStr((char *)(Value?"true":"false"));} TJsonValue(char *Key, char *Value); }; TJsonArray::TJsonArray(char *Key):TJsonTagCloser(Key) { Flags|=TCF_ARRAY; if(Owner && (!(Owner->Flags&TCF_ARRAY) || Owner->Count>1)) WriteTab(); WriteStr("["); } TJsonArray::~TJsonArray() { WriteTab(); WriteStr("]"); } //---------------------------------------------------------------------------- TJsonObject::TJsonObject(char *Key):TJsonTagCloser(Key) { Flags|=TCF_OBJECT; if(Owner && (!(Owner->Flags&TCF_ARRAY) || Owner->Count>1)) WriteTab(); WriteStr("{"); } TJsonObject::~TJsonObject() { WriteTab(); WriteStr("}"); } TJsonValue::TJsonValue(char *Key, char *Value):TJsonTagCloser(Key) { if(Value) { WriteStr("\""); WriteStr(Value); WriteStr("\""); } else WriteStr("null"); }
#define ARRAY(k) for(TJsonArray array(k); array();) #define OBJECT(k) for(TJsonObject object(k); object();) #define VALUE(k,v) {TJsonValue value(k,v);}
void main() { OBJECT("") { VALUE("Comment", "My comment"); VALUE("Count", 10); OBJECT("DiskParam") { VALUE("DB", 10.0); VALUE("DBAngle", 1.234); } VALUE("Range", true); ARRAY("Blades") { for(int i='A'; i<'A'+3; i++) OBJECT("") { VALUE("Caption", (char *)&i); VALUE("Value", i); } } ARRAY("Slots") for(int i=0; i<3; i++) VALUE("", i); } }
Source: https://habr.com/ru/post/230079/
All Articles