glue.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright (c) 2014-2020 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "jansson.h"
  20. #include "brl.mod/blitz.mod/blitz.h"
  21. #ifdef BMX_NG
  22. #define CB_PREF(func) func
  23. #else
  24. #define CB_PREF(func) _##func
  25. #endif
  26. BBObject * CB_PREF(text_json_TJSON__create)(json_t * handle, int jsonType, BBString * key);
  27. BBObject * CB_PREF(text_json_TJSONError__createError)(BBString * text, BBString * source, int line, int column, int position, int errorCode);
  28. BBObject * CB_PREF(text_json_TJSONError__createNoError)(BBObject * js);
  29. void bmx_json_decref(json_t * handle);
  30. json_t * bmx_json_string_nocheck(BBString * text);
  31. BBString * bmx_json_string_value(json_t * handle);
  32. BBObject * bmx_json_array_get(json_t * handle, int index);
  33. int bmx_json_array_size(json_t * handle);
  34. int bmx_json_array_set(json_t * handle, int index, json_t * value);
  35. int bmx_json_array_append(json_t * handle, json_t * value);
  36. int bmx_json_array_insert(json_t * handle, int index, json_t * value);
  37. BBString * bmx_json_dumps(json_t * handle, int flags, int indent, int precision);
  38. int bmx_json_dump_callback(json_t * handle, json_dump_callback_t callback, BBObject * stream, int flags, int indent, int precision);
  39. BBObject * bmx_json_loads(BBString * text, int flags);
  40. BBObject * bmx_json_load_callback(json_load_callback_t callback, BBObject * stream, int flags);
  41. json_t * bmx_json_integer(BBInt64 v);
  42. void bmx_json_integer_value(json_t * handle, BBInt64 * v);
  43. int bmx_json_integer_set(json_t * handle, BBInt64 v);
  44. int bmx_json_object_size(json_t * handle);
  45. BBObject * bmx_json_object_get(json_t * handle, BBString * key);
  46. int bmx_json_object_set_nocheck(json_t * handle, BBString * key, json_t * value);
  47. int bmx_json_object_del(json_t * handle, BBString * key);
  48. BBObject * bmx_json_object_iter_value(void * iter);
  49. json_t * bmx_json_bool(int v);
  50. void bmx_json_decref(json_t * handle) {
  51. json_decref(handle);
  52. }
  53. json_t * bmx_json_string_nocheck(BBString * text) {
  54. char * s = bbStringToUTF8String(text);
  55. json_t * ref = json_string_nocheck(s);
  56. bbMemFree(s);
  57. return ref;
  58. }
  59. BBString * bmx_json_string_value(json_t * handle) {
  60. return bbStringFromUTF8String(json_string_value(handle));
  61. }
  62. BBObject * bmx_json_array_get(json_t * handle, int index) {
  63. json_t * value = json_array_get(handle, index);
  64. if (value) {
  65. return CB_PREF(text_json_TJSON__create)(json_incref(value), json_typeof(value), &bbEmptyString);
  66. } else {
  67. return &bbNullObject;
  68. }
  69. }
  70. int bmx_json_array_size(json_t * handle) {
  71. return json_array_size(handle);
  72. }
  73. int bmx_json_array_set(json_t * handle, int index, json_t * value) {
  74. return json_array_set(handle, index, value);
  75. }
  76. int bmx_json_array_append(json_t * handle, json_t * value) {
  77. return json_array_append(handle, value);
  78. }
  79. int bmx_json_array_insert(json_t * handle, int index, json_t * value) {
  80. return json_array_insert(handle, index, value);
  81. }
  82. BBString * bmx_json_dumps(json_t * handle, int flags, int indent, int precision) {
  83. char * s = json_dumps(handle, flags | JSON_INDENT(indent) | JSON_REAL_PRECISION(precision));
  84. if (s) {
  85. BBString * st = bbStringFromUTF8String(s);
  86. free(s);
  87. return st;
  88. } else {
  89. return &bbEmptyString;
  90. }
  91. }
  92. int bmx_json_dump_callback(json_t * handle, json_dump_callback_t callback, BBObject * stream, int flags, int indent, int precision) {
  93. return json_dump_callback(handle, callback, (void *)stream, flags | JSON_INDENT(indent) | JSON_REAL_PRECISION(precision));
  94. }
  95. BBObject * bmx_json_loads(BBString * text, int flags) {
  96. char * t = bbStringToUTF8String(text);
  97. json_error_t error;
  98. json_t * js = json_loads(t, flags, &error);
  99. if (!js) {
  100. int errorCode = json_error_code(&error);
  101. return CB_PREF(text_json_TJSONError__createError)(bbStringFromUTF8String(error.text), bbStringFromUTF8String(error.source),
  102. error.line, error.column, error.position, errorCode);
  103. }
  104. BBObject * ref = CB_PREF(text_json_TJSON__create)(js, json_typeof(js), &bbEmptyString);
  105. return CB_PREF(text_json_TJSONError__createNoError)(ref);
  106. }
  107. BBObject * bmx_json_load_callback(json_load_callback_t callback, BBObject * stream, int flags) {
  108. json_error_t error;
  109. json_t * js = json_load_callback(callback, (void *)stream, flags, &error);
  110. if (!js) {
  111. int errorCode = json_error_code(&error);
  112. return CB_PREF(text_json_TJSONError__createError)(bbStringFromUTF8String(error.text), bbStringFromUTF8String(error.source),
  113. error.line, error.column, error.position, errorCode);
  114. }
  115. BBObject * ref = CB_PREF(text_json_TJSON__create)(js, json_typeof(js), &bbEmptyString);
  116. return CB_PREF(text_json_TJSONError__createNoError)(ref);
  117. }
  118. json_t * bmx_json_integer(BBInt64 v) {
  119. return json_integer(v);
  120. }
  121. void bmx_json_integer_value(json_t * handle, BBInt64 * v) {
  122. *v = json_integer_value(handle);
  123. }
  124. int bmx_json_integer_set(json_t * handle, BBInt64 v) {
  125. return json_integer_set(handle, v);
  126. }
  127. int bmx_json_object_size(json_t * handle) {
  128. return json_object_size(handle);
  129. }
  130. BBObject * bmx_json_object_get(json_t * handle, BBString * key) {
  131. char * k = bbStringToUTF8String(key);
  132. json_t * obj = json_object_get(handle, k);
  133. bbMemFree(k);
  134. if (obj) {
  135. return CB_PREF(text_json_TJSON__create)(json_incref(obj), json_typeof(obj), key);
  136. } else {
  137. return &bbNullObject;
  138. }
  139. }
  140. int bmx_json_object_set_nocheck(json_t * handle, BBString * key, json_t * value) {
  141. char * k = bbStringToUTF8String(key);
  142. int res = json_object_set_nocheck(handle, k, value);
  143. bbMemFree(k);
  144. return res;
  145. }
  146. int bmx_json_object_del(json_t * handle, BBString * key) {
  147. char * k = bbStringToUTF8String(key);
  148. int res = json_object_del(handle, k);
  149. bbMemFree(k);
  150. return res;
  151. }
  152. BBObject * bmx_json_object_iter_value(void * iter) {
  153. const char * key = json_object_iter_key(iter);
  154. json_t * value = json_object_iter_value(json_object_key_to_iter(key));
  155. if (value) {
  156. return CB_PREF(text_json_TJSON__create)(json_incref(value), json_typeof(value), bbStringFromUTF8String(key));
  157. } else {
  158. return &bbNullObject;
  159. }
  160. }
  161. json_t * bmx_json_bool(int v) {
  162. return json_boolean(v);
  163. }