#json #serde-json #nan #infinity #python #deserialize-json #integer

json-forensics

A crate that adds a read adapter to deal with bad Python caused JSON payloads (NaNs and Infinities)

1 unstable release

0.1.1 Mar 30, 2023

#2396 in Parser implementations

Download history 308/week @ 2024-01-01 441/week @ 2024-01-08 554/week @ 2024-01-15 577/week @ 2024-01-22 408/week @ 2024-01-29 494/week @ 2024-02-05 626/week @ 2024-02-12 486/week @ 2024-02-19 430/week @ 2024-02-26 496/week @ 2024-03-04 405/week @ 2024-03-11 626/week @ 2024-03-18 548/week @ 2024-03-25 405/week @ 2024-04-01 452/week @ 2024-04-08 682/week @ 2024-04-15

2,101 downloads per month

BSD-3-Clause

10KB
178 lines

rust-json-forensics

This crate exposes a function that takes a byteslice and:

  • Converts the invalid JSON tokens NaN and Infinity into 0
  • Replaces all integers that would cause an overflow in serde-json with 0

This is just to get the JSON to parse. All operations happen in-place.

This is useful because the Python JSON library traditionally emits invalid JSON if NaN and Infinity values are encountered. If you have to support clients like this, this wrapper can be used to still deserialize such a JSON document.

Successor of python-json-read-adapter


lib.rs:

This crate implements a Read adapter that converts the invalid JSON tokens NaN and Infinity into other tokens without otherwise distorting the stream. It achieves this by converting NaN and Infinity into 0.0.

This is useful because the Python JSON library traditionally emits invalid JSON if NaN and Infinity values are encountered. If you have to support clients like this, this wrapper can be used to still deserialize such a JSON document.

This is just a way to get this to parse and 0 is a value that can be inserted in a standardized way that fits without changing any of the positions.

Example Conversion

The following JSON document:

{"nan":NaN,"inf":Infinity,"-inf":-Infinity}

is thus converted to:

{"nan":0  ,"inf":0       ,"-inf":-0       }

serde support

If the serde feature is enabled then the crate provides some basic wrappers around serde_json to deserialize quickly and also by running the conversions.

Dependencies

~0–270KB