Skip to main content

Config

Struct Config 

Source
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

FieldDefaultDescription
sort_keyfalseSort object keys alphabetically
preserve_nanfalseEncode NaN as N|0 (vs convert to null)
error_on_nanfalsePanic on NaN (only if preserve_nan is false)
preserve_infinitefalseEncode Infinity as N|+/N|- (vs convert to null)
error_on_infinitefalsePanic 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:

Valuepreserve_* = truepreserve_* = false, error_* = trueBoth false
NaNEncoded as N|0PanicBecomes null
InfinityEncoded as N|+PanicBecomes null
-InfinityEncoded as N|-PanicBecomes 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: bool

Whether 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: bool

Whether 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: bool

Whether 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: bool

Whether 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: bool

Whether 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

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Config

Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Config

Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Config) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Config

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.