Repository navigation
fix(parser): keep JSON numbers and <, >, & as written in JSON items - #21101
Conversation
The JSON parser decoded each array element, object and JSON line with json.Unmarshal, which turns every number into a float64, and wrote the item text back with json.Marshal, which escapes <, > and & for embedding in HTML. An id above 2^53 therefore changed digits (12345678901234567890 became 12345678901234567000, 9007199254740993 became 9007199254740992), 1.50 became 1.5 and 1e5 became 100000, and "a < b && c > d" was indexed as "a \u003c b \u0026\u0026 c \u003e d". Values are now decoded with UseNumber, so each number is written back as it was in the file, and encoded with HTML escaping off. Data after the top-level value is still an error, as with json.Unmarshal.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Summary
JSONParserturns each array element, each single object and each JSON line into an item whosetextis the value re-encoded. It decoded withjson.Unmarshalintoany/map[string]any, which makes every number afloat64, and re-encoded withjson.Marshal, which HTML-escapes<,>and&. The indexed text therefore differed from the file:main{"id": 12345678901234567890}{"id":12345678901234567000}{"id": 9007199254740993}{"id":9007199254740992}"price": 1.50/1e5"price":1.5/100000"note": "a < b && c > d""note":"a < b && c > d"The ids are the serious part. A record whose id is above 2^53 (order numbers, snowflake ids, 64-bit keys) is retrieved with a different id. The Python parser this replaced kept them, since Python ints are not floats.
What changed
Two helpers in
json_parser.goreplace the calls inparseJSONArray,parseSingleJSONObjectandparseJSONLines:unmarshalJSONdecodes withUseNumber(), so each number stays ajson.Numberand is written back exactly as it was. It still rejects data after the top-level value, asjson.Unmarshaldoes, so the shape detection and the per-line fallbacks accept and skip the same inputs as before.marshalJSONencodes withSetEscapeHTML(false).Objects are still written with sorted keys, as before.
Testing
TestJSONParser_KeepsValuesAsWrittenhas one case each for a single object, an array and JSON lines. Withmain'sjson_parser.goall three fail with the values in the table above. The existing JSON parser tests are unchanged and pass.