jsonhelpers.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // jsonhelpers.hpp:
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #pragma warning( disable : 4786)
  17. #ifndef _JSONHELPERS_HPP__
  18. #define _JSONHELPERS_HPP__
  19. #include "jliball.hpp"
  20. #include "wsexcept.hpp"
  21. #define REQSF_ROOT 0x0001
  22. #define REQSF_SAMPLE_DATA 0x0002
  23. #define REQSF_TRIM 0x0004
  24. #define REQSF_ESCAPEFORMATTERS 0x0008
  25. #define REQSF_EXCLUSIVE (REQSF_SAMPLE_DATA | REQSF_TRIM)
  26. namespace HttpParamHelpers
  27. {
  28. static const char * nextParameterTag(StringBuffer &tag, const char *path)
  29. {
  30. while (*path=='.')
  31. path++;
  32. const char *finger = strchr(path, '.');
  33. if (finger)
  34. {
  35. tag.clear().append(finger - path, path);
  36. finger++;
  37. }
  38. else
  39. tag.set(path);
  40. return finger;
  41. }
  42. static void ensureParameter(IPropertyTree *pt, StringBuffer &tag, const char *path, const char *value, const char *fullpath)
  43. {
  44. if (!tag.length())
  45. return;
  46. unsigned idx = 1;
  47. if (path && isdigit(*path))
  48. {
  49. StringBuffer pos;
  50. path = nextParameterTag(pos, path);
  51. idx = (unsigned) atoi(pos.str())+1;
  52. if (idx>25) //adf
  53. throw MakeStringException(-1, "Array items above 25 not supported in HPCC WS HTTP parameters: %s", fullpath);
  54. }
  55. if (tag.charAt(tag.length()-1)=='$')
  56. {
  57. if (path && *path)
  58. throw MakeStringException(-1, "'$' not allowed in parent node of parameter path: %s", fullpath);
  59. tag.setLength(tag.length()-1);
  60. StringArray values;
  61. values.appendList(value, "\r");
  62. ForEachItemIn(pos, values)
  63. {
  64. const char *itemValue = values.item(pos);
  65. while (*itemValue=='\n')
  66. itemValue++;
  67. pt->addProp(tag, itemValue);
  68. }
  69. return;
  70. }
  71. unsigned count = pt->getCount(tag);
  72. while (count++ < idx)
  73. pt->addPropTree(tag, createPTree(tag));
  74. StringBuffer xpath(tag);
  75. xpath.append('[').append(idx).append(']');
  76. pt = pt->queryPropTree(xpath);
  77. if (!path || !*path)
  78. {
  79. pt->setProp(NULL, value);
  80. return;
  81. }
  82. StringBuffer nextTag;
  83. path = HttpParamHelpers::nextParameterTag(nextTag, path);
  84. ensureParameter(pt, nextTag, path, value, fullpath);
  85. }
  86. static void ensureParameter(IPropertyTree *pt, const char *path, const char *value)
  87. {
  88. const char *fullpath = path;
  89. StringBuffer tag;
  90. path = HttpParamHelpers::nextParameterTag(tag, path);
  91. ensureParameter(pt, tag, path, value, fullpath);
  92. }
  93. static IPropertyTree *createPTreeFromHttpParameters(const char *name, IProperties *parameters)
  94. {
  95. Owned<IPropertyTree> pt = createPTree(name);
  96. Owned<IPropertyIterator> props = parameters->getIterator();
  97. ForEach(*props)
  98. {
  99. StringBuffer key = props->getPropKey();
  100. if (!key.length() || key.charAt(key.length()-1)=='!')
  101. continue;
  102. const char *value = parameters->queryProp(key);
  103. if (value && *value)
  104. ensureParameter(pt, key, value);
  105. }
  106. return pt.getClear();
  107. }
  108. };
  109. namespace JsonHelpers
  110. {
  111. static StringBuffer &appendJSONExceptionItem(StringBuffer &s, int code, const char *msg, const char *objname="Exceptions", const char *arrayName = "Exception")
  112. {
  113. if (objname && *objname)
  114. appendJSONName(s, objname).append('{');
  115. if (arrayName && *arrayName)
  116. appendJSONName(s, arrayName).append('[');
  117. delimitJSON(s);
  118. s.append('{');
  119. appendJSONValue(s, "Code", code);
  120. appendJSONValue(s, "Message", msg);
  121. s.append('}');
  122. if (arrayName && *arrayName)
  123. s.append(']');
  124. if (objname && *objname)
  125. s.append('}');
  126. return s;
  127. }
  128. static StringBuffer &appendJSONException(StringBuffer &s, IException *e, const char *objname="Exceptions", const char *arrayName = "Exception")
  129. {
  130. if (!e)
  131. return s;
  132. StringBuffer temp;
  133. return appendJSONExceptionItem(s, e->errorCode(), e->errorMessage(temp).str(), objname, arrayName);
  134. }
  135. static StringBuffer &appendJSONException(StringBuffer &s, IWsException *e, const char *objname="Exceptions", const char *arrayName = "Exception")
  136. {
  137. if (!e)
  138. return s;
  139. StringBuffer temp;
  140. IArrayOf<IException>& exceptions = e->getArray();
  141. for (int i = 0 ; i < exceptions.ordinality(); i++)
  142. {
  143. appendJSONExceptionItem(s, e->errorCode(), e->errorMessage(temp.clear()).str(), objname, arrayName);
  144. }
  145. return s;
  146. }
  147. static StringBuffer &appendJSONExceptions(StringBuffer &s, IMultiException *e, const char *objname="Exceptions", const char *arrayName = "Exception")
  148. {
  149. if (!e)
  150. return s;
  151. if (objname && *objname)
  152. appendJSONName(s, objname).append('{');
  153. if (arrayName && *arrayName)
  154. appendJSONName(s, arrayName).append('[');
  155. ForEachItemIn(i, *e)
  156. appendJSONException(s, &e->item(i), NULL, NULL);
  157. if (arrayName && *arrayName)
  158. s.append(']');
  159. if (objname && *objname)
  160. s.append('}');
  161. return s;
  162. }
  163. static IException *MakeJSONValueException(int code, const char *start, const char *pos, const char *tail, const char *intro="Invalid json format: ")
  164. {
  165. StringBuffer s(intro);
  166. s.append(pos-start, start).append('^').append(pos);
  167. if (tail && *tail)
  168. s.append(" - ").append(tail);
  169. return MakeStringException(code, "%s", s.str());
  170. }
  171. static inline StringBuffer &jsonNumericNext(StringBuffer &s, const char *&c, bool &allowDecimal, bool &allowExponent, const char *start)
  172. {
  173. if (isdigit(*c))
  174. s.append(*c++);
  175. else if ('.'==*c)
  176. {
  177. if (!allowDecimal || !allowExponent)
  178. throw MakeJSONValueException(-1, start, c, "Unexpected decimal");
  179. allowDecimal=false;
  180. s.append(*c++);
  181. }
  182. else if ('e'==*c || 'E'==*c)
  183. {
  184. if (!allowExponent)
  185. throw MakeJSONValueException(-1, start, c, "Unexpected exponent");
  186. allowDecimal=false;
  187. allowExponent=false;
  188. s.append(*c++);
  189. if ('-'==*c || '+'==*c)
  190. s.append(*c++);
  191. if (!isdigit(*c))
  192. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  193. }
  194. else
  195. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  196. return s;
  197. }
  198. static inline StringBuffer &jsonNumericStart(StringBuffer &s, const char *&c, const char *start)
  199. {
  200. if ('-'==*c)
  201. return jsonNumericStart(s.append(*c++), c, start);
  202. else if ('0'==*c)
  203. {
  204. s.append(*c++);
  205. if (*c && '.'!=*c)
  206. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  207. }
  208. else if (isdigit(*c))
  209. s.append(*c++);
  210. else
  211. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  212. return s;
  213. }
  214. static StringBuffer &appendJSONNumericString(StringBuffer &s, const char *value, bool allowDecimal)
  215. {
  216. if (!value || !*value)
  217. return s.append("null");
  218. bool allowExponent = allowDecimal;
  219. const char *pos = value;
  220. jsonNumericStart(s, pos, value);
  221. while (*pos)
  222. jsonNumericNext(s, pos, allowDecimal, allowExponent, value);
  223. return s;
  224. }
  225. typedef enum _JSONFieldCategory
  226. {
  227. JSONField_String,
  228. JSONField_Integer,
  229. JSONField_Real,
  230. JSONField_Boolean,
  231. JSONField_Present //true or remove
  232. } JSONField_Category;
  233. static JSONField_Category xsdTypeToJSONFieldCategory(const char *xsdtype)
  234. {
  235. //map XML Schema types used in ECL generated schemas to basic JSON formatting types
  236. if (streq(xsdtype, "integer") || streq(xsdtype, "nonNegativeInteger"))
  237. return JSONField_Integer;
  238. if (streq(xsdtype, "boolean"))
  239. return JSONField_Boolean;
  240. if (streq(xsdtype, "double"))
  241. return JSONField_Real;
  242. if (!strncmp(xsdtype, "decimal", 7)) //ecl creates derived types of the form decimal#_#
  243. return JSONField_Real;
  244. if (streq(xsdtype, "none")) //maps to an eml schema element with no type. set to true or don't add
  245. return JSONField_Present;
  246. return JSONField_String;
  247. }
  248. static void buildJsonAppendValue(IXmlType* type, StringBuffer& out, const char* tag, const char *value, unsigned flags)
  249. {
  250. JSONField_Category ct = xsdTypeToJSONFieldCategory(type->queryName());
  251. if (ct==JSONField_Present && (!value || !*value))
  252. return;
  253. if (tag && *tag)
  254. out.appendf("\"%s\": ", tag);
  255. StringBuffer sample;
  256. if ((!value || !*value) && (flags & REQSF_SAMPLE_DATA))
  257. {
  258. type->getSampleValue(sample, NULL);
  259. value = sample.str();
  260. }
  261. if (value)
  262. {
  263. switch (ct)
  264. {
  265. case JSONField_String:
  266. appendJSONValue(out, NULL, value);
  267. break;
  268. case JSONField_Integer:
  269. appendJSONNumericString(out, value, false);
  270. break;
  271. case JSONField_Real:
  272. appendJSONNumericString(out, value, true);
  273. break;
  274. case JSONField_Boolean:
  275. if (strieq(value, "default"))
  276. out.append("null");
  277. else
  278. appendJSONValue(out, NULL, strToBool(value));
  279. break;
  280. case JSONField_Present:
  281. appendJSONValue(out, NULL, true);
  282. break;
  283. }
  284. }
  285. else
  286. out.append("null");
  287. }
  288. static void buildJsonMsg(StringArray& parentTypes, IXmlType* type, StringBuffer& out, const char* tag, IPropertyTree *reqTree, unsigned flags)
  289. {
  290. assertex(type!=NULL);
  291. if (flags & REQSF_ROOT)
  292. out.append("{");
  293. const char* typeName = type->queryName();
  294. if (type->isComplexType())
  295. {
  296. if (typeName && !parentTypes.appendUniq(typeName))
  297. return; // recursive
  298. int startlen = out.length();
  299. if (tag)
  300. appendJSONName(out, tag);
  301. out.append('{');
  302. int taglen=out.length()+1;
  303. if (type->getSubType()==SubType_Complex_SimpleContent)
  304. {
  305. if (reqTree)
  306. {
  307. const char *attrval = reqTree->queryProp(NULL);
  308. out.appendf("\"%s\" ", (attrval) ? attrval : "");
  309. }
  310. else if (flags & REQSF_SAMPLE_DATA)
  311. {
  312. out.append("\"");
  313. type->queryFieldType(0)->getSampleValue(out,tag);
  314. out.append("\" ");
  315. }
  316. }
  317. else
  318. {
  319. int flds = type->getFieldCount();
  320. for (int idx=0; idx<flds; idx++)
  321. {
  322. delimitJSON(out);
  323. IPropertyTree *childtree = NULL;
  324. const char *childname = type->queryFieldName(idx);
  325. if (reqTree)
  326. childtree = reqTree->queryPropTree(childname);
  327. buildJsonMsg(parentTypes, type->queryFieldType(idx), out, childname, childtree, flags & ~REQSF_ROOT);
  328. }
  329. }
  330. if (typeName)
  331. parentTypes.pop();
  332. out.append("}");
  333. }
  334. else if (type->isArray())
  335. {
  336. if (typeName && !parentTypes.appendUniq(typeName))
  337. return; // recursive
  338. const char* itemName = type->queryFieldName(0);
  339. IXmlType* itemType = type->queryFieldType(0);
  340. if (!itemName || !itemType)
  341. throw MakeStringException(-1,"*** Invalid array definition: tag=%s, itemName=%s", tag, itemName?itemName:"NULL");
  342. int startlen = out.length();
  343. if (tag)
  344. out.appendf("\"%s\": ", tag);
  345. out.append('{');
  346. out.appendf("\"%s\": [", itemName);
  347. int taglen=out.length();
  348. if (reqTree)
  349. {
  350. Owned<IPropertyTreeIterator> items = reqTree->getElements(itemName);
  351. ForEach(*items)
  352. buildJsonMsg(parentTypes, itemType, delimitJSON(out), NULL, &items->query(), flags & ~REQSF_ROOT);
  353. }
  354. else
  355. buildJsonMsg(parentTypes, itemType, out, NULL, NULL, flags & ~REQSF_ROOT);
  356. out.append(']');
  357. if (typeName)
  358. parentTypes.pop();
  359. out.append("}");
  360. }
  361. else // simple type
  362. {
  363. const char *parmval = (reqTree) ? reqTree->queryProp(NULL) : NULL;
  364. buildJsonAppendValue(type, out, tag, parmval, flags);
  365. }
  366. if (flags & REQSF_ROOT)
  367. out.append('}');
  368. }
  369. };
  370. #endif // _JSONHELPERS_HPP__