common.bmx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ' Copyright (c) 2014-2020 Bruce A Henderson
  2. '
  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. '
  10. ' The above copyright notice and this permission notice shall be included in
  11. ' all copies or substantial portions of the Software.
  12. '
  13. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. ' THE SOFTWARE.
  20. '
  21. SuperStrict
  22. Import BRL.Stream
  23. Import Text.Jansson
  24. Import "source.bmx"
  25. Extern
  26. Function json_array:Byte Ptr()
  27. Function bmx_json_decref(handle:Byte Ptr)
  28. Function bmx_json_string_nocheck:Byte Ptr(Text:String)
  29. Function bmx_json_string_value:String(handle:Byte Ptr)
  30. Function bmx_json_array_get:Object(handle:Byte Ptr, index:Int)
  31. Function json_array_clear:Int(handle:Byte Ptr)
  32. Function json_array_remove:Int(handle:Byte Ptr, index:Int)
  33. Function bmx_json_array_size:Int(handle:Byte Ptr)
  34. Function bmx_json_array_set:Int(handle:Byte Ptr, index:Int, value:Byte Ptr)
  35. Function bmx_json_array_append:Int(handle:Byte Ptr, value:Byte Ptr)
  36. Function bmx_json_array_insert:Int(handle:Byte Ptr, index:Int, value:Byte Ptr)
  37. Function bmx_json_dumps:String(handle:Byte Ptr, flags:Int, indent:Int, precision:Int)
  38. ?bmxng
  39. Function bmx_json_dump_callback:Int(handle:Byte Ptr, callback:Size_T(buffer:Byte Ptr, size:Size_T, data:TStream), stream:TStream, flags:Int, indent:Int, precision:Int)
  40. Function bmx_json_load_callback:Object(callback:Size_T(buffer:Byte Ptr, size:Size_T, data:TStream), Text:TStream, flags:Int)
  41. ?Not bmxng
  42. Function bmx_json_dump_callback:Int(handle:Byte Ptr, callback:Int(buffer:Byte Ptr, size:Int, data:TStream), stream:TStream, flags:Int, indent:Int, precision:Int)
  43. Function bmx_json_load_callback:Object(callback:Int(buffer:Byte Ptr, size:Int, data:TStream), Text:TStream, flags:Int)
  44. ?
  45. Function bmx_json_loads:Object(Text:String, flags:Int)
  46. Function bmx_json_integer:Byte Ptr(v:Long)
  47. Function bmx_json_integer_value(handle:Byte Ptr, v:Long Ptr)
  48. Function bmx_json_integer_set:Int(handle:Byte Ptr, v:Long)
  49. Function json_real:Byte Ptr(v:Double)
  50. Function json_real_value:Double(handle:Byte Ptr)
  51. Function json_real_set:Int(handle:Byte Ptr, v:Double)
  52. Function json_object:Byte Ptr()
  53. Function bmx_json_object_size:Int(handle:Byte Ptr)
  54. Function bmx_json_object_get:Object(handle:Byte Ptr, key:String)
  55. Function bmx_json_object_set_nocheck:Int(handle:Byte Ptr, key:String, value:Byte Ptr)
  56. Function bmx_json_object_del:Int(handle:Byte Ptr, key:String)
  57. Function json_object_clear:Int(handle:Byte Ptr)
  58. Function json_object_update:Int(handle:Byte Ptr, other:Byte Ptr)
  59. Function json_object_update_existing:Int(handle:Byte Ptr, other:Byte Ptr)
  60. Function json_object_update_missing:Int(handle:Byte Ptr, other:Byte Ptr)
  61. Function json_object_iter:Byte Ptr(handle:Byte Ptr)
  62. Function json_object_iter_next:Byte Ptr(handle:Byte Ptr, iter:Byte Ptr)
  63. Function bmx_json_object_iter_value:Object(iter:Byte Ptr)
  64. Function bmx_json_bool:Byte Ptr(v:Int)
  65. End Extern
  66. Const JSON_TYPE_OBJECT:Int = 0
  67. Const JSON_TYPE_ARRAY:Int = 1
  68. Const JSON_TYPE_STRING:Int = 2
  69. Const JSON_TYPE_INTEGER:Int = 3
  70. Const JSON_TYPE_REAL:Int = 4
  71. Const JSON_TYPE_TRUE:Int = 5
  72. Const JSON_TYPE_FALSE:Int = 6
  73. Const JSON_TYPE_NULL:Int = 7
  74. Const JSON_MAX_INDENT:Int = $1F
  75. Rem
  76. bbdoc: Enables a compact representation, i.e. sets the separator between array and object items to "," and between object keys and values to ":".
  77. about: Without this flag, the corresponding separators are ", " and ": " for more readable output.
  78. End Rem
  79. Const JSON_COMPACT:Int = $20
  80. Rem
  81. bbdoc: Guarantees the output to consist only of ASCII characters.
  82. about: This is achived by escaping all Unicode characters outside the ASCII range.
  83. End Rem
  84. Const JSON_ENSURE_ASCII:Int = $40
  85. Rem
  86. bbdoc: All the objects in output are sorted by key.
  87. about: This is useful e.g. if two JSON texts are diffed or visually compared.
  88. End Rem
  89. Const JSON_SORT_KEYS:Int = $80
  90. Rem
  91. bbdoc: Object keys in the output are sorted into the same order in which they were first inserted to the object.
  92. about: For example, decoding a JSON text and then encoding with this flag preserves the order of object keys.
  93. End Rem
  94. Const JSON_PRESERVE_ORDER:Int = $100
  95. Rem
  96. bbdoc: Makes it possible to encode any JSON value on its own.
  97. about: Without it, only objects and arrays can be passed as the root value to the encoding functions.
  98. Note: Encoding any value may be useful in some scenarios, but it's generally discouraged as it violates strict compatiblity with
  99. RFC 4627. If you use this flag, don't expect interoperatibility with other JSON systems.
  100. End Rem
  101. Const JSON_ENCODE_ANY:Int = $200
  102. Rem
  103. bbdoc: Escapes the / characters in strings with \/.
  104. End Rem
  105. Const JSON_ESCAPE_SLASH:Int = $400
  106. Rem
  107. bbdoc: The opening and closing characters of the top-level array ('[', ']') or object ('{', '}') are omitted during encoding.
  108. about: This flag is useful when concatenating multiple arrays or objects into a stream.
  109. End Rem
  110. Const JSON_EMBED:Int = $10000
  111. Rem
  112. bbdoc: Precision becomes the number of fraction digits.
  113. End Rem
  114. Const JSON_FRACTIONAL_DIGITS:Int = $20000
  115. Rem
  116. bbdoc: Pretty-prints the result, using newlines between array and object items, and indenting with n spaces.
  117. about: The valid range for n is between 0 and 31 (inclusive), other values result in an undefined output. If JSON_INDENT is not used or n is 0,
  118. no newlines are inserted between array and object items.
  119. End Rem
  120. Function JSON_INDENT:Int(n:Int)
  121. Return n & JSON_MAX_INDENT
  122. End Function
  123. Rem
  124. bbdoc: Outputs all real numbers with at most n digits of precision.
  125. about: The valid range for n is between 0 and 31 (inclusive), and other values result in an undefined behavior.
  126. By default, the precision is 17, to correctly and losslessly encode all IEEE 754 double precision floating point numbers.
  127. End Rem
  128. Function JSON_REAL_PRECISION:Int(n:Int)
  129. Return (n & $1F) Shl 11
  130. End Function
  131. Rem
  132. bbdoc: Issues a decoding error if any JSON object in the input text contains duplicate keys.
  133. about: Without this flag, the value of the last occurence of each key ends up in the result.
  134. Key equivalence is checked byte-by-byte, without special Unicode comparison algorithms.
  135. End Rem
  136. Const JSON_REJECT_DUPLICATES:Int = $1
  137. Rem
  138. bbdoc: With this flag enabled, the decoder stops after decoding a valid JSON array or object, and thus allows extra data after the JSON text.
  139. about: By default, the decoder expects that its whole input constitutes a valid JSON text, and issues an error if there's extra data after the otherwise valid JSON input.
  140. Normally, reading will stop when the last ] or } in the JSON input is encountered. If both JSON_DISABLE_EOF_CHECK and JSON_DECODE_ANY flags
  141. are used, the decoder may read one extra UTF-8 code unit (up to 4 bytes of input). For example, decoding 4true correctly decodes
  142. the integer 4, but also reads the t. For this reason, if reading multiple consecutive values that are not arrays or objects,
  143. they should be separated by at least one whitespace character.
  144. End Rem
  145. Const JSON_DISABLE_EOF_CHECK:Int = $2
  146. Rem
  147. bbdoc: With this flag enabled, the decoder accepts any valid JSON value.
  148. about: By default, the decoder expects an array or object as the input.
  149. Note: Decoding any value may be useful in some scenarios, but it's generally discouraged as it violates strict compatiblity
  150. with RFC 4627. If you use this flag, don't expect interoperatibility with other JSON systems.
  151. End Rem
  152. Const JSON_DECODE_ANY:Int = $4
  153. Rem
  154. bbdoc: With this flag enabled the decoder interprets all numbers as real values.
  155. about: JSON defines only one number type. Jansson distinguishes between ints and reals. For more information see Real vs. Integer. Integers that do
  156. not have an exact double representation will silently result in a loss of precision. Integers that cause a double overflow will cause an error.
  157. End Rem
  158. Const JSON_DECODE_INT_AS_REAL:Int = $8
  159. Rem
  160. bbdoc: Allows \u0000 escape inside string values.
  161. about: This is a safety measure; If you know your input can contain NUL bytes, use this flag. If you don't use this flag, you don't have
  162. to worry about NUL bytes inside strings.
  163. Object keys cannot have embedded NUL bytes even if this flag is used.
  164. End Rem
  165. Const JSON_ALLOW_NUL:Int = $10
  166. Const JSON_ERROR_UNKNOWN:Int = 0
  167. Const JSON_ERROR_OUT_OF_MEMORY:Int = 1
  168. Const JSON_ERROR_STACK_OVERFLOW:Int = 2
  169. Const JSON_ERROR_CANNOT_OPEN_FILE:Int = 3
  170. Const JSON_ERROR_INVALID_ARGUMENT:Int = 4
  171. Const JSON_ERROR_INVALID_UTF8:Int = 5
  172. Const JSON_ERROR_PREMATURE_END_OF_INPUT:Int = 6
  173. Const JSON_ERROR_END_OF_INPUT_EXPECTED:Int = 7
  174. Const JSON_ERROR_INVALID_SYNTAX:Int = 8
  175. Const JSON_ERROR_INVALID_FORMAT:Int = 9
  176. Const JSON_ERROR_WRONG_TYPE:Int = 10
  177. Const JSON_ERROR_NULL_CHARACTER:Int = 11
  178. Const JSON_ERROR_NULL_VALUE:Int = 12
  179. Const JSON_ERROR_NULL_BYTE_IN_KEY:Int = 13
  180. Const JSON_ERROR_DUPLICATE_KEY:Int = 14
  181. Const JSON_ERROR_NUMERIC_OVERFLOW:Int = 15
  182. Const JSON_ERROR_ITEM_NOT_FOUND:Int = 16
  183. Const JSON_ERROR_INDEX_OUT_OF_RANGE:Int = 17