pub fn write(w: &mut dyn Write, pp: &Pp, level: usize, v: &Val) -> Result<()>Expand description
Write a value as JSON.
Note that unlike jq, this may actually produce invalid JSON. In particular, this may yield:
- literals for special floating-point values (NaN, Infinity, -Infinity)
- invalid UTF-8 characters
- byte strings with
\xXXsequences - objects with non-string keys
The key principles behind this behaviour are:
- Printing a value should always succeed. (Otherwise, there would exist values that we could not even inspect.)
- Printing a value should yield valid JSON if and only if the value can be represented by an equivalent JSON value. (To give users a chance to find non-JSON values and to take appropriate action.)
jq and jaq agree on principle 1, but disagree on principle 2.
In particular, this shows by the fact that jq -n 'nan' yields null.
That means that jq maps values that cannot be represented by JSON
to different values that can be represented by JSON.
In summary, jq may cause silent information loss, whereas jaq may yield invalid JSON values. Choose your poison.