JSON.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 ERROR_VARIABLE error GET "${json1}" foo)
  114. assert_strequal_error("${result}" "bar" "${error}")
  115. string(JSON result ERROR_VARIABLE error GET "${json1}" notThere)
  116. assert_strequal("${result}" "notThere-NOTFOUND")
  117. assert_strequal("${error}" "member 'notThere' not found")
  118. string(JSON result ERROR_VARIABLE error GET "${json1}" 0)
  119. assert_strequal("${result}" "0-NOTFOUND")
  120. assert_strequal("${error}" "member '0' not found")
  121. string(JSON result ERROR_VARIABLE error GET "${json1}" array 10)
  122. assert_strequal("${result}" "array-10-NOTFOUND")
  123. assert_strequal("${error}" "expected an index less than 4 got '10'")
  124. string(JSON result ERROR_VARIABLE error GET "${json1}" array 2 some notThere)
  125. assert_strequal("${result}" "array-2-some-notThere-NOTFOUND")
  126. assert_strequal("${error}" "invalid path 'array 2 some notThere', need element of OBJECT or ARRAY type to lookup 'notThere' got STRING")
  127. # special chars
  128. string(JSON result ERROR_VARIABLE error GET "${json1}" special "foo;bar")
  129. assert_strequal_error("${result}" "value1" "${error}")
  130. string(JSON result ERROR_VARIABLE error GET "${json1}" special ";")
  131. assert_strequal_error("${result}" "value2" "${error}")
  132. string(JSON result ERROR_VARIABLE error GET "${json1}" special semicolon)
  133. assert_strequal_error("${result}" ";" "${error}")
  134. string(JSON result ERROR_VARIABLE error GET "${json1}" special list 1)
  135. assert_strequal_error("${result}" "two;three" "${error}")
  136. string(JSON result ERROR_VARIABLE error GET "${json1}")
  137. assert_json_equal("${error}" "${result}" "${json1}")
  138. string(JSON result ERROR_VARIABLE error GET "${json1}" array)
  139. assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
  140. string(JSON result ERROR_VARIABLE error GET "${json1}" special quote)
  141. assert_strequal_error("${result}" "\"" "${error}")
  142. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\"")
  143. assert_strequal_error("${result}" "quote" "${error}")
  144. string(JSON result ERROR_VARIABLE error GET "${json1}" special backslash)
  145. assert_strequal_error("${result}" "\\" "${error}")
  146. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\\")
  147. assert_strequal_error("${result}" "backslash" "${error}")
  148. string(JSON result ERROR_VARIABLE error GET "${json1}" special slash)
  149. assert_strequal_error("${result}" "/" "${error}")
  150. string(JSON result ERROR_VARIABLE error GET "${json1}" special "/")
  151. assert_strequal_error("${result}" "slash" "${error}")
  152. string(JSON result ERROR_VARIABLE error GET "${json1}" special newline)
  153. assert_strequal_error("${result}" "\n" "${error}")
  154. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\n")
  155. assert_strequal_error("${result}" "newline" "${error}")
  156. string(JSON result ERROR_VARIABLE error GET "${json1}" special return)
  157. assert_strequal_error("${result}" "\r" "${error}")
  158. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\r")
  159. assert_strequal_error("${result}" "return" "${error}")
  160. string(JSON result ERROR_VARIABLE error GET "${json1}" special tab)
  161. assert_strequal_error("${result}" "\t" "${error}")
  162. string(JSON result ERROR_VARIABLE error GET "${json1}" special "\t")
  163. assert_strequal_error("${result}" "tab" "${error}")
  164. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  165. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  166. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  167. assert_strequal_error("${result}" "backspace" "${error}")
  168. file(READ ${CMAKE_CURRENT_LIST_DIR}/json/unicode.json unicode)
  169. string(JSON char ERROR_VARIABLE error GET "${unicode}" backspace)
  170. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  171. assert_strequal_error("${result}" "backspace" "${error}")
  172. string(JSON char ERROR_VARIABLE error GET "${unicode}" formfeed)
  173. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  174. assert_strequal_error("${result}" "formfeed" "${error}")
  175. string(JSON char ERROR_VARIABLE error GET "${unicode}" datalinkescape)
  176. string(JSON result ERROR_VARIABLE error GET "${unicode}" "${char}")
  177. assert_strequal_error("${result}" "datalinkescape" "${error}")
  178. # Test GET_RAW
  179. string(JSON result GET_RAW "${json1}" values null)
  180. assert_strequal("${result}" null)
  181. string(JSON result GET_RAW "${json1}" values number)
  182. assert_strequal("${result}" 5)
  183. string(JSON result GET_RAW "${json1}" values string)
  184. assert_strequal("${result}" "\"foo\"")
  185. string(JSON result GET_RAW "${json1}" values false)
  186. assert_strequal("${result}" false)
  187. string(JSON result GET_RAW "${json1}" values true)
  188. assert_strequal("${result}" true)
  189. string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" array)
  190. assert_json_equal("${error}" "${result}" [=[ [5, "val", {"some": "other"}, null] ]=])
  191. string(JSON result ERROR_VARIABLE error GET_RAW "${json1}" object)
  192. assert_json_equal("${error}" "${result}" [=[ { "foo": "bar" } ]=])
  193. # Test TYPE
  194. string(JSON result TYPE "${json1}" types null)
  195. assert_strequal("${result}" NULL)
  196. string(JSON result TYPE "${json1}" types number)
  197. assert_strequal("${result}" NUMBER)
  198. string(JSON result TYPE "${json1}" types string)
  199. assert_strequal("${result}" STRING)
  200. string(JSON result TYPE "${json1}" types boolean)
  201. assert_strequal("${result}" BOOLEAN)
  202. string(JSON result TYPE "${json1}" types array)
  203. assert_strequal("${result}" ARRAY)
  204. string(JSON result TYPE "${json1}" types object)
  205. assert_strequal("${result}" OBJECT)
  206. # Test LENGTH
  207. string(JSON result ERROR_VARIABLE error LENGTH "${json1}")
  208. assert_strequal("${result}" 6)
  209. if(error)
  210. message(SEND_ERROR "Unexpected error: ${error}")
  211. endif()
  212. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" array)
  213. assert_strequal("${result}" 4)
  214. if(error)
  215. message(SEND_ERROR "Unexpected error: ${error}")
  216. endif()
  217. string(JSON result ERROR_VARIABLE error LENGTH "${json1}" foo)
  218. assert_strequal("${result}" "foo-NOTFOUND")
  219. assert_strequal("${error}" "LENGTH needs to be called with an element of type ARRAY or OBJECT, got STRING")
  220. # Test MEMBER
  221. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 2)
  222. assert_strequal("${result}" "number")
  223. if(error)
  224. message(SEND_ERROR "Unexpected error: ${error}")
  225. endif()
  226. string(JSON result ERROR_VARIABLE error MEMBER "${json1}" values 100)
  227. assert_strequal("${result}" "values-100-NOTFOUND")
  228. assert_strequal("${error}" "expected an index less than 5 got '100'")
  229. # Test length loops
  230. string(JSON arrayLength ERROR_VARIABLE error LENGTH "${json1}" types array)
  231. if(error)
  232. message(SEND_ERROR "Unexpected error: ${error}")
  233. endif()
  234. set(values "")
  235. math(EXPR arrayLength "${arrayLength}-1")
  236. foreach(index RANGE ${arrayLength})
  237. string(JSON value ERROR_VARIABLE error GET "${json1}" types array ${index})
  238. if(error)
  239. message(SEND_ERROR "Unexpected error: ${error}")
  240. endif()
  241. list(APPEND values "${value}")
  242. endforeach()
  243. assert_strequal("${values}" "1;2;3")
  244. string(JSON valuesLength ERROR_VARIABLE error LENGTH "${json1}" values)
  245. if(error)
  246. message(SEND_ERROR "Unexpected error: ${error}")
  247. endif()
  248. set(values "")
  249. set(members "")
  250. math(EXPR valuesLength "${valuesLength}-1")
  251. foreach(index RANGE ${valuesLength})
  252. string(JSON member ERROR_VARIABLE error MEMBER "${json1}" values ${index})
  253. if(error)
  254. message(SEND_ERROR "Unexpected error: ${error}")
  255. endif()
  256. string(JSON value ERROR_VARIABLE error GET "${json1}" values ${member})
  257. if(error)
  258. message(SEND_ERROR "Unexpected error: ${error}")
  259. endif()
  260. list(APPEND members "${member}")
  261. list(APPEND values "${value}")
  262. endforeach()
  263. assert_strequal("${members}" "false;null;number;string;true")
  264. assert_strequal("${values}" "OFF;;5;foo;ON")
  265. # Test REMOVE
  266. set(json2 [=[{
  267. "foo" : "bar",
  268. "array" : [5, "val", {"some": "other"}, null]
  269. }]=])
  270. string(JSON result ERROR_VARIABLE error REMOVE ${json2} foo)
  271. assert_json_equal("${error}" "${result}"
  272. [=[{
  273. "array" : [5, "val", {"some": "other"}, null]
  274. }]=])
  275. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 1)
  276. assert_json_equal("${error}" "${result}"
  277. [=[{
  278. "foo" : "bar",
  279. "array" : [5, {"some": "other"}, null]
  280. }]=])
  281. string(JSON result ERROR_VARIABLE error REMOVE ${json2} array 100)
  282. assert_strequal("${result}" "array-100-NOTFOUND")
  283. assert_strequal("${error}" "expected an index less than 4 got '100'")
  284. # Test SET
  285. string(JSON result ERROR_VARIABLE error SET ${json2} new 5)
  286. assert_json_equal("${error}" "${result}"
  287. [=[{
  288. "foo" : "bar",
  289. "array" : [5, "val", {"some": "other"}, null],
  290. "new" : 5
  291. }]=])
  292. string(JSON result ERROR_VARIABLE error SET ${json2} new [=[ {"obj" : false} ]=])
  293. assert_json_equal("${error}" "${result}"
  294. [=[{
  295. "foo" : "bar",
  296. "array" : [5, "val", {"some": "other"}, null],
  297. "new" : {"obj" : false}
  298. }]=])
  299. string(JSON result ERROR_VARIABLE error SET ${json2} array 0 6)
  300. assert_json_equal("${error}" "${result}"
  301. [=[{
  302. "foo" : "bar",
  303. "array" : [6, "val", {"some": "other"}, null]
  304. }]=])
  305. string(JSON result ERROR_VARIABLE error SET ${json2} array 5 [["append"]])
  306. assert_json_equal("${error}" "${result}"
  307. [=[{
  308. "foo" : "bar",
  309. "array" : [5, "val", {"some": "other"}, null, "append"]
  310. }]=])
  311. string(JSON result ERROR_VARIABLE error SET ${json2} array 100 [["append"]])
  312. assert_json_equal("${error}" "${result}"
  313. [=[{
  314. "foo" : "bar",
  315. "array" : [5, "val", {"some": "other"}, null, "append"]
  316. }]=])
  317. # Test STRING_ENCODE
  318. string(JSON result STRING_ENCODE Hello)
  319. assert_strequal("${result}" "\"Hello\"")
  320. string(JSON result STRING_ENCODE "\"Hello\"")
  321. assert_strequal("${result}" "\"\\\"Hello\\\"\"")
  322. string(JSON result STRING_ENCODE null)
  323. assert_strequal("${result}" "\"null\"")
  324. string(JSON result STRING_ENCODE 0)
  325. assert_strequal("${result}" "\"0\"")
  326. string(JSON result STRING_ENCODE false)
  327. assert_strequal("${result}" "\"false\"")
  328. string(JSON result STRING_ENCODE {})
  329. assert_strequal("${result}" "\"{}\"")
  330. string(JSON result STRING_ENCODE [])
  331. assert_strequal("${result}" "\"[]\"")