Skip to main content

Crate tomljson

Crate tomljson 

Source
Expand description

Lossless TOML ↔ JSON translation.

tomljson translates serde_json::Value to TOML and back, handling the representational gaps where TOML can’t express something JSON can:

  • Null: TOML has no null type. JSON null is encoded as a string placeholder (default "__null__", configurable via TomlJsonOptions). Decoding substitutes the placeholder back to JSON null.
  • Top-level non-table values: TOML documents must be a table at the root. Non-table JSON values (booleans, scalars, arrays) are wrapped under a reserved __root__ key on encode and unwrapped on decode.
  • Integer range: TOML integers are signed 64-bit. Encoding refuses values larger than i64::MAX (per TOML’s spec, not a tomljson limitation).

The motivating use case is JSON Schema 2020-12 documents authored as TOML. tomljson is mdvs-agnostic — anyone moving JSON-shaped data through TOML can use it.

§Quick start

use serde_json::json;

let value = json!({
    "type": "object",
    "properties": {
        "name": { "type": "string" }
    }
});

let toml_str = tomljson::to_string(&value).unwrap();
let back = tomljson::from_str(&toml_str).unwrap();
assert_eq!(back, value);

Structs§

TomlJsonOptions
Options for encode and decode operations.

Enums§

Error
Errors produced by tomljson’s encode and decode paths.

Constants§

DEFAULT_NULL_PLACEHOLDER
Default placeholder string for JSON null values.
DEFAULT_ROOT_PLACEHOLDER
Default key used to wrap top-level non-table JSON values (booleans, scalars, arrays) in TOML output, since TOML documents must have a table at the root.

Functions§

from_str
Decode a TOML string to a JSON value using default options.
from_str_with_options
Decode a TOML string to a JSON value.
to_string
Encode a JSON value to TOML using default options.
to_string_with_options
Encode a JSON value to a TOML string.

Type Aliases§

Result
Convenience alias for Result<T, tomljson::Error>.