4 #include <Poco/JSON/Object.h>
5 #include <Poco/JSON/Array.h>
6 #include <Poco/JSON/JSON.h>
7 #include <Poco/JSON/Stringifier.h>
8 #include <Poco/JSON/Parser.h>
9 #include <Poco/JSON/JSONException.h>
29 Poco::JSON::Object::Ptr
pObj =
new Poco::JSON::Object();
31 throw Poco::Exception(
"Object instance was not created");
35 pObj->set((*iter).first, (*iter).second);
38 std::stringstream ostr;
39 Poco::JSON::Stringifier::stringify(pObj, ostr);
42 catch(Poco::Exception& e)
48 catch(std::exception& e)
62 Poco::JSON::Parser parser;
63 Poco::Dynamic::Var res = parser.parse(json);
65 Poco::JSON::Object::Ptr
pObj = res.extract<Poco::JSON::Object::Ptr>();
69 std::vector<std::string> names;
70 pObj->getNames(names);
72 for (
size_t i=0;i<names.size();++i)
74 Poco::Dynamic::Var tmp = pObj->get(names[i]);
79 setItem(names[i], tmp.convert<std::string>());
81 else if(tmp.isNumeric())
87 setItem(names[i], static_cast<long int>(tmp.convert<Poco::Int64>()));
91 setItem(names[i], static_cast<unsigned long int>(tmp.convert<Poco::UInt64>()));
96 setItem(names[i], tmp.convert<
double>());
103 catch(Poco::JSON::JSONException& e)
109 catch(Poco::Exception& e)
115 catch(std::exception& e)
130 Poco::File jsonFile(fileName);
131 if (jsonFile.exists())
133 if (!jsonFile.isFile())
134 throw std::runtime_error(fileName+
" is not file");
136 if (!jsonFile.canRead())
137 throw std::runtime_error(fileName+
" is cannot be read");
139 std::ifstream ifs(fileName.c_str());
141 throw std::runtime_error(fileName+
" is not opened");
148 catch(std::exception& e)
159 std::ofstream ofs(fileName, std::fstream::trunc);
162 return Poco::File(fileName).exists();
168 is.seekg(0, std::ios::end);
169 json.resize(is.tellg());
170 is.seekg(0, std::ios::beg);
171 is.read(const_cast<char*>(json.c_str()), json.size());