File indexing completed on 2025-01-18 10:17:39
0001
0002
0003
0004
0005 #include <sstream>
0006 #include <vector>
0007 #include <string>
0008 #include <map>
0009
0010 #include <JANA/JObject.h>
0011
0012
0013
0014 template <typename T>
0015 void JJSON_Add(std::stringstream &ss, const T &t, int &indent_level){
0016 ss << std::string(indent_level*2, ' ') << t;
0017 }
0018
0019
0020
0021 void JJSON_Add(std::stringstream &ss, const std::string &s, int &indent_level){
0022
0023 bool has_whitespace = s.find_first_not_of("\t\n ") != string::npos;
0024 bool is_json = s.find_first_not_of("{[\t\n ") != 0;
0025 std::string quote = ((has_whitespace && !is_json) || s.empty()) ? "\"":"";
0026 ss << std::string(indent_level*2, ' ') << quote << s << quote;
0027 }
0028
0029
0030 template <typename T>
0031 void JJSON_Add(std::stringstream &ss, const std::vector<T> &V, int &indent_level){
0032
0033 ss << std::string(indent_level*2, ' ') << "[\n";
0034 indent_level++;
0035 auto items_left = V.size();
0036 for( auto v : V){
0037 JJSON_Add(ss, v, indent_level);
0038 if(--items_left != 0) ss << ",";
0039 ss << "\n";
0040 }
0041 indent_level--;
0042 ss << std::string(indent_level*2, ' ') << "]";
0043 }
0044
0045
0046 template <typename T>
0047 void JJSON_Add(std::stringstream &ss, const std::map<std::string, T> &M, int &indent_level){
0048
0049 ss << std::string(indent_level*2, ' ') << "{\n";
0050 indent_level++;
0051 auto items_left = M.size();
0052 for( auto p : M){
0053 ss << std::string(indent_level*2, ' ') << "\"" << p.first << "\":";
0054 JJSON_Add(ss, p.second, indent_level);
0055 if(--items_left != 0) ss << ",";
0056 ss << "\n";
0057 }
0058 indent_level--;
0059 ss << std::string(indent_level*2, ' ') << "}";
0060 }
0061
0062 template <typename T>
0063 void JJSON_Add(std::stringstream &ss, const std::unordered_map<std::string, T> &M, int &indent_level){
0064
0065 ss << std::string(indent_level*2, ' ') << "{\n";
0066 indent_level++;
0067 auto items_left = M.size();
0068 for( auto p : M){
0069 ss << std::string(indent_level*2, ' ') << "\"" << p.first << "\":";
0070 JJSON_Add(ss, p.second, indent_level);
0071 if(--items_left != 0) ss << ",";
0072 ss << "\n";
0073 }
0074 indent_level--;
0075 ss << std::string(indent_level*2, ' ') << "}";
0076 }
0077
0078
0079 void JJSON_Add(std::stringstream &ss, const JObjectSummary &jobj_summary, int &indent_level) {
0080 auto fields = jobj_summary.get_fields();
0081 JJSON_Add(ss, fields, indent_level);
0082 }
0083
0084
0085 void JJSON_Add(std::stringstream &ss, const JObjectMember &jobj_member, int &indent_level) {
0086 std::unordered_map<std::string, std::string> vals;
0087 vals["name"] = jobj_member.name;
0088 vals["type"] = jobj_member.type;
0089 vals["value"] = jobj_member.value;
0090 JJSON_Add(ss, vals, indent_level);
0091 }
0092
0093 template<typename T>
0094 std::string JJSON_Create(const T &t, int indent_level = 0){
0095 std::stringstream ss;
0096 JJSON_Add(ss, t, indent_level);
0097 return ss.str();
0098 }