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