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