6 #include "boost/algorithm/string.hpp"
7 #include "boost/lexical_cast.hpp"
12 Json::Reader json_reader;
15 if (!input.is_open()) {
16 throw std::runtime_error(
"[ExtractJsonFromFile] Unable to open file " + file);
18 json_reader.parse(input, js);
24 Json::Reader reader(Json::Features::all());
25 reader.parse(str, js);
30 std::vector<std::string> split_str;
31 std::vector<std::string> split_lastarg;
32 boost::split(split_str,str,boost::is_any_of(
":"));
33 boost::split(split_lastarg,split_str.at(split_str.size()-1),boost::is_any_of(
"^"));
34 std::string jsonstr =
"{\"";
35 if(split_lastarg.size()==1){
36 for(
unsigned i = 0; i<split_str.size()-1;i++){
37 if(i==split_str.size()-2){
38 jsonstr = jsonstr+split_str.at(i)+
"\":";
39 }
else jsonstr = jsonstr+split_str.at(i)+
"\":{\"";
43 boost::lexical_cast<
double>(split_str.at(split_str.size()-1));
45 catch(boost::bad_lexical_cast&
e){
49 if(strcmp((split_str.at(split_str.size()-1)).c_str(),
"false")==0||strcmp((split_str.at(split_str.size()-1)).c_str(),
"true")==0){
52 if(is_bool || is_num){
53 jsonstr = jsonstr+split_str.at(split_str.size()-1)+std::string(split_str.size()-1,
'}');
55 if(strcmp(split_str.at(split_str.size()-1).c_str(),
"")==0){
56 jsonstr = jsonstr+
"[]"+std::string(split_str.size()-1,
'}');
58 jsonstr = jsonstr+
"\""+split_str.at(split_str.size()-1)+
"\""+std::string(split_str.size()-1,
'}');
64 for(
unsigned i = 0; i<split_str.size()-1;i++){
65 if(i==split_str.size()-2){
66 jsonstr = jsonstr+split_str.at(i)+
"\":[";
67 }
else jsonstr = jsonstr+split_str.at(i)+
"\":{\"";
70 bool are_all_num =
true;
71 for(
unsigned k = 0; k<split_lastarg.size(); k++){
72 if(strcmp((split_lastarg.at(k)).c_str(),
"")!=0){
74 boost::lexical_cast<
double>(split_lastarg.at(k));
76 catch(boost::bad_lexical_cast&
e){
79 are_all_num = are_all_num&&is_num;
83 bool are_all_bool =
true;
84 for(
unsigned k = 0; k<split_lastarg.size();k++){
85 if(strcmp((split_lastarg.at(k)).c_str(),
"")!=0){
86 if(strcmp((split_lastarg.at(k)).c_str(),
"false")==0||strcmp((split_lastarg.at(k)).c_str(),
"true")==0){
89 are_all_bool = are_all_bool&&is_bool;
92 if(are_all_bool || are_all_num){
93 for(
unsigned k = 0; k<split_lastarg.size()-1;k++){
94 if(strcmp(
"",split_lastarg.at(k).c_str())!=0){
95 jsonstr = jsonstr+split_lastarg.at(k)+
",";
98 jsonstr=jsonstr+split_lastarg.at(split_lastarg.size()-1)+
"]"+std::string(split_str.size()-1,
'}');
100 for(
unsigned k = 0; k<split_lastarg.size()-1;k++){
101 if(strcmp(
"",split_lastarg.at(k).c_str())!=0){
102 jsonstr = jsonstr+
"\""+split_lastarg.at(k)+
"\",";
105 jsonstr=jsonstr+
"\""+split_lastarg.at(split_lastarg.size()-1)+
"\"]"+std::string(split_str.size()-1,
'}');
110 Json::Reader reader(Json::Features::all());
111 reader.parse(jsonstr, js);
118 if (!a.isObject() || !b.isObject())
return;
119 for (
auto const& key : b.getMemberNames()) {
120 if (a[key].isObject()) {
129 Json::Value res(Json::objectValue);
131 for (
int i = 1; i < argc; ++i) {
132 if (boost::algorithm::iends_with(argv[i],
".json")) {
144 Json::Value
MergedJson(std::vector<std::string>
const& vec) {
145 Json::Value res(Json::objectValue);
146 for (
auto const& str : vec) {
147 if (boost::algorithm::iends_with(str,
".json")) {
Json::Value MergedJson(int argc, char *argv[])
Create a single merged Json::Value from a mixture of json files and json-formatted strings...
Json::Value ExtractJsonFromFlatString(std::string const &str)
Extracts a Json::Value from the given input string.
void UpdateJson(Json::Value &a, Json::Value const &b)
Updates the values in one json from the values in another.
Json::Value ExtractJsonFromString(std::string const &str)
Extracts a Json::Value from the given input string.
Json::Value ExtractJsonFromFile(std::string const &file)
Extracts a Json::Value from the specified input file.