conformance.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. .. _rfc-conformance:
  2. ***************
  3. RFC Conformance
  4. ***************
  5. JSON is specified in :rfc:`4627`, *"The application/json Media Type
  6. for JavaScript Object Notation (JSON)"*.
  7. Character Encoding
  8. ==================
  9. Jansson only supports UTF-8 encoded JSON texts. It does not support or
  10. auto-detect any of the other encodings mentioned in the RFC, namely
  11. UTF-16LE, UTF-16BE, UTF-32LE or UTF-32BE. Pure ASCII is supported, as
  12. it's a subset of UTF-8.
  13. Strings
  14. =======
  15. JSON strings are mapped to C-style null-terminated character arrays,
  16. and UTF-8 encoding is used internally. Strings may not contain
  17. embedded null characters, not even escaped ones.
  18. For example, trying to decode the following JSON text leads to a parse
  19. error::
  20. ["this string contains the null character: \u0000"]
  21. All other Unicode codepoints U+0001 through U+10FFFF are allowed.
  22. Unicode normalization or any other transformation is never performed
  23. on any strings (string values or object keys). When checking for
  24. equivalence of strings or object keys, the comparison is performed
  25. byte by byte between the original UTF-8 representations of the
  26. strings.
  27. Numbers
  28. =======
  29. .. _real-vs-integer:
  30. Real vs. Integer
  31. ----------------
  32. JSON makes no distinction between real and integer numbers; Jansson
  33. does. Real numbers are mapped to the ``double`` type and integers to
  34. the ``json_int_t`` type, which is a typedef of ``long long`` or
  35. ``long``, depending on whether ``long long`` is supported by your
  36. compiler or not.
  37. A JSON number is considered to be a real number if its lexical
  38. representation includes one of ``e``, ``E``, or ``.``; regardless if
  39. its actual numeric value is a true integer (e.g., all of ``1E6``,
  40. ``3.0``, ``400E-2``, and ``3.14E3`` are mathematical integers, but
  41. will be treated as real values). With the ``JSON_DECODE_INT_AS_REAL``
  42. decoder flag set all numbers are interpreted as real.
  43. All other JSON numbers are considered integers.
  44. When encoding to JSON, real values are always represented
  45. with a fractional part; e.g., the ``double`` value 3.0 will be
  46. represented in JSON as ``3.0``, not ``3``.
  47. Overflow, Underflow & Precision
  48. -------------------------------
  49. Real numbers whose absolute values are too small to be represented in
  50. a C ``double`` will be silently estimated with 0.0. Thus, depending on
  51. platform, JSON numbers very close to zero such as 1E-999 may result in
  52. 0.0.
  53. Real numbers whose absolute values are too large to be represented in
  54. a C ``double`` will result in an overflow error (a JSON decoding
  55. error). Thus, depending on platform, JSON numbers like 1E+999 or
  56. -1E+999 may result in a parsing error.
  57. Likewise, integer numbers whose absolute values are too large to be
  58. represented in the ``json_int_t`` type (see above) will result in an
  59. overflow error (a JSON decoding error). Thus, depending on platform,
  60. JSON numbers like 1000000000000000 may result in parsing error.
  61. Parsing JSON real numbers may result in a loss of precision. As long
  62. as overflow does not occur (i.e. a total loss of precision), the
  63. rounded approximate value is silently used. Thus the JSON number
  64. 1.000000000000000005 may, depending on platform, result in the
  65. ``double`` value 1.0.
  66. Signed zeros
  67. ------------
  68. JSON makes no statement about what a number means; however Javascript
  69. (ECMAscript) does state that +0.0 and -0.0 must be treated as being
  70. distinct values, i.e. -0.0 |not-equal| 0.0. Jansson relies on the
  71. underlying floating point library in the C environment in which it is
  72. compiled. Therefore it is platform-dependent whether 0.0 and -0.0 will
  73. be distinct values. Most platforms that use the IEEE 754
  74. floating-point standard will support signed zeros.
  75. Note that this only applies to floating-point; neither JSON, C, or
  76. IEEE support the concept of signed integer zeros.
  77. .. |not-equal| unicode:: U+2260
  78. Types
  79. -----
  80. No support is provided in Jansson for any C numeric types other than
  81. ``json_int_t`` and ``double``. This excludes things such as unsigned
  82. types, ``long double``, etc. Obviously, shorter types like ``short``,
  83. ``int``, ``long`` (if ``json_int_t`` is ``long long``) and ``float``
  84. are implicitly handled via the ordinary C type coercion rules (subject
  85. to overflow semantics). Also, no support or hooks are provided for any
  86. supplemental "bignum" type add-on packages.