Skip to main content

Convert JSON data structures to native language literal syntax.

Project description

Build Status PyPI

literalizer

literalizer converts JSON data structures to native language literal syntax (Python, JavaScript, TypeScript, Go, Ruby, C#, C++, Java, Kotlin, Rust, Haskell, Swift, PHP).

Installation

Requires Python 3.12+.

pip install literalizer

Usage examples

"""Examples of using literalizer."""

from literalizer import (
    JAVA,
    JAVASCRIPT,
    PYTHON,
    LanguageSpec,
    format_bytes_hex,
    format_date_iso,
    format_datetime_iso,
    literalize_json,
    literalize_yaml,
)

# Convert a JSON array to Java literal items
result = literalize_json(
    json_string='[true, null, "hi", [1, 2]]',
    language=JAVA,
    prefix="",
    wrap=False,
)
# result:
# true,
# null,
# "hi",
# {1, 2},

# Convert to JavaScript/TypeScript array
result = literalize_json(
    json_string='[true, null, "hi", [1, 2]]',
    language=JAVASCRIPT,
    prefix="    ",
    wrap=True,
)
# result:
# [
#     true,
#     null,
#     "hi",
#     [1, 2],
# ]

# Convert a JSON string to Python
result = literalize_json(
    json_string='[true, null, "hi", [1, 2]]',
    language=PYTHON,
    prefix="",
    wrap=True,
)
# result:
# (
#     True,
#     None,
#     "hi",
#     (1, 2),
# )

# Convert from a YAML string directly
result = literalize_yaml(
    yaml_string="- true\n- null\n- hi\n- [1, 2]",
    language=PYTHON,
    prefix="",
    wrap=True,
)
# result:
# (
#     True,
#     None,
#     "hi",
#     (1, 2),
# )

# Built-in languages: PYTHON, JAVASCRIPT, TYPESCRIPT, GO, RUBY,
#                      CSHARP, CPP, JAVA, KOTLIN, RUST, HASKELL, SWIFT, PHP


# Create a custom language:
def _omap_entry(key: str, value: str) -> str:
    """Format an ordered-map entry."""
    return f"{key}: {value}"


custom = LanguageSpec(
    null_literal="nil",
    true_literal="TRUE",
    false_literal="FALSE",
    collection_open="[",
    collection_close="]",
    dict_separator=": ",
    dict_open="{",
    dict_close="}",
    format_dict_entry=None,
    trailing_comma=True,
    single_element_trailing_comma=False,
    format_bytes=format_bytes_hex,
    format_date=format_date_iso,
    format_datetime=format_datetime_iso,
    empty_collection=None,
    empty_dict=None,
    set_open="[",
    set_close="]",
    empty_set=None,
    format_set_entry=None,
    comment_prefix="//",
    omap_open="{",
    omap_close="}",
    format_omap_entry=_omap_entry,
    multiline_close_indent="",
    skip_null_dict_values=False,
    format_variable_declaration=None,
)

Use cases

  • Generate test fixtures from JSON samples.

  • Convert API responses to language-native data structures for documentation.

  • Create type-safe literal data from JSON config files.

Full documentation

See the full documentation for more information including how to contribute.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

literalizer-2026.3.16.1.tar.gz (65.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

literalizer-2026.3.16.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file literalizer-2026.3.16.1.tar.gz.

File metadata

  • Download URL: literalizer-2026.3.16.1.tar.gz
  • Upload date:
  • Size: 65.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for literalizer-2026.3.16.1.tar.gz
Algorithm Hash digest
SHA256 a4c54a263c6f22a0c147af885ae1506b9142f760870fc3884694306787e2effc
MD5 717f63fe7ce9c7d2b196106318a36554
BLAKE2b-256 4b1adea81d67f24eeead399a8fc32a9be8585d69b5cfcd133f5ea9f9b87d015c

See more details on using hashes here.

Provenance

The following attestation bundles were made for literalizer-2026.3.16.1.tar.gz:

Publisher: release.yml on adamtheturtle/literalizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file literalizer-2026.3.16.1-py3-none-any.whl.

File metadata

File hashes

Hashes for literalizer-2026.3.16.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d53fce180a9f15c834e99a34f30b6d2aed20795b87961f117207927d29de177
MD5 85d6b19100883dbe84cd0bbbac25275e
BLAKE2b-256 826bf4bbe03b0a0f2516f5e8125322c9b144ba8afdb1c92adbf4b4ddb369608f

See more details on using hashes here.

Provenance

The following attestation bundles were made for literalizer-2026.3.16.1-py3-none-any.whl:

Publisher: release.yml on adamtheturtle/literalizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page