void TGraphCascade::GenGraph(const PJsonVal& Dag) { // Dag is an object, whose props are node names, whose values are arrays of parent node names Graph.Clr(); int Keys = Dag->GetObjKeys(); for (int KeyN = 0; KeyN < Keys; KeyN++) { TStr Key = Dag->GetObjKey(KeyN); if (!NodeNmIdH.IsKey(Key)) { NodeNmIdH.AddDat(Key, NodeNmIdH.Len()); NodeIdNmH.AddDat(NodeIdNmH.Len(), Key); } int DstId = NodeNmIdH.GetDat(Key); if (!Graph.IsNode(DstId)) { Graph.AddNode(DstId); } PJsonVal Val = Dag->GetObjKey(Key); int Parents = Val->GetArrVals(); for (int NodeN = 0; NodeN < Parents; NodeN++) { TStr NodeNm = Val->GetArrVal(NodeN)->GetStr(); if (!NodeNmIdH.IsKey(NodeNm)) { NodeNmIdH.AddDat(NodeNm, NodeNmIdH.Len()); NodeIdNmH.AddDat(NodeIdNmH.Len(), NodeNm); } int SrcId = NodeNmIdH.GetDat(NodeNm); if (!Graph.IsNode(SrcId)) { Graph.AddNode(SrcId); } if (!Graph.IsEdge(SrcId, DstId)) { Graph.AddEdge(SrcId, DstId); } } } }
// extend/update the object with values from Val // this and Val should be an Object and not an array or something else void TJsonVal::MergeObj(const PJsonVal& Val) { EAssert(Val->IsObj() && IsObj()); for (int N = 0; N < Val->GetObjKeys(); N++) { const TStr Key = Val->GetObjKey(N); AddToObj(Key, Val->GetObjKey(Key)); } }
void TFolderBackup::ParseSettings(const PJsonVal& SettingsJson) { // name of the file that holds all backups DestinationDirNm = SettingsJson->GetObjStr("destination"); // load profiles for which we wish to make backups PJsonVal ProfilesJson = SettingsJson->GetObjKey("profiles"); if (!ProfilesJson->IsObj()) { TNotify::StdNotify->OnStatus("'profiles' object is not an object"); return; } for (int N = 0; N < ProfilesJson->GetObjKeys(); N++) { const TStr ProfileName = ProfilesJson->GetObjKey(N); ProfileH.AddDat(ProfileName, TBackupProfile(ProfilesJson->GetObjKey(ProfileName), DestinationDirNm, ProfileName)); } }
v8::Local<v8::Value> TNodeJsUtil::ParseJson(v8::Isolate* Isolate, const PJsonVal& JsonVal) { v8::EscapableHandleScope HandleScope(Isolate); if (!JsonVal->IsDef()) { return v8::Undefined(Isolate); } else if (JsonVal->IsBool()) { return v8::Boolean::New(Isolate, JsonVal->GetBool()); } else if (JsonVal->IsNull()) { return v8::Null(Isolate); } else if (JsonVal->IsNum()) { return HandleScope.Escape(v8::Number::New(Isolate, JsonVal->GetNum())); } else if (JsonVal->IsStr()) { return HandleScope.Escape(v8::String::NewFromUtf8(Isolate, JsonVal->GetStr().CStr())); } else if (JsonVal->IsArr()) { const uint Len = JsonVal->GetArrVals(); v8::Local<v8::Array> ResArr = v8::Array::New(Isolate, Len); for (uint i = 0; i < Len; i++) { ResArr->Set(i, ParseJson(Isolate, JsonVal->GetArrVal(i))); } return HandleScope.Escape(ResArr); } else if (JsonVal->IsObj()) { v8::Local<v8::Object> ResObj = v8::Object::New(Isolate); const int NKeys = JsonVal->GetObjKeys(); for (int i = 0; i < NKeys; i++) { TStr Key; PJsonVal Val; JsonVal->GetObjKeyVal(i, Key, Val); ResObj->Set(v8::String::NewFromUtf8(Isolate, Key.CStr()), ParseJson(Isolate, Val)); } return HandleScope.Escape(ResObj); } else { throw TExcept::New("Invalid JSON!", "TNodeJsUtil::ParseJson"); } }
void TGraphCascade::ProcessModels(const PJsonVal& NodeModels) { int Keys = NodeModels->GetObjKeys(); for (int KeyN = 0; KeyN < Keys; KeyN++) { TStr Key = NodeModels->GetObjKey(KeyN); if (!NodeNmIdH.IsKey(Key)) { // skip, we will not need this model continue; } PJsonVal Val = NodeModels->GetObjKey(Key); TFltV PMF; Val->GetArrNumV(PMF); int NodeId = NodeNmIdH.GetDat(Key); TFltV NodeCDF(PMF.Len()); int Len = NodeCDF.Len(); if (Len > 0) { NodeCDF[0] = PMF[0]; for (int ElN = 1; ElN < Len; ElN++) { NodeCDF[ElN] = NodeCDF[ElN - 1] + PMF[ElN]; } } CDF.AddDat(NodeId, NodeCDF); } }