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