SNAP Library, Developer Reference
2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
|
00001 00002 // Json-Value 00003 00004 typedef enum { 00005 jvtUndef, jvtNull, jvtBool, jvtNum, jvtStr, jvtArr, jvtObj} TJsonValType; 00006 00007 ClassTPV(TJsonVal, PJsonVal, TJsonValV)//{ 00008 private: 00009 TJsonValType JsonValType; 00010 TBool Bool; 00011 TFlt Num; 00012 TStr Str; 00013 TJsonValV ValV; 00014 THash<TStr, PJsonVal> KeyValH; 00015 UndefCopyAssign(TJsonVal); 00016 public: 00017 TJsonVal(): JsonValType(jvtUndef){} 00018 static PJsonVal New(){ 00019 return new TJsonVal();} 00020 TJsonVal(TSIn& SIn){} 00021 static PJsonVal Load(TSIn& SIn){return new TJsonVal(SIn);} 00022 void Save(TSOut& SOut) const {} 00023 00024 // putting value 00025 void PutNull(){JsonValType=jvtNull;} 00026 void PutBool(const bool& _Bool){JsonValType=jvtBool; Bool=_Bool;} 00027 void PutNum(const double& _Num){JsonValType=jvtNum; Num=_Num;} 00028 void PutStr(const TStr& _Str){JsonValType=jvtStr; Str=_Str;} 00029 void PutArr(){JsonValType=jvtArr;} 00030 void AddToArr(const PJsonVal& Val){ 00031 EAssert(JsonValType==jvtArr); ValV.Add(Val);} 00032 void PutObj(){JsonValType=jvtObj;} 00033 void AddToObj(const TStr& KeyNm, const PJsonVal& Val){ 00034 EAssert(JsonValType==jvtObj); KeyValH.AddDat(KeyNm, Val);} 00035 void AddToObj(const TStr& KeyNm, const int& Val){ AddToObj(KeyNm, NewNum((double)Val)); } 00036 void AddToObj(const TStr& KeyNm, const double& Val){ AddToObj(KeyNm, NewNum(Val)); } 00037 void AddToObj(const TStr& KeyNm, const TStr& Val){ AddToObj(KeyNm, NewStr(Val)); } 00038 void AddToObj(const TStr& KeyNm, const char* Val){ AddToObj(KeyNm, NewStr(Val)); } 00039 void AddToObj(const TStr& KeyNm, const bool& Val){ AddToObj(KeyNm, NewBool(Val)); } 00040 void AddToObj(const TStr& KeyNm, const TJsonValV& ValV){ AddToObj(KeyNm, NewArr(ValV)); } 00041 00042 // simplified coreation of basic elements 00043 static PJsonVal NewNull() { PJsonVal Val = TJsonVal::New(); Val->PutNull(); return Val; } 00044 static PJsonVal NewBool(const bool& Bool) { PJsonVal Val = TJsonVal::New(); Val->PutBool(Bool); return Val; } 00045 static PJsonVal NewNum(const double& Num) { PJsonVal Val = TJsonVal::New(); Val->PutNum(Num); return Val; } 00046 static PJsonVal NewStr(const TStr& Str) { PJsonVal Val = TJsonVal::New(); Val->PutStr(Str); return Val; } 00047 static PJsonVal NewArr() { PJsonVal Val = TJsonVal::New(); Val->PutArr(); return Val; } 00048 static PJsonVal NewArr(const TJsonValV& ValV); 00049 static PJsonVal NewArr(const TStrV& StrV); 00050 static PJsonVal NewObj() { PJsonVal Val = TJsonVal::New(); Val->PutObj(); return Val; } 00051 static PJsonVal NewObj(const TStr& KeyNm, const PJsonVal& ObjVal) { 00052 PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; } 00053 static PJsonVal NewObj(const TStr& KeyNm, const int& ObjVal) { 00054 PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; } 00055 static PJsonVal NewObj(const TStr& KeyNm, const double& ObjVal) { 00056 PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; } 00057 static PJsonVal NewObj(const TStr& KeyNm, const TStr& ObjVal) { 00058 PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; } 00059 static PJsonVal NewObj(const TStr& KeyNm, const bool& ObjVal) { 00060 PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; } 00061 00062 // testing value-type 00063 TJsonValType GetJsonValType() const {return JsonValType;} 00064 bool IsNull() const {return JsonValType==jvtNull;} 00065 bool IsBool() const {return JsonValType==jvtBool;} 00066 bool IsNum() const {return JsonValType==jvtNum;} 00067 bool IsStr() const {return JsonValType==jvtStr;} 00068 bool IsArr() const {return JsonValType==jvtArr;} 00069 bool IsObj() const {return JsonValType==jvtObj;} 00070 00071 // getting value 00072 bool GetBool() const {EAssert(IsBool()); return Bool;} 00073 double GetNum() const {EAssert(IsNum()); return Num;} 00074 TStr GetStr() const {EAssert(IsStr()); return Str;} 00075 int GetArrVals() const {EAssert(IsArr()); return ValV.Len();} 00076 PJsonVal GetArrVal(const int& ValN) const {return ValV[ValN];} 00077 int GetObjKeys() const {EAssert(IsObj()); return KeyValH.Len();} 00078 void GetObjKeyVal(const int& KeyValN, TStr& Key, PJsonVal& Val) const { 00079 EAssert(IsObj()); Key=KeyValH.GetKey(KeyValN); Val=KeyValH[KeyValN];} 00080 bool IsObjKey(const TStr& Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);} 00081 bool IsObjKey(const char *Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);} 00082 PJsonVal GetObjKey(const TStr& Key) const; 00083 PJsonVal GetObjKey(const char *Key) const; 00084 bool GetObjBool(const TStr& Key) const { return GetObjKey(Key)->GetBool(); } 00085 bool GetObjBool(const char *Key) const { return GetObjKey(Key)->GetBool(); } 00086 double GetObjNum(const TStr& Key) const { return GetObjKey(Key)->GetNum(); } 00087 double GetObjNum(const char *Key) const { return GetObjKey(Key)->GetNum(); } 00088 TStr GetObjStr(const TStr& Key) const { return GetObjKey(Key)->GetStr(); } 00089 TStr GetObjStr(const char *Key) const { return GetObjKey(Key)->GetStr(); } 00090 bool GetObjBool(const TStr& Key, const bool& DefBool) const; 00091 bool GetObjBool(const char *Key, const bool& DefBool) const; 00092 double GetObjNum(const TStr& Key, const double& DefNum) const; 00093 double GetObjNum(const char *Key, const double& DefNum) const; 00094 TStr GetObjStr(const TStr& Key, const TStr& DefStr) const; 00095 TStr GetObjStr(const char *Key, const TStr& DefStr) const; 00096 00097 // (de)serialization 00098 static PJsonVal GetValFromLx(TILx& Lx); 00099 static PJsonVal GetValFromStr(const TStr& JsonStr); 00100 static void AddEscapeChAFromStr(const TStr& Str, TChA& ChA); 00101 static TStr AddEscapeStrFromStr(const TStr& Str) { 00102 TChA ChA; AddEscapeChAFromStr(Str, ChA); return ChA; } 00103 static void AddQChAFromStr(const TStr& Str, TChA& ChA); 00104 static void GetChAFromVal(const PJsonVal& Val, TChA& ChA); 00105 static TStr GetStrFromVal(const PJsonVal& Val); 00106 };