Enforcing RFC‑Strict Numeric Parsing in Production JSON Configurations
0 reputation · 26 May 2020, 00:03 UTC
0 reputation · 26 May 2020, 00:03 UTC
A configuration file that includes numeric literals with leading zeros parses successfully when processed by a lenient JSON parser used locally, but throws a parsing error in production where the parser enforces RFC-8259 compliance.
The unresolved decision concerns whether the production JSON library should default to strict numeric parsing, rejecting leading zeros, or retain permissive behavior for backward compatibility, which may require adjustments to serialization code that generates numbers with unnecessary leading zeros.
What criteria should be used to determine the appropriate default parsing mode for production environments? How can we verify that serialized configuration files contain no leading zeros without manual inspection? Which testing approach guarantees identical parsing outcomes across lenient and strict modes?
35775 reputation · 26 May 2020, 06:40 UTC
Set the production JSON parser to reject leading zeros unless you have confirmed that legacy configuration files cannot be rewritten and every downstream consumer tolerates non-canonical numbers. Strict parsing is the safer default because RFC 8259 permits only the literal 0 to begin with a zero; 01, 007, or 01.5 are invalid.
08 as eight and another rejects it.| Item | Status | Detail |
|---|---|---|
| RFC 8259 numeric grammar | Confirmed | Leading zeros are prohibited except for the number 0. |
Lenient local parse of 01 | Likely | The local parser accepts the token and coerces it to 1; production rejects it. |
| Serialized leading zeros | Likely | Configuration serialization code is padding integers or emitting decimal strings with unnecessary zeros. |
Use a script that walks the serialized text and flags numeric tokens matching a leading-zero pattern. Do not apply a global regex to the whole file, because timestamps, version strings, and quoted keys may contain zeros that are not JSON numbers.
# Illustrative Python; adapt to your language and JSON tokenizer.
import re
with open("config.json") as f:
text = f.read()
# Flag unquoted numeric tokens that begin with an unnecessary zero.
for match in re.finditer(r"(?<=[:,\[\{]\s*)-?0[0-9]", text):
print("Potential leading-zero number at", match.start())
A cleaner approach is to parse with the strict parser and compare its output against a round-trip serialization; any token the strict parser rejects is the violation.
Load the same fixture file with two parser instances—one lenient, one strict—and assert that the resulting object graphs are equal. Then serialize the parsed object back to JSON and assert that no numeric token contains a leading zero. This catches cases where lenient mode silently accepts 01 while strict mode rejects it, and where serialization reintroduces padding.
Missing diagnostic detail: Which JSON library and version runs in production? That determines the exact strict-mode API or flag. Share it if you need the precise configuration line.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.