JSON.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. function(assert_strequal actual expected)
  2. if(NOT expected STREQUAL actual)
  3. message(SEND_ERROR "Output:\n${actual}\nDid not match expected:\n${expected}\n")
  4. endif()
  5. endfunction()
  6. function(assert_strequal_error actual expected error)
  7. if(error)
  8. message(SEND_ERROR "Unexpected error: ${error}")
  9. endif()
  10. assert_strequal("${actual}" "${expected}")
  11. endfunction()
  12. function(assert_json_equal error actual expected)
  13. if(error)
  14. message(SEND_ERROR "Unexpected error: ${error}")
  15. endif()
  16. string(JSON eql EQUAL "${actual}" "${expected}")
  17. if(NOT eql)
  18. message(SEND_ERROR "Expected equality got\n ${actual}\n expected\n${expected}")
  19. endif()
  20. endfunction()
  21. # test EQUAL
  22. string(JSON result EQUAL
  23. [=[ {"foo":"bar"} ]=]
  24. [=[
  25. {
  26. "foo": "bar"
  27. }
  28. ]=])
  29. if(NOT result)
  30. message(SEND_ERROR "Expected ON got ${result}")
  31. endif()
  32. string(JSON result EQUAL
  33. [=[ {"foo":"bar"} ]=]
  34. [=[
  35. {
  36. "foo1": "bar"
  37. }
  38. ]=])
  39. if(result)
  40. message(SEND_ERROR "Expected OFF got ${result}")
  41. endif()
  42. set(json1 [=[
  43. {
  44. "foo" : "bar",
  45. "array" : [5, "val", {"some": "other"}, null],
  46. "types" : {
  47. "null" : null,
  48. "number" : 5,
  49. "string" : "foo",
  50. "boolean" : false,
  51. "array" : [1,2,3],
  52. "object" : {}
  53. },
  54. "values" : {
  55. "null" : null,
  56. "number" : 5,
  57. "string" : "foo",
  58. "false" : false,
  59. "true" : true
  60. },
  61. "object": {
  62. "foo": "bar"
  63. },
  64. "special" : {
  65. "foo;bar" : "value1",
  66. ";" : "value2",
  67. "semicolon" : ";",
  68. "list" : ["one", "two;three", "four"],
  69. "quote" : "\"",
  70. "\"" : "quote",
  71. "backslash" : "\\",
  72. "\\" : "backslash",
  73. "slash" : "\/",
  74. "\/" : "slash",
  75. "newline" : "\n",
  76. "\n" : "newline",
  77. "return" : "\r",
  78. "\r" : "return",
  79. "tab" : "\t",
  80. "\t" : "tab",
  81. "backspace" : "\b",
  82. "\b" : "backspace",
  83. "formfeed" : "\f",
  84. "\f" : "formfeed"
  85. }
  86. }
  87. ]=])
  88. # test GET
  89. string(JSON result GET "${json1}" foo)
  90. assert_strequal("${result}" bar)
  91. string(JSON result GET "${json1}" array 0)
  92. assert_strequal("${result}" 5)
  93. string(JSON result GET "${json1}" array 1)
  94. assert_strequal("${result}" val)
  95. string(JSON result GET "${json1}" array 2 some)
  96. assert_strequal("${result}" other)
  97. string(JSON result GET "${json1}" values null)
  98. assert_strequal("${result}" "")
  99. string(JSON result GET "${json1}" values number)
  100. assert_strequal("${result}" 5)
  101. string(JSON result GET "${json1}" values string)
  102. assert_strequal("${result}" "foo")
  103. string(JSON result GET "${json1}" values true)
  104. assert_strequal("${result}" "ON")
  105. if(NOT result)
  106. message(SEND_ERROR "Output did not match expected: TRUE actual: ${result}")
  107. endif()
  108. string(JSON result GET "${json1}" values false)
  109. assert_strequal("${result}" "OFF")
  110. if(result)
  111. message(SEND_ERROR "Output did not match expected: FALSE actual: ${result}")
  112. endif()
  113. string(JSON result GET "\"Hello\"")
  114. assert_strequal("${result}" Hello)
  115. string(JSON result GET "5")
  116. assert_strequal("${result}" 5)
  117. string(JSON result GET "null")
  118. assert_strequal("${result}" "")
  119. string(JSON result ERROR_VARIABLE error GET "{}")
  120. assert_json_equal("${error}" "${result}" "{}")
  121. string(JSON result ERROR_VARIABLE error GET "[]")
  122. assert_json_equal("${error}" "${result}" "[]")
  123. string(JSON result ERROR_VARIABLE error GET "${json1}" foo)
  124. assert_strequal_error("${result}" "bar" "${error}")
  125. string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
  126. assert_strequal("${result}" "notThere-NOTFOUND")
  127. assert_strequal("${error}" "member 'notThere' not found")
  128. string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
  129. assert_strequal("${result}" "0-NOTFOUND")
  130. assert_strequal("${error}" "member '0' not found")
  131. string(JSON result ERROR_VARIABLE error GET "${json1}" array 10)
  132. assert_strequal("${result}" "array-10-NOTFOUND")
  133. assert_strequal("${error}" "expected an index less than 4 got '10'")
  134. string(JSON result ERROR_VARIABLE error GET "${json1}" array 2 some notThere)
  135. assert_strequal("${result}" "array-2-some-notThere-NOTFOUND")
  136. assert_strequal("${error}" "invalid path 'array 2 some notThere', need element of OBJECT or ARRAY type to lookup 'notThere' got STRING")
  137. # special chars
  138. string(JSON result ERROR_VARIABLE error GET "${json1}" special "foo;bar")
  139. assert_strequal_error("${result}" "value1" "${error}")
  140. string(JSON result ERROR_VARIABLE error GET "${json1}" special ";")
  141. assert_strequal_error("${result}" "value2" "${error}")
  142. string(JSON result ERROR_VARIABLE error GET "${json1}" special semicolon)
  143. assert_strequal_error("${result}" ";" "${error}")
  144. string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
  145. assert_strequal_error("${result}" "two;three" "${error}")
  146. string(JSON result ERROR_VARIABLE error GET "${json1}")
  147. assert_json_equal("${error}" "${result}" "${json1}")
  148. string(JSON result ERROR_VARIABLE error GET "${json1}" array)
  149. assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
  150. string(JSON result ERROR_VARIABLE error GET "${json1}" special quote)
  151. assert_strequal_error("${result}" "\"" "${error}")
  152. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\"")
  153. assert_strequal_error("${result}" "quote" "${error}")
  154. string(JSON result ERROR_VARIABLE error GET "${json1}" special backslash)
  155. assert_strequal_error("${result}" "\\" "${error}")
  156. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\\")
  157. assert_strequal_error("${result}" "backslash" "${error}")
  158. string(JSON result ERROR_VARIABLE error GET "${json1}" special slash)
  159. assert_strequal_error("${result}" "/" "${error}")
  160. string(JSON result ERROR_VARIABLE error GET "${json1}" special "/")
  161. assert_strequal_error("${result}" "slash" "${error}")
  162. string(JSON result ERROR_VARIABLE error GET "${json1}" special newline)
  163. assert_strequal_error("${result}" "\n" "${error}")
  164. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\n")
  165. assert_strequal_error("${result}" "newline" "${error}")
  166. string(JSON result ERROR_VARIABLE error GET "${json1}" special return)
  167. assert_strequal_error("${result}" "\r" "${error}")
  168. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\r")
  169. assert_strequal_error("${result}" "return" "${error}")
  170. string(JSON result ERROR_VARIABLE error GET "${json1}" special tab)
  171. assert_strequal_error("${result}" "\t" "${error}")
  172. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\t")
  173. assert_strequal_error("${result}" "tab" "${error}")
  174. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  175. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  176. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  177. assert_strequal_error("${result}" "backspace" "${error}")
  178. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  179. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  180. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  181. assert_strequal_error("${result}" "backspace" "${error}")
  182. string(JSON char ERROR_VARIABLE error GET "${unicode}" formfeed)
  183. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  184. assert_strequal_error("${result}" "formfeed" "${error}")
  185. string(JSON char ERROR_VARIABLE error GET "${unicode}" datalinkescape)
  186. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  187. assert_strequal_error("${result}" "datalinkescape" "${error}")
  188. # Test GET_RAW
  189. string(JSON result GET_RAW "${json1}" values null)
  190. assert_strequal("${result}" null)
  191. string(JSON result GET_RAW "${json1}" values number)
  192. assert_strequal("${result}" 5)
  193. string(JSON result GET_RAW "${json1}" values string)
  194. assert_strequal("${result}" "\"foo\"")
  195. string(JSON result GET_RAW "${json1}" values false)
  196. assert_strequal("${result}" false)
  197. string(JSON result GET_RAW "${json1}" values true)
  198. assert_strequal("${result}" true)
  199. string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" array)
  200. assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
  201. string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" object)
  202. assert_json_equal("${error}" "${result}" [=[ { "foo": "bar" } ]=])
  203. # Test TYPE
  204. string(JSON result TYPE "${json1}" types null)
  205. assert_strequal("${result}" NULL)
  206. string(JSON result TYPE "${json1}" types number)
  207. assert_strequal("${result}" NUMBER)
  208. string(JSON result TYPE "${json1}" types string)
  209. assert_strequal("${result}" STRING)
  210. string(JSON result TYPE "${json1}" types boolean)
  211. assert_strequal("${result}" BOOLEAN)
  212. string(JSON result TYPE "${json1}" types array)
  213. assert_strequal("${result}" ARRAY)
  214. string(JSON result TYPE "${json1}" types object)
  215. assert_strequal("${result}" OBJECT)
  216. string(JSON result TYPE "null")
  217. assert_strequal("${result}" NULL)
  218. string(JSON result TYPE "5")
  219. assert_strequal("${result}" NUMBER)
  220. string(JSON result TYPE "\"Hello\"")
  221. assert_strequal("${result}" STRING)
  222. # Test LENGTH
  223. string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
  224. assert_strequal("${result}" 6)
  225. if(error)
  226. message(SEND_ERROR "Unexpected error: ${error}")
  227. endif()
  228. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
  229. assert_strequal("${result}" 4)
  230. if(error)
  231. message(SEND_ERROR "Unexpected error: ${error}")
  232. endif()
  233. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" foo)
  234. assert_strequal("${result}" "foo-NOTFOUND")
  235. assert_strequal("${error}" "LENGTH needs to be called with an element of type ARRAY or OBJECT, got STRING")
  236. # Test MEMBER
  237. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
  238. assert_strequal("${result}" "number")
  239. if(error)
  240. message(SEND_ERROR "Unexpected error: ${error}")
  241. endif()
  242. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 100)
  243. assert_strequal("${result}" "values-100-NOTFOUND")
  244. assert_strequal("${error}" "expected an index less than 5 got '100'")
  245. # Test length loops
  246. string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
  247. if(error)
  248. message(SEND_ERROR "Unexpected error: ${error}")
  249. endif()
  250. set(values "")
  251. math(EXPR arrayLength "${arrayLength}-1")
  252. foreach(index RANGE ${arrayLength})
  253. string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
  254. if(error)
  255. message(SEND_ERROR "Unexpected error: ${error}")
  256. endif()
  257. list(APPEND values "${value}")
  258. endforeach()
  259. assert_strequal("${values}" "1;2;3")
  260. string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
  261. if(error)
  262. message(SEND_ERROR "Unexpected error: ${error}")
  263. endif()
  264. set(values "")
  265. set(members "")
  266. math(EXPR valuesLength "${valuesLength}-1")
  267. foreach(index RANGE ${valuesLength})
  268. string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
  269. if(error)
  270. message(SEND_ERROR "Unexpected error: ${error}")
  271. endif()
  272. string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
  273. if(error)
  274. message(SEND_ERROR "Unexpected error: ${error}")
  275. endif()
  276. list(APPEND members "${member}")
  277. list(APPEND values "${value}")
  278. endforeach()
  279. assert_strequal("${members}" "false;null;number;string;true")
  280. assert_strequal("${values}" "OFF;;5;foo;ON")
  281. # Test REMOVE
  282. set(json2 [=[{
  283. "foo" : "bar",
  284. "array" : [5, "val", {"some": "other"}, null]
  285. }]=])
  286. string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
  287. assert_json_equal("${error}" "${result}"
  288. [=[{
  289. "array" : [5, "val", {"some": "other"}, null]
  290. }]=])
  291. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
  292. assert_json_equal("${error}" "${result}"
  293. [=[{
  294. "foo" : "bar",
  295. "array" : [5, {"some": "other"}, null]
  296. }]=])
  297. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 100)
  298. assert_strequal("${result}" "array-100-NOTFOUND")
  299. assert_strequal("${error}" "expected an index less than 4 got '100'")
  300. # Test SET
  301. string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
  302. assert_json_equal("${error}" "${result}"
  303. [=[{
  304. "foo" : "bar",
  305. "array" : [5, "val", {"some": "other"}, null],
  306. "new" : 5
  307. }]=])
  308. string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
  309. assert_json_equal("${error}" "${result}"
  310. [=[{
  311. "foo" : "bar",
  312. "array" : [5, "val", {"some": "other"}, null],
  313. "new" : {"obj" : false}
  314. }]=])
  315. string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
  316. assert_json_equal("${error}" "${result}"
  317. [=[{
  318. "foo" : "bar",
  319. "array" : [6, "val", {"some": "other"}, null]
  320. }]=])
  321. string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
  322. assert_json_equal("${error}" "${result}"
  323. [=[{
  324. "foo" : "bar",
  325. "array" : [5, "val", {"some": "other"}, null, "append"]
  326. }]=])
  327. string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
  328. assert_json_equal("${error}" "${result}"
  329. [=[{
  330. "foo" : "bar",
  331. "array" : [5, "val", {"some": "other"}, null, "append"]
  332. }]=])
  333. # Test STRING_ENCODE
  334. string(JSON result STRING_ENCODE Hello)
  335. assert_strequal("${result}" "\"Hello\"")
  336. string(JSON result STRING_ENCODE "\"Hello\"")
  337. assert_strequal("${result}" "\"\\\"Hello\\\"\"")
  338. string(JSON result STRING_ENCODE null)
  339. assert_strequal("${result}" "\"null\"")
  340. string(JSON result STRING_ENCODE 0)
  341. assert_strequal("${result}" "\"0\"")
  342. string(JSON result STRING_ENCODE false)
  343. assert_strequal("${result}" "\"false\"")
  344. string(JSON result STRING_ENCODE {})
  345. assert_strequal("${result}" "\"{}\"")
  346. string(JSON result STRING_ENCODE [])
  347. assert_strequal("${result}" "\"[]\"")