Contents

CharlZKP/json-stringify-deterministic-swift

swift implementation of the json-stringify-deterministic js lib

What It Does

Standard JSON serialization doesn't guarantee consistent output across different runs or environments. Object keys may be ordered differently, spacing may vary, and implementation details can cause subtle differences.

This library ensures deterministic JSON output by:

  • Always sorting object keys alphabetically
  • Applying consistent formatting rules
  • Providing stable, reproducible serialization

This makes it ideal for:

  • Generating stable content hashes for caching
  • Creating reliable signatures for API responses
  • Comparing JSON outputs in tests
  • Ensuring consistent logging across distributed systems

Features

  • Deterministic output: Object keys always sorted alphabetically
  • Circular reference handling: Optional support with [Circular] placeholder
  • Custom replacer functions: Transform values during serialization
  • Custom key comparison: Override default alphabetical sorting
  • Pretty printing: Optional indentation with custom spacing

Installation

dependencies: [
    .package(url: "https://github.com/CharlZKP/json-stringify-deterministic-swift", from: "1.0.0")
]

Usage

public func stringify(_ obj: Any, _ opts: Options? = nil) -> String

Example

import JSONStringifyDeterministic

let obj: [String: Any] = ["c": 8, "b": [4, 5], "a": 3]
let result = stringify(obj)
// {"a":3,"b":[4,5],"c":8}

Options

let result = stringify(obj, Options(
    space: "  ",        // Pretty print with 2 spaces
    cycles: true,        // Handle circular references
    replacer: { k, v in v },  // Transform values
    compare: { a, b in .orderedAscending }  // Custom key sort
))
public struct Options {
    public var space: String?
    public var cycles: Bool
    public var compare: ((KeyValue, KeyValue) -> ComparisonResult)?
    public var replacer: ((String, Any) -> Any?)?
}

Testing

swift test

Releasing

To create a release:

git tag 1.0.0
git push origin 1.0.0

Then create a GitHub Release (which automatically uses the tag).

License

MIT - See LICENSE for details.

Original

This is a Swift port of json-stringify-deterministic by Kikobeats.

Package Metadata

Repository: CharlZKP/json-stringify-deterministic-swift

Stars: 0

Forks: 0

Open issues: 0

Default branch: master

Primary language: swift

License: MIT

README: README.md