bool JSONReader::parseAll(const std::string& text, IdType& nextId, std::string& filepath, std::string& checksum, std::vector<std::shared_ptr<MetadataStream::VideoSegment>>& segments, std::vector<std::shared_ptr<MetadataSchema>>& schemas, std::vector<std::shared_ptr<MetadataInternal>>& metadata) { if(text.empty()) { VMF_LOG_ERROR("Empty input JSON string"); return false; } schemas.clear(); metadata.clear(); JSONNode root; try { root = libjson::parse(text); } catch(...) { VMF_LOG_ERROR("Can't get JSON root"); return false; } if(root.size() != 1) { VMF_LOG_ERROR("More than one JSON root"); return false; } JSONNode localRootNode = root[0]; if( localRootNode.name() == TAG_VMF ) { auto nextIdIter = localRootNode.find(ATTR_VMF_NEXTID); if(nextIdIter != localRootNode.end() ) nextId = nextIdIter->as_int(); auto filepathIter = localRootNode.find(ATTR_VMF_FILEPATH); if(filepathIter != localRootNode.end() ) filepath = filepathIter->as_string(); auto checksumIter = localRootNode.find(ATTR_VMF_CHECKSUM); if(checksumIter != localRootNode.end() ) checksum = checksumIter->as_string(); if(!parseVideoSegments(text, segments)) return false; if(!parseSchemas(text, schemas)) return false; if(!parseMetadata(text, schemas, metadata)) return false; } else { VMF_LOG_ERROR("Root JSON element isn't 'vmf'"); return false; } return true; }
int main(){ JSONNode n = read_formatted("input.json"); JSONNode n1 = read_formatted("input1.json"); test *tst = new test(); int status; //n.set_name(abi::__cxa_demangle(typeid(*tst).name(), 0,0,&status )); n.push_back(JSONNode("FF::Node_Subclass", abi::__cxa_demangle(typeid(*tst).name(), 0,0,&status ))); if(n1.type()==JSON_NULL){ std::cout<<"null"<<std::endl; } JSONNode n2 (JSON_NODE); n2.set_name("Child1"); n2.push_back(JSONNode("id",1)); n.set_name("Parrent"); n.push_back(n2); JSONNode::iterator it =n.find_nocase("String Node"); if(it->as_string()=="-1"){std::cout<<it->as_int()<<std::endl;} it->as_int()!=-1 ? (std::cout<< "it is"<<std::endl) : (std::cout<<"Mapper Warning: processor id is -1"<<std::endl); if (n.at("String Node")==""){ std::cout<<"ha ha ha"<<std::endl; } std::cout<< "This is the name: "<<n.name()<<std::endl; n.at("String Node")=""; bool x =true; n.push_back(JSONNode("MyBOOLNODE", x)); n["String Node"]=x; //n.erase(n.find_nocase("String Node")); write_formatted(n1, "out1.json"); write_formatted(n, "out.json"); JSONNode::const_iterator i =n.find_nocase("ArrayOfNumbers"); std::string strLowerCase= "ARRAYOfNUMbers"; std::string myTest= "ff::ff_farm<adaptive_loadbalancer, ff::ff_gatherer>"; std::transform(myTest.begin(), myTest.end(), myTest.begin(), ::tolower); std::size_t found = myTest.find("adaptive_loadbalancer"); if (found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; std::cout<< "here it is: " << myTest<< std::endl; JSONNode n_ar = n.at("ArrayOfNumbers"); std::cout<<"here :"<<n_ar[0].as_int()<< std::endl; // if (0 == strcasecmp("hello", "HELLO")) if(strcasecmp((i->name()).c_str(), strLowerCase.c_str()) == 0) //if(!(n2.empty())) std::cout<<"haha"<<i->size()<<std::endl; std::cout<<i->name()<<std::endl; std::cout<<((i->as_array()).begin()+1)->as_int()<<std::endl; std::cout<<((i->as_array()).at(1)).as_int()<<std::endl; std::cout<<((i->as_array())[1]).as_int()<<std::endl; //std::cout<<i->as_string()<<std::endl; //JSONNode c(JSON_ARRAY); //c=i->as_array(); //JSONNode nk= c.at(0); //JSONNode::const_iterator it = c.begin(); //std::cout <<nk.as_int()<<std::endl; return 0; }
void xmm::Label::from_json(JSONNode root) { try { if (root.type() != JSON_NODE) throw JSONException("Wrong type: was expecting 'JSON_NODE'", root.name()); JSONNode::const_iterator root_it = root.begin(); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->name() != "type") throw JSONException("Wrong name: was expecting 'type'", root_it->name()); if (root_it->type() != JSON_STRING) throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name()); if (root_it->as_string() == "INT") type = INT; else type = SYM; ++root_it; if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->name() != "value") throw JSONException("Wrong name: was expecting 'value'", root_it->name()); if (type == INT) { if (root_it->type() != JSON_NUMBER) throw JSONException("Wrong type: was expecting 'JSON_NUMBER'", root_it->name()); intLabel_ = static_cast<int>(root_it->as_int()); } else { if (root_it->type() != JSON_STRING) throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name()); symLabel_ = root_it->as_string(); } } catch (JSONException &e) { throw JSONException(e, root.name()); } catch (std::exception &e) { throw JSONException(e, root.name()); } }
void parseUser(const JSONNode &it, facebook_user *fbu) { fbu->user_id = it.name(); std::string id = it["id"].as_string(); // same as element's name, but doesn't exists for "page" type std::string alternateName = it["alternateName"].as_string(); // nickname std::string name = it["name"].as_string(); std::string thumbSrc = it["thumbSrc"].as_string(); std::string vanity = it["vanity"].as_string(); // username std::string type = it["type"].as_string(); // "friend", "page", "user" (friend with disabled account or not friend) int gender = it["gender"].as_int(); //const JSONNode &uri = it["uri"); // profile url //const JSONNode &is_friend = it["is_friend"); // e.g. "True" for type="friend", "False" for type="user", doesn't exist for type="page" if (type == "user" && (id.empty() || id == "0")) { // this user has deleted account or is just unavailable for us (e.g., ignore list) -> don't read dummy name and avatar and rather ignore that completely return; } if (type == "friend") fbu->type = CONTACT_FRIEND; else if (type == "user") fbu->type = CONTACT_NONE; else if (type == "page") fbu->type = CONTACT_PAGE; if (!name.empty()) fbu->real_name = utils::text::slashu_to_utf8(name); if (!thumbSrc.empty()) fbu->image_url = utils::text::slashu_to_utf8(thumbSrc); if (!vanity.empty()) fbu->username = utils::text::slashu_to_utf8(vanity); if (!alternateName.empty()) fbu->nick = alternateName; if (gender) { switch (gender) { case 1: // female fbu->gender = 70; break; case 2: // male fbu->gender = 77; break; // case 7: // not available female? // case 11: // page } } }
void SimulationAnimationPatch::loadJSON(const JSONNode data) { string patchName = data.name(); // Load options for raytracer application. (window, ray tracer, filter) JSONNode::const_iterator i = data.begin(); while (i != data.end()) { std::string nodeName = i->name(); if (nodeName == "frameDirectory") { JSONNode fileName = *i; m_file_frameManager = new ArnoldFileFrameManager(fileName.as_string()); } i++; } }
void parseUser(const JSONNode &it, facebook_user *fbu) { fbu->user_id = it.name(); std::string id = it["id"].as_string(); if (id.empty() || id == "0") { // this user has deleted account or is just unavailable for us (e.g., ignore list) -> don't read dummy name and avatar and rather ignore that completely return; } std::string name = it["name"].as_string(); std::string thumbSrc = it["thumbSrc"].as_string(); std::string vanity = it["vanity"].as_string(); // username int gender = it["gender"].as_int(); //const JSONNode &uri = it["uri"); // profile url //const JSONNode &is_friend = it["is_friend"); // e.g. "True" //const JSONNode &type = it["type"); // e.g. "friend" (classic contact) or "user" (disabled/deleted account) if (!name.empty()) fbu->real_name = utils::text::slashu_to_utf8(name); if (!thumbSrc.empty()) fbu->image_url = utils::text::slashu_to_utf8(thumbSrc); if (!vanity.empty()) fbu->username = utils::text::slashu_to_utf8(vanity); if (gender) { switch (gender) { case 1: // female fbu->gender = 70; break; case 2: // male fbu->gender = 77; break; // case 7: not available female? } } }
TEST_F(FieldsTableUnittest, Serilalize) { // Filling some info DataFieldInfo info1, info2; info1.Init( "Field1", "double", 8, 3, 38, 24, 60, 1, 3, 3, 3, 3, 0, 0, 0, 0); info1.AddMetainfo("ADV", true); info2.Init( "Field2", "double", 8, 3, 38, 24, 61, 1, 3, 3, 3, 3, 0, 1, 0, 0); info2.AddMetainfo("Init", 7.4); // Registering fields into table table.RegisterField(info1); table.RegisterField(info2); // Checking table ASSERT_EQ(2, getTableSize()); ASSERT_TRUE(table.HasField("Field1")); ASSERT_TRUE(table.HasField("Field2")); ASSERT_FALSE(table.HasField("Field3")); ASSERT_TRUE(info1 == table.Find("Field1")); ASSERT_TRUE(info2 == table.Find("Field2")); JSONNode node = table.TableToJSON(); ASSERT_EQ(node.name(), "FieldsTable"); ASSERT_EQ(node.type(), JSON_ARRAY); ASSERT_EQ(node.size(), 2); // Check first field info ASSERT_EQ(node[0].name(), "DataFieldInfo"); ASSERT_EQ(node[0]["__name"].as_string(), "Field1"); ASSERT_EQ(node[0]["__id"].as_int(), 0); ASSERT_EQ(node[0]["__isize"].as_int(), 38); ASSERT_EQ(node[0]["__jsize"].as_int(), 24); ASSERT_EQ(node[0]["__ksize"].as_int(), 60); ASSERT_EQ(node[0]["__iminushalosize"].as_int(), 3); ASSERT_EQ(node[0]["__iplushalosize"].as_int(), 3); ASSERT_EQ(node[0]["__jminushalosize"].as_int(), 3); ASSERT_EQ(node[0]["__jplushalosize"].as_int(), 3); ASSERT_EQ(node[0]["__kminushalosize"].as_int(), 0); ASSERT_EQ(node[0]["__kplushalosize"].as_int(), 0); ASSERT_EQ(node[0]["ADV"].as_bool(), true); // Check second field info ASSERT_EQ(node[1].name(), "DataFieldInfo"); ASSERT_EQ(node[1]["__name"].as_string(), "Field2"); ASSERT_EQ(node[1]["__id"].as_int(), 1); ASSERT_EQ(node[1]["__isize"].as_int(), 38); ASSERT_EQ(node[1]["__jsize"].as_int(), 24); ASSERT_EQ(node[1]["__ksize"].as_int(), 61); ASSERT_EQ(node[1]["__iminushalosize"].as_int(), 3); ASSERT_EQ(node[1]["__iplushalosize"].as_int(), 3); ASSERT_EQ(node[1]["__jminushalosize"].as_int(), 3); ASSERT_EQ(node[1]["__jplushalosize"].as_int(), 3); ASSERT_EQ(node[1]["__kminushalosize"].as_int(), 0); ASSERT_EQ(node[1]["__kplushalosize"].as_int(), 1); ASSERT_EQ(node[1]["Init"].as_float(), 7.4); }
bool JSONReader::parseVideoSegments(const std::string& text, std::vector<std::shared_ptr<MetadataStream::VideoSegment> >& segments) { if(text.empty()) { VMF_LOG_ERROR("Empty input JSON string"); return false; } segments.clear(); JSONNode root(JSON_NODE); try { root = libjson::parse(text); } catch(...) { VMF_LOG_ERROR("JSON document has no root element"); return false; } if(root.size() != 1) { VMF_LOG_ERROR("More than one JSON root"); return false; } JSONNode localRootNode = root[0]; if( localRootNode.name() == TAG_VIDEO_SEGMENT ) { try { std::shared_ptr<MetadataStream::VideoSegment> spSegment = parseVideoSegmentFromNode(localRootNode); segments.push_back(spSegment); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } else if( localRootNode.name() == TAG_VIDEO_SEGMENTS_ARRAY ) { for(auto node = localRootNode.begin(); node != localRootNode.end(); node++) try { std::shared_ptr<MetadataStream::VideoSegment> spSegment = parseVideoSegmentFromNode(*node); segments.push_back(spSegment); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } else if( localRootNode.name() == TAG_VMF ) { for(auto rootChildNode = localRootNode.begin(); rootChildNode != localRootNode.end(); rootChildNode++) if(rootChildNode->name() == TAG_VIDEO_SEGMENTS_ARRAY ) for(auto node = rootChildNode->begin(); node != rootChildNode->end(); node++) try { std::shared_ptr<MetadataStream::VideoSegment> spSegment = parseVideoSegmentFromNode(*node); segments.push_back(spSegment); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } return true; }
bool JSONReader::parseMetadata(const std::string& text, const std::vector<std::shared_ptr<MetadataSchema>>& schemas, std::vector<std::shared_ptr<MetadataInternal>>& metadata) { if(text.empty()) { VMF_LOG_ERROR("Empty input JSON string"); return false; } metadata.clear(); JSONNode root; try { root = libjson::parse(text); } catch(...) { VMF_LOG_ERROR("Can't get JSON root"); return false; } if(root.size() != 1) { VMF_LOG_ERROR("More than one JSON root"); return false; } JSONNode localRootNode = root[0]; if( localRootNode.name() == TAG_METADATA ) { try { std::shared_ptr<MetadataInternal> spMetadata = parseMetadataFromNode(localRootNode, schemas); metadata.push_back(spMetadata); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } else if( localRootNode.name() == TAG_METADATA_ARRAY ) { for(auto node = localRootNode.begin(); node != localRootNode.end(); node++) try { std::shared_ptr<MetadataInternal> spMetadata = parseMetadataFromNode(*node, schemas); metadata.push_back(spMetadata); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } else if( localRootNode.name() == TAG_VMF ) { for(auto rootChildNode = localRootNode.begin(); rootChildNode != localRootNode.end(); rootChildNode++) if(rootChildNode->name() == TAG_METADATA_ARRAY ) for(auto node = rootChildNode->begin(); node != rootChildNode->end(); node++) try { std::shared_ptr<MetadataInternal> spMetadata = parseMetadataFromNode(*node, schemas); metadata.push_back(spMetadata); } catch(Exception& e) { VMF_LOG_ERROR("Exception: %s", e.what()); return false; } } return true; }
void xmm::TrainingSet::from_json(JSONNode root) { if (!owns_data) throw std::runtime_error("Cannot read Training Set with Shared memory"); try { if (root.type() != JSON_NODE) throw JSONException("Wrong type: was expecting 'JSON_NODE'", root.name()); JSONNode::const_iterator root_it = root.begin(); // Get Number of modalities root_it = root.find("bimodal"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_BOOL) throw JSONException("Wrong type for node 'bimodal': was expecting 'JSON_BOOL'", root_it->name()); if(bimodal_ != root_it->as_bool()) { if (bimodal_) throw JSONException("Trying to read an unimodal model in a bimodal model.", root.name()); else throw JSONException("Trying to read a bimodal model in an unimodal model.", root.name()); } // Get Dimension root_it = root.find("dimension"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_NUMBER) throw JSONException("Wrong type for node 'dimension': was expecting 'JSON_NUMBER'", root_it->name()); dimension_ = static_cast<unsigned int>(root_it->as_int()); // Get Input Dimension if bimodal_ if (bimodal_){ root_it = root.find("dimension_input"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_NUMBER) throw JSONException("Wrong type for node 'dimension_input': was expecting 'JSON_NUMBER'", root_it->name()); dimension_input_ = static_cast<unsigned int>(root_it->as_int()); } // Get Column Names column_names_.assign(dimension_, ""); root_it = root.find("column_names"); if (root_it != root.end()) { if (root_it->type() != JSON_ARRAY) throw JSONException("Wrong type for node 'column_names': was expecting 'JSON_ARRAY'", root_it->name()); json2vector(*root_it, column_names_, dimension_); } // Get Size: Number of Phrases root_it = root.find("size"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_NUMBER) throw JSONException("Wrong type for node 'size': was expecting 'JSON_NUMBER'", root_it->name()); unsigned int ts_size = static_cast<unsigned int>(root_it->as_int()); // Get Default label root_it = root.find("defaultlabel"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_NODE) throw JSONException("Wrong type for node 'defaultlabel': was expecting 'JSON_NODE'", root_it->name()); defaultLabel_.from_json(*root_it); // Get Phrases phrases.clear(); phraseLabels.clear(); root_it = root.find("phrases"); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->type() != JSON_ARRAY) throw JSONException("Wrong type for node 'phrases': was expecting 'JSON_ARRAY'", root_it->name()); for (unsigned int i=0 ; i<ts_size ; i++) { JSONNode::const_iterator array_it = (*root_it)[i].end(); // Get Index array_it = (*root_it)[i].find("index"); if (array_it == (*root_it)[i].end()) throw JSONException("JSON Node is incomplete", array_it->name()); if (array_it->type() != JSON_NUMBER) throw JSONException("Wrong type for node 'index': was expecting 'JSON_NUMBER'", array_it->name()); unsigned int phraseIndex = static_cast<unsigned int>(array_it->as_int()); // Get Label array_it = (*root_it)[i].find("label"); if (array_it == (*root_it)[i].end()) throw JSONException("JSON Node is incomplete", array_it->name()); if (array_it->type() != JSON_NODE) throw JSONException("Wrong type for node 'label': was expecting 'JSON_NODE'", array_it->name()); phraseLabels[phraseIndex].from_json(*array_it); updateLabelList(); // Get Phrase Content array_it = (*root_it)[i].find("Phrase"); if (array_it == (*root_it)[i].end()) throw JSONException("JSON Node is incomplete", array_it->name()); if (array_it->type() != JSON_NODE) throw JSONException("Wrong type for node 'Phrase': was expecting 'JSON_NODE'", array_it->name()); phrases[phraseIndex] = new Phrase(flags_, dimension_, dimension_input_); phrases[phraseIndex]->from_json(*array_it); } if (ts_size != phrases.size()) throw JSONException("Number of phrases does not match", root_it->name()); has_changed_ = true; } catch (JSONException &e) { throw JSONException(e, root.name()); } catch (std::exception &e) { throw JSONException(e, root.name()); } }
Event::Event(JSONNode node) { _id = node.name(); _callback = [](){ Logger::log(WARN, "Event loaded from JSON node has no callback. Update this in code."); }; }
PresetReaderJson::PresetReaderJson(std::string data) { LOGDEBUG("Preset Data:" << data); JSONNode node = libjson::parse(data); if(node.contains("profile-uuid")){ db::HiveDb db=DatabaseService::getDatabase();//"sqlite3", org::esb::config::Config::get("db.url")); litesql::DataSource<db::Preset>s = litesql::select<db::Preset > (db, db::Preset::Uuid == node["profile-uuid"].as_string()); if(s.count()==1){ data=s.one().data; }else{ throw org::esb::lang::Exception(__FILE__,__LINE__,"profile defined by profile-uuid not found"); } node = libjson::parse(data); } if (node.contains("format") && node["format"].contains("id")) { _preset["id"] = node["format"]["id"].as_string(); if(node.contains("name")){ _preset["name"]=node["name"].as_string(); } }else{ throw org::esb::lang::Exception(__FILE__,__LINE__,"no format attribute found"); } std::string type; type = "video"; if (node.contains(type)) { int c = node[type].size(); for (int a = 0; a < c; a++) { JSONNode n = node[type].at(a); std::string name = n.name(); std::string value = n.as_string(); /*filter out some unwanted attributes*/ if (name == "frame_size"){ int width; int height; int result=sscanf(value.c_str(),"%dx%d", &width, &height); LOGDEBUG("width="<<width); LOGDEBUG("width="<<height); _codecs[type]["width"]=StringUtil::toString(width); _codecs[type]["height"]=StringUtil::toString(height); continue; } //LOGDEBUG("json type : "<<n.type()); if (name == "id") { name = "codec_id"; } if (name == "b" || name == "maxrate" || name == "bt" || name == "bufsize" || name == "minrate") value += "000"; LOGDEBUG("Name=" << name << " val=" << value); _codecs[type][name] = value; //.insert(std::pair<std::string, std::string>(name,value)); } }else{ throw org::esb::lang::Exception(__FILE__,__LINE__,"no video attribute found"); } type = "audio"; if (node.contains(type)) { int c = node[type].size(); for (int a = 0; a < c; a++) { JSONNode n = node[type].at(a); LOGDEBUG("Name=" << n.name() << " val=" << n.as_string()); if (n.name() == "id") { n.set_name("codec_id"); //AVCodec * codec=avcodec_find_encoder_by_name(n.as_string().c_str()); //n=JSONNode("codec_id",codec->id); } _codecs[type][n.name()] = n.as_string(); //.insert(std::pair<std::string, std::string>(n.name(),n.as_string())); } }else{ throw org::esb::lang::Exception(__FILE__,__LINE__,"no audio attribute found"); } }