SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
json.h
Go to the documentation of this file.
1 #include "bd.h"
2 
4 // Json-Value
5 
6 typedef enum {
8 
10 private:
11  TJsonValType JsonValType;
12  TBool Bool;
13  TFlt Num;
14  TStr Str;
15  TJsonValV ValV;
16  THash<TStr, PJsonVal> KeyValH;
18 public:
19  TJsonVal(): JsonValType(jvtUndef){}
20  static PJsonVal New(){
21  return new TJsonVal();}
22  TJsonVal(TSIn& SIn);
23  static PJsonVal Load(TSIn& SIn){return new TJsonVal(SIn);}
24  void Save(TSOut& SOut) const;
25  TStr SaveStr();
26 
27  bool operator==(const TJsonVal& JsonVal) const;
28  bool operator!=(const TJsonVal& JsonVal) const;
29 
30  // putting value
31  void PutNull(){JsonValType=jvtNull;}
32  void PutBool(const bool& BoolVal){JsonValType=jvtBool; Bool=BoolVal;}
33  void PutNum(const double& NumVal){JsonValType=jvtNum; Num=NumVal;}
34  void PutStr(const TStr& StrVal){JsonValType=jvtStr; Str=StrVal;}
35  void PutArr(){JsonValType=jvtArr;}
36  void AddToArr(const PJsonVal& Val){
37  EAssert(JsonValType==jvtArr); ValV.Add(Val);}
38  void PutObj(){JsonValType=jvtObj;}
39  void AddToObj(const TStr& KeyNm, const PJsonVal& Val){
40  EAssert(JsonValType==jvtObj); KeyValH.AddDat(KeyNm, Val);}
41  void AddToObj(const TStr& KeyNm, const int& Val){ AddToObj(KeyNm, NewNum((double)Val)); }
42  void AddToObj(const TStr& KeyNm, const double& Val){ AddToObj(KeyNm, NewNum(Val)); }
43  void AddToObj(const TStr& KeyNm, const TStr& Val){ AddToObj(KeyNm, NewStr(Val)); }
44  void AddToObj(const TStr& KeyNm, const char* Val){ AddToObj(KeyNm, NewStr(Val)); }
45  void AddToObj(const TStr& KeyNm, const bool& Val){ AddToObj(KeyNm, NewBool(Val)); }
46  void AddToObj(const TStr& KeyNm, const TJsonValV& ValV){ AddToObj(KeyNm, NewArr(ValV)); }
47  void AddToObj(const PJsonVal& Val);
48 
49  // simplified coreation of basic elements
50  static PJsonVal NewNull() { PJsonVal Val = TJsonVal::New(); Val->PutNull(); return Val; }
51  static PJsonVal NewBool(const bool& Bool) { PJsonVal Val = TJsonVal::New(); Val->PutBool(Bool); return Val; }
52  static PJsonVal NewNum(const double& Num) { PJsonVal Val = TJsonVal::New(); Val->PutNum(Num); return Val; }
53  static PJsonVal NewStr(const TStr& Str) { PJsonVal Val = TJsonVal::New(); Val->PutStr(Str); return Val; }
54  static PJsonVal NewArr() { PJsonVal Val = TJsonVal::New(); Val->PutArr(); return Val; }
55  static PJsonVal NewArr(const TJsonValV& ValV);
56  static PJsonVal NewArr(const TIntV& IntV);
57  static PJsonVal NewArr(const TFltV& FltV);
58  static PJsonVal NewArr(const TStrV& StrV);
59  static PJsonVal NewArr(const TFltPr& FltPr);
60  static PJsonVal NewObj() { PJsonVal Val = TJsonVal::New(); Val->PutObj(); return Val; }
61  static PJsonVal NewObj(const TStr& KeyNm, const PJsonVal& ObjVal) {
62  PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
63  static PJsonVal NewObj(const TStr& KeyNm, const int& ObjVal) {
64  PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
65  static PJsonVal NewObj(const TStr& KeyNm, const double& ObjVal) {
66  PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
67  static PJsonVal NewObj(const TStr& KeyNm, const TStr& ObjVal) {
68  PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
69  static PJsonVal NewObj(const TStr& KeyNm, const bool& ObjVal) {
70  PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
71 
72  // testing value-type
73  TJsonValType GetJsonValType() const {return JsonValType;}
74  bool IsDef() const {return JsonValType!=jvtUndef;}
75  bool IsNull() const {return JsonValType==jvtNull;}
76  bool IsBool() const {return JsonValType==jvtBool;}
77  bool IsNum() const {return JsonValType==jvtNum;}
78  bool IsStr() const {return JsonValType==jvtStr;}
79  bool IsArr() const {return JsonValType==jvtArr;}
80  bool IsObj() const {return JsonValType==jvtObj;}
81 
82  // getting value
83  bool GetBool() const {EAssert(IsBool()); return Bool;}
84  double GetNum() const {EAssert(IsNum()); return Num;}
85  TStr GetStr() const {EAssert(IsStr()); return Str;}
86  int GetArrVals() const {EAssert(IsArr()); return ValV.Len();}
87  PJsonVal GetArrVal(const int& ValN) const {return ValV[ValN];}
88  int GetObjKeys() const {EAssert(IsObj()); return KeyValH.Len();}
89  void GetObjKeyVal(const int& KeyValN, TStr& Key, PJsonVal& Val) const {
90  EAssert(IsObj()); Key=KeyValH.GetKey(KeyValN); Val=KeyValH[KeyValN];}
91  bool IsObjKey(const TStr& Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);}
92  bool IsObjKey(const char *Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);}
93  PJsonVal GetObjKey(const TStr& Key) const;
94  PJsonVal GetObjKey(const char *Key) const;
95  bool GetObjBool(const TStr& Key) const { return GetObjKey(Key)->GetBool(); }
96  bool GetObjBool(const char *Key) const { return GetObjKey(Key)->GetBool(); }
97  double GetObjNum(const TStr& Key) const { return GetObjKey(Key)->GetNum(); }
98  double GetObjNum(const char *Key) const { return GetObjKey(Key)->GetNum(); }
99  TStr GetObjStr(const TStr& Key) const { return GetObjKey(Key)->GetStr(); }
100  TStr GetObjStr(const char *Key) const { return GetObjKey(Key)->GetStr(); }
101  bool GetObjBool(const TStr& Key, const bool& DefBool) const;
102  bool GetObjBool(const char *Key, const bool& DefBool) const;
103  double GetObjNum(const TStr& Key, const double& DefNum) const;
104  double GetObjNum(const char *Key, const double& DefNum) const;
105  TStr GetObjStr(const TStr& Key, const TStr& DefStr) const;
106  TStr GetObjStr(const char *Key, const TStr& DefStr) const;
107 
108  // (de)serialization
109  static PJsonVal GetValFromLx(TILx& Lx);
110  static PJsonVal GetValFromSIn(const PSIn& SIn);
111  static PJsonVal GetValFromStr(const TStr& JsonStr);
112  static void AddEscapeChAFromStr(const TStr& Str, TChA& ChA);
113  static TStr AddEscapeStrFromStr(const TStr& Str) {
114  TChA ChA; AddEscapeChAFromStr(Str, ChA); return ChA; }
115  static void AddQChAFromStr(const TStr& Str, TChA& ChA);
116  static void GetChAFromVal(const PJsonVal& Val, TChA& ChA);
117  static TStr GetStrFromVal(const PJsonVal& Val);
118 };
bool IsDef() const
Definition: json.h:74
Definition: json.h:9
static PJsonVal NewObj(const TStr &KeyNm, const int &ObjVal)
Definition: json.h:63
void AddToObj(const TStr &KeyNm, const double &Val)
Definition: json.h:42
bool IsNum() const
Definition: json.h:77
static PJsonVal New()
Definition: json.h:20
double GetNum() const
Definition: json.h:84
void GetObjKeyVal(const int &KeyValN, TStr &Key, PJsonVal &Val) const
Definition: json.h:89
static PJsonVal Load(TSIn &SIn)
Definition: json.h:23
void PutObj()
Definition: json.h:38
static PJsonVal NewBool(const bool &Bool)
Definition: json.h:51
void AddToObj(const TStr &KeyNm, const char *Val)
Definition: json.h:44
void PutStr(const TStr &StrVal)
Definition: json.h:34
static PJsonVal NewNull()
Definition: json.h:50
Definition: json.h:7
static PJsonVal NewObj(const TStr &KeyNm, const PJsonVal &ObjVal)
Definition: json.h:61
TJsonValType GetJsonValType() const
Definition: json.h:73
#define UndefCopyAssign(TNm)
Definition: bd.h:209
bool IsStr() const
Definition: json.h:78
Definition: json.h:7
bool GetBool() const
Definition: json.h:83
Definition: dt.h:1386
Definition: fl.h:58
void PutNum(const double &NumVal)
Definition: json.h:33
bool operator!=(const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:414
TStr GetStr() const
Definition: json.h:85
int GetArrVals() const
Definition: json.h:86
static PJsonVal NewObj(const TStr &KeyNm, const TStr &ObjVal)
Definition: json.h:67
#define ClassTPV(TNm, PNm, TNmV)
Definition: bd.h:162
void AddToObj(const TStr &KeyNm, const TJsonValV &ValV)
Definition: json.h:46
static PJsonVal NewObj()
Definition: json.h:60
int GetObjKeys() const
Definition: json.h:88
void AddToObj(const TStr &KeyNm, const TStr &Val)
Definition: json.h:43
bool IsBool() const
Definition: json.h:76
TJsonValType
Definition: json.h:6
Definition: lx.h:129
static PJsonVal NewStr(const TStr &Str)
Definition: json.h:53
void AddToObj(const TStr &KeyNm, const bool &Val)
Definition: json.h:45
bool IsArr() const
Definition: json.h:79
Definition: fl.h:128
void AddToObj(const TStr &KeyNm, const PJsonVal &Val)
Definition: json.h:39
double GetObjNum(const char *Key) const
Definition: json.h:98
Definition: json.h:7
TStr GetStr() const
Definition: dt.h:681
bool GetObjBool(const char *Key) const
Definition: json.h:96
static PJsonVal NewObj(const TStr &KeyNm, const double &ObjVal)
Definition: json.h:65
void AddToArr(const PJsonVal &Val)
Definition: json.h:36
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
void PutBool(const bool &BoolVal)
Definition: json.h:32
Definition: ds.h:32
Definition: dt.h:412
static PJsonVal NewObj(const TStr &KeyNm, const bool &ObjVal)
Definition: json.h:69
Definition: json.h:7
Definition: json.h:7
Definition: hash.h:97
bool GetObjBool(const TStr &Key) const
Definition: json.h:95
TStr GetObjStr(const TStr &Key) const
Definition: json.h:99
bool IsNull() const
Definition: json.h:75
Definition: json.h:7
Definition: bd.h:196
void PutArr()
Definition: json.h:35
TStr GetObjStr(const char *Key) const
Definition: json.h:100
static TStr AddEscapeStrFromStr(const TStr &Str)
Definition: json.h:113
void PutNull()
Definition: json.h:31
static PJsonVal NewArr()
Definition: json.h:54
Definition: dt.h:974
PJsonVal GetArrVal(const int &ValN) const
Definition: json.h:87
bool IsObjKey(const char *Key) const
Definition: json.h:92
void AddToObj(const TStr &KeyNm, const int &Val)
Definition: json.h:41
static PJsonVal NewNum(const double &Num)
Definition: json.h:52
bool IsObj() const
Definition: json.h:80
bool IsObjKey(const TStr &Key) const
Definition: json.h:91