pub struct Config {
pub sort_key: bool,
pub preserve_nan: bool,
pub error_on_nan: bool,
pub preserve_infinite: bool,
pub error_on_infinite: bool,
}Expand description
Global configuration for compression behavior.
This struct defines options that control how JSON values are processed
during compression. The library uses a compile-time constant CONFIG
with these settings.
§Fields
| Field | Default | Description |
|---|---|---|
sort_key | false | Sort object keys alphabetically |
preserve_nan | false | Encode NaN as N|0 (vs convert to null) |
error_on_nan | false | Panic on NaN (only if preserve_nan is false) |
preserve_infinite | false | Encode Infinity as N|+/N|- (vs convert to null) |
error_on_infinite | false | Panic on Infinity (only if preserve_infinite is false) |
§Key Sorting
When sort_key is true, object keys are sorted alphabetically before
compression. This ensures consistent output regardless of insertion order,
which is useful for:
- Deterministic compression output
- Easier diff comparison
- Consistent hashing of compressed data
§Special Number Handling (v3.4.0+)
JSON doesn’t support NaN or Infinity. The handling depends on config:
| Value | preserve_* = true | preserve_* = false, error_* = true | Both false |
|---|---|---|---|
| NaN | Encoded as N|0 | Panic | Becomes null |
| Infinity | Encoded as N|+ | Panic | Becomes null |
| -Infinity | Encoded as N|- | Panic | Becomes null |
Note: error_on_nan and error_on_infinite only take effect when
their corresponding preserve_* option is false.
§Example
use compress_json_rs::{Config, CONFIG};
// View the default configuration
assert_eq!(CONFIG.sort_key, false);
assert_eq!(CONFIG.preserve_nan, false);
assert_eq!(CONFIG.error_on_nan, false);
assert_eq!(CONFIG.preserve_infinite, false);
assert_eq!(CONFIG.error_on_infinite, false);Fields§
§sort_key: boolWhether to sort object keys alphabetically.
When true, object keys are sorted before compression, ensuring
consistent output regardless of key insertion order.
Default: false
preserve_nan: boolWhether to preserve NaN values with special encoding.
When true, NaN values are encoded as N|0 for cross-platform
compatibility with JavaScript/Python implementations.
When false, NaN is converted to null (like JSON.stringify).
Default: false
Added in: v3.4.0
error_on_nan: boolWhether to panic when encountering NaN values.
Only effective when preserve_nan is false.
When true, the library will panic if a NaN is encountered.
When false, NaN is silently converted to null.
Default: false
preserve_infinite: boolWhether to preserve infinite values with special encoding.
When true, Infinity and -Infinity are encoded as N|+ and N|-
for cross-platform compatibility with JavaScript/Python implementations.
When false, infinite values are converted to null (like JSON.stringify).
Default: false
Added in: v3.4.0
error_on_infinite: boolWhether to panic when encountering infinite values.
Only effective when preserve_infinite is false.
When true, the library will panic if Infinity or -Infinity is encountered.
When false, infinite values are silently converted to null.
Default: false