SNAP Library 2.0, Developer Reference  2013-05-13 16:33:57
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   TStr SaveStr();
00026 
00027   bool operator==(const TJsonVal& JsonVal) const;
00028   bool operator!=(const TJsonVal& JsonVal) const;
00029 
00030   // putting value
00031   void PutNull(){JsonValType=jvtNull;}
00032   void PutBool(const bool& _Bool){JsonValType=jvtBool; Bool=_Bool;}
00033   void PutNum(const double& _Num){JsonValType=jvtNum; Num=_Num;}
00034   void PutStr(const TStr& _Str){JsonValType=jvtStr; Str=_Str;}
00035   void PutArr(){JsonValType=jvtArr;}
00036   void AddToArr(const PJsonVal& Val){
00037     EAssert(JsonValType==jvtArr); ValV.Add(Val);}
00038   void PutObj(){JsonValType=jvtObj;}
00039   void AddToObj(const TStr& KeyNm, const PJsonVal& Val){
00040     EAssert(JsonValType==jvtObj); KeyValH.AddDat(KeyNm, Val);}
00041   void AddToObj(const TStr& KeyNm, const int& Val){ AddToObj(KeyNm, NewNum((double)Val)); }
00042   void AddToObj(const TStr& KeyNm, const double& Val){ AddToObj(KeyNm, NewNum(Val)); }
00043   void AddToObj(const TStr& KeyNm, const TStr& Val){ AddToObj(KeyNm, NewStr(Val)); }
00044   void AddToObj(const TStr& KeyNm, const char* Val){ AddToObj(KeyNm, NewStr(Val)); }
00045   void AddToObj(const TStr& KeyNm, const bool& Val){ AddToObj(KeyNm, NewBool(Val)); }
00046   void AddToObj(const TStr& KeyNm, const TJsonValV& ValV){ AddToObj(KeyNm, NewArr(ValV)); }
00047   void AddToObj(const PJsonVal& Val);
00048 
00049   // simplified coreation of basic elements
00050   static PJsonVal NewNull() { PJsonVal Val = TJsonVal::New(); Val->PutNull(); return Val; }
00051   static PJsonVal NewBool(const bool& Bool) { PJsonVal Val = TJsonVal::New(); Val->PutBool(Bool); return Val; }
00052   static PJsonVal NewNum(const double& Num) { PJsonVal Val = TJsonVal::New(); Val->PutNum(Num); return Val; }
00053   static PJsonVal NewStr(const TStr& Str) { PJsonVal Val = TJsonVal::New(); Val->PutStr(Str); return Val; }
00054   static PJsonVal NewArr() { PJsonVal Val = TJsonVal::New(); Val->PutArr(); return Val; }
00055   static PJsonVal NewArr(const TJsonValV& ValV);
00056   static PJsonVal NewArr(const TIntV& IntV);
00057   static PJsonVal NewArr(const TFltV& FltV);
00058   static PJsonVal NewArr(const TStrV& StrV);
00059   static PJsonVal NewArr(const TFltPr& FltPr);
00060   static PJsonVal NewObj() { PJsonVal Val = TJsonVal::New(); Val->PutObj(); return Val; }
00061   static PJsonVal NewObj(const TStr& KeyNm, const PJsonVal& ObjVal) {
00062           PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
00063   static PJsonVal NewObj(const TStr& KeyNm, const int& ObjVal) {
00064           PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
00065   static PJsonVal NewObj(const TStr& KeyNm, const double& ObjVal) {
00066           PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
00067   static PJsonVal NewObj(const TStr& KeyNm, const TStr& ObjVal) {
00068           PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
00069   static PJsonVal NewObj(const TStr& KeyNm, const bool& ObjVal) {
00070           PJsonVal Val = TJsonVal::New(); Val->PutObj(); Val->AddToObj(KeyNm, ObjVal); return Val; }
00071 
00072   // testing value-type
00073   TJsonValType GetJsonValType() const {return JsonValType;}
00074   bool IsDef() const {return JsonValType!=jvtUndef;}
00075   bool IsNull() const {return JsonValType==jvtNull;}
00076   bool IsBool() const {return JsonValType==jvtBool;}
00077   bool IsNum() const {return JsonValType==jvtNum;}
00078   bool IsStr() const {return JsonValType==jvtStr;}
00079   bool IsArr() const {return JsonValType==jvtArr;}
00080   bool IsObj() const {return JsonValType==jvtObj;}
00081 
00082   // getting value
00083   bool GetBool() const {EAssert(IsBool()); return Bool;}
00084   double GetNum() const {EAssert(IsNum()); return Num;}
00085   TStr GetStr() const {EAssert(IsStr()); return Str;}
00086   int GetArrVals() const {EAssert(IsArr()); return ValV.Len();}
00087   PJsonVal GetArrVal(const int& ValN) const {return ValV[ValN];}
00088   int GetObjKeys() const {EAssert(IsObj()); return KeyValH.Len();}
00089   void GetObjKeyVal(const int& KeyValN, TStr& Key, PJsonVal& Val) const {
00090     EAssert(IsObj()); Key=KeyValH.GetKey(KeyValN); Val=KeyValH[KeyValN];}
00091   bool IsObjKey(const TStr& Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);}
00092   bool IsObjKey(const char *Key) const {EAssert(IsObj()); return KeyValH.IsKey(Key);}
00093   PJsonVal GetObjKey(const TStr& Key) const;
00094   PJsonVal GetObjKey(const char *Key) const;
00095   bool GetObjBool(const TStr& Key) const { return GetObjKey(Key)->GetBool(); }
00096   bool GetObjBool(const char *Key) const { return GetObjKey(Key)->GetBool(); }
00097   double GetObjNum(const TStr& Key) const { return GetObjKey(Key)->GetNum(); }
00098   double GetObjNum(const char *Key) const { return GetObjKey(Key)->GetNum(); }
00099   TStr GetObjStr(const TStr& Key) const { return GetObjKey(Key)->GetStr(); }
00100   TStr GetObjStr(const char *Key) const { return GetObjKey(Key)->GetStr(); }
00101   bool GetObjBool(const TStr& Key, const bool& DefBool) const;
00102   bool GetObjBool(const char *Key, const bool& DefBool) const;
00103   double GetObjNum(const TStr& Key, const double& DefNum) const;
00104   double GetObjNum(const char *Key, const double& DefNum) const;
00105   TStr GetObjStr(const TStr& Key, const TStr& DefStr) const;
00106   TStr GetObjStr(const char *Key, const TStr& DefStr) const;
00107 
00108   // (de)serialization
00109   static PJsonVal GetValFromLx(TILx& Lx);
00110   static PJsonVal GetValFromSIn(const PSIn& SIn);
00111   static PJsonVal GetValFromStr(const TStr& JsonStr);
00112   static void AddEscapeChAFromStr(const TStr& Str, TChA& ChA);
00113   static TStr AddEscapeStrFromStr(const TStr& Str) { 
00114           TChA ChA; AddEscapeChAFromStr(Str, ChA); return ChA; }
00115   static void AddQChAFromStr(const TStr& Str, TChA& ChA);
00116   static void GetChAFromVal(const PJsonVal& Val, TChA& ChA);
00117   static TStr GetStrFromVal(const PJsonVal& Val);
00118 };