00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "marojson.h"
00022 #include <sstream>
00023 #include <iostream>
00024
00025
00026 using namespace Marsyas;
00027
00028 marojson::marojson() : marostring()
00029 {
00030 this->prettyPrint = true;
00031 }
00032
00033 marojson::~marojson()
00034 {
00035 clear();
00036 }
00037
00038 void
00039 marojson::begin_marsystem(bool isComposite, std::string type, std::string name)
00040 {
00041 result_ << "'" << name << "':{";
00042
00043 result_ << "'isComposite':";
00044 if(isComposite)
00045 result_ << "true";
00046 else
00047 result_ << "false";
00048
00049 result_ << ",";
00050
00051 result_ << "'type':'" << type << "',";
00052 }
00053
00054 void
00055 marojson::end_marsystem(bool isComposite, std::string type, std::string name)
00056 {
00057 result_ << "},";
00058 }
00059
00060 void
00061 marojson::begin_controls(int num_controls)
00062 {
00063 if (num_controls > 0)
00064 result_ << "'controls':[";
00065 }
00066
00067 void
00068 marojson::begin_control(std::string type, std::string name, std::string value, bool has_state)
00069 {
00070 result_ << "{";
00071 result_ << "'type':'" << type << "',";
00072 result_ << "'name':'" << name << "',";
00073
00074 result_ << "'value':";
00075
00076 if (type == "mrs_realvec" || type == "mrs_string")
00077 result_ << "'";
00078
00079 if (value=="")
00080 result_ << "MARSYAS_EMPTYSTRING";
00081 else
00082 result_ << value;
00083
00084 if (type == "mrs_realvec" || type == "mrs_string")
00085 result_ << "'";
00086
00087 result_ << "',";
00088 }
00089 void
00090 marojson::begin_control_links_in(int num_links)
00091 {
00092 if (num_links > 0)
00093 result_ << "'linksTo':[";
00094 }
00095 void
00096 marojson::put_control_link_in(std::string abspath, std::string type, std::string name)
00097 {
00098 result_ << "{";
00099 result_ << "'abspath':'" << abspath << "',";
00100 result_ << "'type':'" << type << "',";
00101 result_ << "'name':'" << name << "',";
00102 result_ << "},";
00103 }
00104
00105 void
00106 marojson::end_control_links_in(int num_links)
00107 {
00108 if (num_links > 0)
00109 result_ << "],";
00110 }
00111
00112 void
00113 marojson::begin_control_links_out(int num_links)
00114 {
00115 if (num_links > 0)
00116 result_ << "'linksFrom':[";
00117 }
00118 void
00119 marojson::put_control_link_out(std::string abspath, std::string type, std::string name)
00120 {
00121 result_ << "{";
00122 result_ << "'abspath':'" << abspath << "',";
00123 result_ << "'type':'" << type << "',";
00124 result_ << "'name':'" << name << "',";
00125 result_ << "},";
00126 }
00127 void
00128 marojson::end_control_links_out(int num_links)
00129 {
00130 if (num_links > 0)
00131 result_ << "],";
00132 }
00133
00134 void
00135 marojson::end_control(std::string type, std::string name, std::string value, bool has_state)
00136 {
00137 result_ << "},";
00138 }
00139 void
00140 marojson::end_controls(int num_links)
00141 {
00142 if (num_links > 0)
00143 result_ << "],";
00144 }
00145
00146 void
00147 marojson::begin_children(int num_children)
00148 {
00149 if (num_children > 0)
00150 result_ << "'components':[";
00151 }
00152
00153 void
00154 marojson::end_children(int num_children)
00155 {
00156 if (num_children > 0)
00157 result_ << "],";
00158 }
00159
00160 std::string
00161 marojson::str()
00162 {
00163 std::string json = result_.str();
00164 replace_all(json, ",}", "}");
00165 replace_all(json, ",]", "]");
00166
00167 if (prettyPrint)
00168 {
00169 replace_all(json, ":", ": ");
00170
00171
00172
00173 replace_all(json, "{", "{\r\n");
00174 replace_all(json, "}", "\r\n}");
00175 replace_all(json, "[", "[\r\n");
00176 replace_all(json, "]", "\r\n]");
00177 replace_all(json, ",", ",\r\n");
00178 }
00179
00180 return "{" + json + "}";
00181 }
00182
00183 std::string&
00184 marojson::replace_between(std::string& str,
00185 const std::string &oldsubstr, const std::string &newsubstr,
00186 std::string::size_type from_pos, std::string::size_type to_pos)
00187 {
00188 std::string::size_type position;
00189
00190 position = str.find(oldsubstr, from_pos);
00191 while (position != std::string::npos && position < to_pos)
00192 {
00193 str.replace(position, oldsubstr.size(), newsubstr);
00194 position = str.find(oldsubstr, position + newsubstr.size());
00195 }
00196
00197 return str;
00198 }
00199
00200 std::string&
00201 marojson::replace_all(std::string& str, const std::string &oldsubstr, const std::string &newsubstr)
00202 {
00203 return replace_between(str, oldsubstr, newsubstr, 0, str.size() - 1);
00204 }
00205
00206 std::string&
00207 marojson::smart_indent(std::string& str, const std::string &start, const std::string &end)
00208 {
00209 std::string::size_type position;
00210
00211 position = str.find(start);
00212 while (position != std::string::npos)
00213 {
00214 str.replace(position, start.size(), start);
00215 position = str.find(start, position + start.size());
00216 }
00217
00218 return str;
00219 }