SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
json.cpp
Go to the documentation of this file.
1 // Json-Value
4  JsonValType((TJsonValType)(TInt(SIn).Val)), Bool(SIn),
5  Num(SIn), Str(SIn), ValV(SIn), KeyValH(SIn) { }
6 
7 void TJsonVal::Save(TSOut& SOut) const {
8  TInt((int)JsonValType).Save(SOut);
9  Bool.Save(SOut); Num.Save(SOut);
10  Str.Save(SOut); ValV.Save(SOut);
11  KeyValH.Save(SOut);
12 }
13 
15  return GetStrFromVal(this);
16 }
17 
18 bool TJsonVal::operator==(const TJsonVal& JsonVal) const {
19  return JsonValType == JsonVal.JsonValType &&
20  Bool == JsonVal.Bool &&
21  Num == JsonVal.Num &&
22  Str == JsonVal.Str &&
23  ValV == JsonVal.ValV &&
24  KeyValH == JsonVal.KeyValH;
25 }
26 
27 bool TJsonVal::operator!=(const TJsonVal& JsonVal) const {
28  return !(*this == JsonVal);
29 }
30 
31 void TJsonVal::AddToObj(const PJsonVal& Val) {
32  EAssert(Val->IsObj());
33  int KeyId = Val->KeyValH.FFirstKeyId();
34  while (Val->KeyValH.FNextKeyId(KeyId)) {
35  AddToObj(Val->KeyValH.GetKey(KeyId), Val->KeyValH[KeyId]);
36  }
37 }
38 
40  PJsonVal Val = TJsonVal::NewArr();
41  for (int ValN = 0; ValN < ValV.Len(); ValN++) {
42  Val->AddToArr(ValV[ValN]);
43  }
44  return Val;
45 }
46 
48  PJsonVal Val = TJsonVal::NewArr();
49  for (int IntN = 0; IntN < IntV.Len(); IntN++) {
50  Val->AddToArr(TJsonVal::NewNum((double)IntV[IntN]));
51  }
52  return Val;
53 }
54 
56  PJsonVal Val = TJsonVal::NewArr();
57  for (int FltN = 0; FltN < FltV.Len(); FltN++) {
58  Val->AddToArr(TJsonVal::NewNum(FltV[FltN]));
59  }
60  return Val;
61 }
62 
64  PJsonVal Val = TJsonVal::NewArr();
65  for (int StrN = 0; StrN < StrV.Len(); StrN++) {
66  Val->AddToArr(TJsonVal::NewStr(StrV[StrN]));
67  }
68  return Val;
69 }
70 
72  PJsonVal Val = TJsonVal::NewArr();
73  Val->AddToArr(TJsonVal::NewNum(FltPr.Val1));
74  Val->AddToArr(TJsonVal::NewNum(FltPr.Val2));
75  return Val;
76 }
77 
78 PJsonVal TJsonVal::GetObjKey(const TStr& Key) const {
79  EAssert(IsObj());
80  EAssert(IsObjKey(Key));
81  return KeyValH.GetDat(Key);
82 }
83 
84 PJsonVal TJsonVal::GetObjKey(const char *Key) const {
85  EAssert(IsObj());
86  EAssert(IsObjKey(Key));
87  return KeyValH.GetDat(Key);
88 }
89 
90 bool TJsonVal::GetObjBool(const TStr& Key, const bool& DefBool) const {
91  EAssert(IsObj());
92  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetBool() : DefBool;
93 }
94 
95 bool TJsonVal::GetObjBool(const char *Key, const bool& DefBool) const {
96  EAssert(IsObj());
97  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetBool() : DefBool;
98 }
99 
100 double TJsonVal::GetObjNum(const TStr& Key, const double& DefNum) const {
101  EAssert(IsObj());
102  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetNum() : DefNum;
103 }
104 
105 double TJsonVal::GetObjNum(const char *Key, const double& DefNum) const {
106  EAssert(IsObj());
107  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetNum() : DefNum;
108 }
109 
110 TStr TJsonVal::GetObjStr(const TStr& Key, const TStr& DefStr) const {
111  EAssert(IsObj());
112  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetStr() : DefStr;
113 }
114 
115 TStr TJsonVal::GetObjStr(const char *Key, const TStr& DefStr) const {
116  EAssert(IsObj());
117  return (IsObjKey(Key)) ? KeyValH.GetDat(Key)->GetStr() : DefStr;
118 }
119 
122  PJsonVal Val=TJsonVal::New();
123  if ((Lx.Sym==syIdStr)&&(Lx.Str=="null")){
124  Val->PutNull(); Lx.GetSym();
125  } else if ((Lx.Sym==syIdStr)&&(Lx.Str=="true")){
126  Val->PutBool(true); Lx.GetSym();
127  } else if ((Lx.Sym==syIdStr)&&(Lx.Str=="false")){
128  Val->PutBool(false); Lx.GetSym();
129  } else if (Lx.Sym==syFlt){
130  Val->PutNum(Lx.Flt); Lx.GetSym();
131  } else if (Lx.Sym==syQStr){
132  Val->PutStr(Lx.Str); Lx.GetSym();
133  } else if (Lx.Sym==syLBracket){
134  Val->PutArr(); Lx.GetSym(ValExpect); // added ValExpect to correctyl parse arrays of floats
135  if (Lx.Sym!=syRBracket){
136  forever{
137  PJsonVal SubVal=TJsonVal::GetValFromLx(Lx);
138  Val->AddToArr(SubVal);
139  if (Lx.Sym==syComma){Lx.GetSym(ValExpect);}
140  else if (Lx.Sym==syRBracket){break;}
141  else {TExcept::Throw("JSON Array not properly formed.");}
142  }
143  }
144  Lx.GetSym();
145  } else if (Lx.Sym==syLBrace){
146  Val->PutObj(); Lx.GetSym(TFSet()|syRBrace|syQStr);
147  if (Lx.Sym!=syRBrace){
148  forever{
149  TStr SubKey=Lx.Str;
150  Lx.GetSym(syColon);
151  Lx.GetSym(ValExpect);
152  PJsonVal SubVal=TJsonVal::GetValFromLx(Lx);
153  Val->AddToObj(SubKey, SubVal);
154  if (Lx.Sym==syComma){Lx.GetSym(TFSet()|syQStr);}
155  else if (Lx.Sym==syRBrace){break;}
156  else {TExcept::Throw("JSON Object not properly formed.");}
157  }
158  }
159  Lx.GetSym();
160  } else {
161  TExcept::Throw("Unexpected JSON symbol.");
162  }
163  return Val;
164 }
165 
168  PJsonVal Val;
169  //bool Ok=true;
170  TStr MsgStr="Ok";
171  try {
173  Val=GetValFromLx(Lx);
174  }
175  catch (PExcept Except){
176  //Ok=false;
177  MsgStr=Except->GetMsgStr();
178  Val=TJsonVal::New();
179  }
180  return Val;
181 }
182 
184  PSIn SIn=TStrIn::New(JsonStr);
185  return GetValFromSIn(SIn);
186 }
187 
188 void TJsonVal::AddEscapeChAFromStr(const TStr& Str, TChA& ChA){
189  if (TUnicodeDef::IsDef()) {
190  // parse the UTF8 string
191  TIntV UStr; TUnicodeDef::GetDef()->DecodeUtf8(Str, UStr);
192  // escape the string
193  for (int ChN = 0; ChN < UStr.Len(); ChN++) {
194  const int UCh = UStr[ChN];
195  if (UCh < 0x80) {
196  // 7-bit ascii
197  const char Ch = (char)UCh;
198  switch (Ch) {
199  case '"' : ChA.AddCh('\\'); ChA.AddCh('"'); break;
200  case '\\' : ChA.AddCh('\\'); ChA.AddCh('\\'); break;
201  case '/' : ChA.AddCh('\\'); ChA.AddCh('/'); break;
202  case '\b' : ChA.AddCh('\\'); ChA.AddCh('b'); break;
203  case '\f' : ChA.AddCh('\\'); ChA.AddCh('f'); break;
204  case '\n' : ChA.AddCh('\\'); ChA.AddCh('n'); break;
205  case '\r' : ChA.AddCh('\\'); ChA.AddCh('r'); break;
206  case '\t' : ChA.AddCh('\\'); ChA.AddCh('t'); break;
207  default :
208  ChA.AddCh(Ch);
209  }
210  } else {
211  // escape
212  ChA += "\\u";
213  ChA += TStr::Fmt("%04x", UCh);
214  }
215  }
216  } else {
217  // escape the string
218  for (int ChN = 0; ChN < Str.Len(); ChN++) {
219  const char Ch = Str[ChN];
220  if ((Ch & 0x80) == 0) {
221  // 7-bit ascii
222  switch (Ch) {
223  case '"' : ChA.AddCh('\\'); ChA.AddCh('"'); break;
224  case '\\' : ChA.AddCh('\\'); ChA.AddCh('\\'); break;
225  case '/' : ChA.AddCh('\\'); ChA.AddCh('/'); break;
226  case '\b' : ChA.AddCh('\\'); ChA.AddCh('b'); break;
227  case '\f' : ChA.AddCh('\\'); ChA.AddCh('f'); break;
228  case '\n' : ChA.AddCh('\\'); ChA.AddCh('n'); break;
229  case '\r' : ChA.AddCh('\\'); ChA.AddCh('r'); break;
230  case '\t' : ChA.AddCh('\\'); ChA.AddCh('t'); break;
231  default : ChA.AddCh(Ch);
232  }
233  } else {
234  // escape
235  ChA += "\\u";
236  ChA += TStr::Fmt("%02x", (int)Ch);
237  }
238  }
239  }
240 }
241 
242 void TJsonVal::AddQChAFromStr(const TStr& Str, TChA& ChA){
243  ChA+="\"";
244  AddEscapeChAFromStr(Str, ChA);
245  ChA+="\"";
246 }
247 
248 void TJsonVal::GetChAFromVal(const PJsonVal& Val, TChA& ChA){
249  switch (Val->GetJsonValType()){
250  case jvtNull:
251  ChA+="null"; break;
252  case jvtBool:
253  if (Val->GetBool()){ChA+="true";} else {ChA+="false";} break;
254  case jvtNum:
255  ChA+=TStr::Fmt("%f", Val->GetNum()); break;
256  case jvtStr:
257  AddQChAFromStr(Val->GetStr(), ChA); break;
258  case jvtArr:
259  ChA+="[";
260  for (int ArrValN=0; ArrValN<Val->GetArrVals(); ArrValN++){
261  if (ArrValN>0){ChA+=", ";}
262  GetChAFromVal(Val->GetArrVal(ArrValN), ChA);
263  }
264  ChA+="]";
265  break;
266  case jvtObj:
267  ChA+="{";
268  for (int ObjKeyN=0; ObjKeyN<Val->GetObjKeys(); ObjKeyN++){
269  if (ObjKeyN>0){ChA+=", ";}
270  TStr ObjKey; PJsonVal ObjVal; Val->GetObjKeyVal(ObjKeyN, ObjKey, ObjVal);
271  AddQChAFromStr(ObjKey, ChA);
272  ChA+=":";
273  GetChAFromVal(ObjVal, ChA);
274  }
275  ChA+="}";
276  break;
277  default: TExcept::Throw("Error parsing json");
278  }
279 }
280 
282  TChA ChA;
283  GetChAFromVal(Val, ChA);
284  return ChA;
285 }
Definition: json.h:9
Definition: lx.h:127
Definition: lx.h:50
static PJsonVal New()
Definition: json.h:20
int Len() const
Definition: dt.h:490
static void GetChAFromVal(const PJsonVal &Val, TChA &ChA)
Definition: json.cpp:248
Definition: lx.h:50
void Save(TSOut &SOut) const
Definition: dt.h:1153
#define forever
Definition: bd.h:6
TStr SaveStr()
Definition: json.cpp:14
void AddCh(const char &Ch, const int &MxLen=-1)
Definition: dt.h:271
Definition: bits.h:119
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: json.h:7
Definition: lx.h:45
void Save(TSOut &SOut) const
Definition: dt.h:995
Definition: lx.h:45
static TUnicode * GetDef()
Definition: unicodestring.h:23
static PJsonVal GetValFromLx(TILx &Lx)
Definition: json.cpp:120
TLxSym Sym
Definition: lx.h:149
Definition: json.h:7
static PJsonVal GetValFromStr(const TStr &JsonStr)
Definition: json.cpp:183
TStr Str
Definition: json.h:14
THash< TStr, PJsonVal > KeyValH
Definition: json.h:16
Definition: fl.h:58
Definition: lx.h:126
void Save(TSOut &SOut) const
Definition: ds.h:954
bool operator!=(const TJsonVal &JsonVal) const
Definition: json.cpp:27
Definition: lx.h:50
Definition: lx.h:126
void Save(TSOut &SOut) const
Definition: json.cpp:7
TBool Bool
Definition: json.h:12
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
TJsonValType
Definition: json.h:6
void Save(TSOut &SOut, const bool &IsSmall=false) const
Definition: dt.h:440
static PSIn New(const TStr &Str)
Definition: dt.h:711
Definition: lx.h:129
static PJsonVal NewStr(const TStr &Str)
Definition: json.h:53
Definition: lx.h:50
Definition: fl.h:128
void AddToObj(const TStr &KeyNm, const PJsonVal &Val)
Definition: json.h:39
TJsonValType JsonValType
Definition: json.h:11
Definition: lx.h:45
TChA Str
Definition: lx.h:150
Definition: json.h:7
bool operator==(const TJsonVal &JsonVal) const
Definition: json.cpp:18
Definition: dt.h:1137
static void AddEscapeChAFromStr(const TStr &Str, TChA &ChA)
Definition: json.cpp:188
int DecodeUtf8(const TIntV &src, TIntV &dest) const
Definition: unicode.h:1787
Definition: dt.h:201
#define EAssert(Cond)
Definition: bd.h:280
double GetObjNum(const TStr &Key) const
Definition: json.h:97
Definition: json.h:7
static TStr GetStrFromVal(const PJsonVal &Val)
Definition: json.cpp:281
TJsonValV ValV
Definition: json.h:15
Definition: dt.h:412
PJsonVal GetObjKey(const TStr &Key) const
Definition: json.cpp:78
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: json.h:7
Definition: json.h:7
static bool IsDef()
Definition: unicodestring.h:21
bool GetObjBool(const TStr &Key) const
Definition: json.h:95
TStr GetObjStr(const TStr &Key) const
Definition: json.h:99
Definition: lx.h:46
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
Definition: lx.h:46
static PJsonVal NewArr()
Definition: json.h:54
TLxSym GetSym(const TFSet &Expect)
Definition: lx.cpp:315
TJsonVal()
Definition: json.h:19
static PJsonVal GetValFromSIn(const PSIn &SIn)
Definition: json.cpp:166
double Flt
Definition: lx.h:151
TFlt Num
Definition: json.h:13
static PJsonVal NewNum(const double &Num)
Definition: json.h:52
void Save(TSOut &SOut) const
Definition: dt.h:1402
bool IsObj() const
Definition: json.h:80
Definition: lx.h:126
static void AddQChAFromStr(const TStr &Str, TChA &ChA)
Definition: json.cpp:242
bool IsObjKey(const TStr &Key) const
Definition: json.h:91