From 0618e61238b48430f9888ffe1fd80efe87dc2098 Mon Sep 17 00:00:00 2001 From: Thorsten Fuchs <76904008+ThorFuchs@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:26:52 +0100 Subject: [PATCH] Add JSONCanonicalWriter for RFC 8785 canonical JSON JSONCanonicalWriter writes JSON Canonicalization Scheme bytes: compact output, keys ordered by UTF-16 code units, short string escapes and the ECMAScript spelling of binary64 numbers. It shares the strict writer cursor and the invocation-owned work meter. NumberPolicy.preserving (default) refuses a number whose binary64 spelling would denote a different exact decimal; .rounding rounds to the nearest binary64 as RFC 8785 specifies. Scratch binary conversion text is no longer charged as output bytes on this path; existing writers are unchanged. Tests cover the RFC 8785 Appendix B vectors, the section 3.2.2 sample and the section 3.2.3 key-order example, plus policy, limits and cancellation. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 12 ++ Documentation/Architecture/API.md | 9 ++ .../Qualification/TestDiscovery.json | 6 +- Sources/JSON/Codec/JSONCanonicalWriter.swift | 80 ++++++++++ Sources/JSON/Codec/JSONWriter.swift | 36 ++++- Sources/JSON/Documentation.docc/JSON.md | 6 +- Sources/JSON/Number/JSONNumberCanonical.swift | 75 +++++++++ .../JSON/Number/JSONNumberConversions.swift | 9 +- Sources/JSONCLIEngine/CLIBuildIdentity.swift | 2 +- Tests/JSONTests/CanonicalizationTests.swift | 151 ++++++++++++++++++ 10 files changed, 372 insertions(+), 14 deletions(-) create mode 100644 Sources/JSON/Codec/JSONCanonicalWriter.swift create mode 100644 Sources/JSON/Number/JSONNumberCanonical.swift create mode 100644 Tests/JSONTests/CanonicalizationTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 00588a9..82b53ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## Unreleased — 1.10.0 exact interchange (in progress) + +- Add `JSONCanonicalWriter`, which writes RFC 8785 JSON Canonicalization Scheme + (JCS) bytes: compact output, keys ordered by UTF-16 code units, short string + escapes and ECMAScript spelling of binary64 numbers. Equal values produce equal + bytes for hashing, signing and content addressing. `NumberPolicy.preserving` + (the default) refuses a number whose binary64 spelling would denote a different + exact decimal; `.rounding` rounds to the nearest binary64 value as RFC 8785 does. + Tests cover the RFC's Appendix B number vectors, its sample document and its + key-sorting example. The writer does not hash or sign. The existing writer and + its scalar key order are unchanged. + ## Unreleased — 1.9.1 hygiene - Add the `platform-floor` native CI lane. It checks the macOS 15 / iOS 18 diff --git a/Documentation/Architecture/API.md b/Documentation/Architecture/API.md index 9d31f41..a389790 100644 --- a/Documentation/Architecture/API.md +++ b/Documentation/Architecture/API.md @@ -116,6 +116,8 @@ or shape-inferencing result builders. See [numbers and Unicode](NumbersAndUnicod | `JSONWriter.write(_:)` | Owned Data containing complete strict UTF-8 JSON | | `JSONWriter.writeString(_:)` | Owned String with its additional retention charged | | `JSONWriter.writeWithReport(_:)` | JSONOperationReport<Data> | +| `JSONCanonicalWriter.write(_:)` | Owned Data containing RFC 8785 canonical UTF-8 JSON | +| `JSONCanonicalWriter.writeWithReport(_:)` | JSONOperationReport<Data> | Parsing accepts exactly one root followed by JSON whitespace and EOF. It rejects BOM, malformed UTF-8, unpaired surrogate escapes, raw controls, malformed numbers, @@ -127,6 +129,13 @@ or scalar key order, and normalized or preserved number text. Preserved lexemes were admitted with their grammar and identity. Sorting makes no cryptographic canonicalization claim. +JSONCanonicalWriter writes RFC 8785 JSON Canonicalization Scheme bytes: compact, +keys ordered by UTF-16 code units, short string escapes and ECMAScript binary64 +numbers. Its default `.preserving` number policy refuses, with an inexact-conversion +failure, a number whose binary64 spelling would denote a different exact decimal; +`.rounding` follows RFC 8785 and rounds. It produces bytes only, with no hashing, +signing or exported cryptography API. + JSONDocument owns optional original bytes and bounded value/member-key spans. Spans are half-open zero-based byte ranges; diagnostic line/UTF-8 byte column and physical record coordinates are one-based when present. Typed source paths keep diff --git a/Documentation/Qualification/TestDiscovery.json b/Documentation/Qualification/TestDiscovery.json index 43bed89..b6edb38 100644 --- a/Documentation/Qualification/TestDiscovery.json +++ b/Documentation/Qualification/TestDiscovery.json @@ -1,7 +1,7 @@ { "schema_version": 2, "root_functions": { - "JSONTests": 190, + "JSONTests": 198, "JSONSchemaTests": 79, "JSONRegexRuntimeTests": 16, "JSONStreamingTests": 36, @@ -73,7 +73,7 @@ "JSONSchemaDSL" ], "portable_root_functions": { - "JSONTests": 190, + "JSONTests": 198, "JSONSchemaTests": 79, "JSONRegexRuntimeTests": 16, "JSONStreamingTests": 36, @@ -89,7 +89,7 @@ "JSONDSLTests": 47 }, "wasi_root_functions": { - "JSONTests": 190, + "JSONTests": 198, "JSONSchemaTests": 79, "JSONRegexRuntimeTests": 16, "JSONStreamingTests": 36, diff --git a/Sources/JSON/Codec/JSONCanonicalWriter.swift b/Sources/JSON/Codec/JSONCanonicalWriter.swift new file mode 100644 index 0000000..dee77bb --- /dev/null +++ b/Sources/JSON/Codec/JSONCanonicalWriter.swift @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: Apache-2.0 +// Original implementation of RFC 8785, JSON Canonicalization Scheme (JCS). +import Foundation + +/// A writer for RFC 8785 canonical JSON, the JSON Canonicalization Scheme (JCS). +/// +/// The output is compact UTF-8 with object members ordered by the UTF-16 code +/// units of their keys, strings using the short escapes `\b`, `\t`, `\n`, `\f`, +/// `\r`, `\"` and `\\` (and `\u00xx` for other control characters), and numbers +/// spelled as ECMAScript formats binary64 values. Equal values produce equal +/// bytes, so the result can be hashed, signed or used as a content address. +/// +/// RFC 8785 treats every number as an IEEE 754 binary64 value. A ``JSONNumber`` +/// is an exact decimal, so the ``NumberPolicy`` decides what happens when the +/// binary64 spelling would denote a different value. The default, +/// ``NumberPolicy/preserving``, refuses such a number instead of changing it. +/// +/// This type produces bytes only. It performs no hashing or signing, and its key +/// order differs from ``JSONWritingOptions/KeyOrder/scalar`` for keys that mix +/// supplementary-plane characters with characters from U+E000 through U+FFFF. +public struct JSONCanonicalWriter: Sendable { + /// How a number that binary64 cannot represent exactly is handled. + public enum NumberPolicy: Sendable { + /// Refuse a number whose canonical binary64 spelling would denote a + /// different exact decimal, with an inexact-conversion failure. + /// + /// A short decimal such as `0.1` is accepted, because its canonical + /// spelling `0.1` denotes the same exact value. `9007199254740993` and + /// `0.10000000000000001` are refused. + case preserving + /// Round to the nearest binary64 value before formatting, as RFC 8785 + /// does for I-JSON input. Different exact decimals can then produce the + /// same bytes. Values outside the finite binary64 range still fail. + case rounding + } + /// Finite capacities and work quotas applied to each call. + public let limits: JSONLimits + /// The treatment of numbers that binary64 cannot represent exactly. + public let numberPolicy: NumberPolicy + /// Creates an immutable canonical writer. Limits are checked when writing begins. + /// + /// - Parameters: + /// - limits: Finite inclusive capacities and work quotas for each call. + /// - numberPolicy: Refuse (the default) or round numbers that binary64 cannot represent exactly. + public init(limits: JSONLimits = .balanced, numberPolicy: NumberPolicy = .preserving) { + self.limits = limits; self.numberPolicy = numberPolicy + } + /// Writes the RFC 8785 canonical bytes of a complete value. + /// + /// - Returns: Owned canonical UTF-8 bytes. + /// - Throws: Inexact conversion for a number the policy refuses or for a value + /// outside the finite binary64 range; resource failure or cancellation. + /// + /// - Parameters: + /// - value: The exact root to admit and canonicalize. + public func write(_ value: JSON) throws -> Data { try writeWithReport(value).result } + /// Writes RFC 8785 canonical bytes with successful operation accounting. + /// + /// - Returns: Owned canonical bytes and counts; output bytes are charged before growth. + /// - Throws: Inexact conversion for a number the policy refuses or for a value + /// outside the finite binary64 range; resource failure or cancellation. + /// + /// - Parameters: + /// - value: The exact root to admit and canonicalize. + public func writeWithReport(_ value: JSON) throws -> JSONOperationReport { + var meter = try JSONWorkMeter(limits: limits, domain: .writing) + let bytes = try write(value, meter: &meter) + for _ in bytes { try meter.work(); try meter.retain(1) } + return .init(result: Data(bytes), statistics: meter.statistics()) + } + package func write(_ value: JSON, meter: inout JSONWorkMeter) throws -> [UInt8] { + try JSONAdmission.value(value, meter: &meter) + var cursor = JSONWritingCursor(value: value, limits: limits, canonical: numberPolicy) + var bytes: [UInt8] = [] + while let byte = try cursor.next(meter: &meter) { + try meter.charge(.outputBytes); try meter.retain(1); bytes.append(byte) + } + return bytes + } +} diff --git a/Sources/JSON/Codec/JSONWriter.swift b/Sources/JSON/Codec/JSONWriter.swift index 7ae458c..87d27ff 100644 --- a/Sources/JSON/Codec/JSONWriter.swift +++ b/Sources/JSON/Codec/JSONWriter.swift @@ -78,9 +78,15 @@ package struct JSONWritingCursor { } private var stack: [Step] private let options: JSONWritingOptions + /// RFC 8785 mode: UTF-16 key order, short escapes and ECMAScript numbers. + private let canonical: JSONCanonicalWriter.NumberPolicy? private var initialized = false private var beginsValue = false - package init(value: JSON, options: JSONWritingOptions) { stack = [.value(value, 0)]; self.options = options } + package init(value: JSON, options: JSONWritingOptions) { stack = [.value(value, 0)]; self.options = options; canonical = nil } + package init(value: JSON, limits: JSONLimits, canonical policy: JSONCanonicalWriter.NumberPolicy) { + stack = [.value(value, 0)]; options = .init(limits: limits); canonical = policy + } + private var sortsKeys: Bool { canonical != nil || options.keyOrder == .scalar } private mutating func push(_ step: Step, meter: inout JSONWorkMeter) throws { try meter.retain(JSONLogicalWeights.frame); stack.append(step) } @@ -103,8 +109,21 @@ package struct JSONWritingCursor { if x == nil { return false } } } + /// RFC 8785 section 3.2.3 orders keys by their UTF-16 code units. + private func utf16Less(_ a: JSONMember, _ b: JSONMember, meter: inout JSONWorkMeter) throws -> Bool { + var l = a.key.string.utf16.makeIterator(), r = b.key.string.utf16.makeIterator() + while true { + try meter.work() + let x = l.next(), y = r.next() + if x != y { if let x, let y { return x < y }; return x == nil } + if x == nil { return false } + } + } + private func less(_ a: JSONMember, _ b: JSONMember, meter: inout JSONWorkMeter) throws -> Bool { + canonical == nil ? try scalarLess(a, b, meter: &meter) : try utf16Less(a, b, meter: &meter) + } private func ordered(_ object: JSONObject, meter: inout JSONWorkMeter) throws -> [JSONMember] { - if options.keyOrder == .insertion { return object.members } + if !sortsKeys { return object.members } var values: [JSONMember] = [], scratch: [JSONMember] = [] for member in object { try meter.work(); try meter.retain(JSONLogicalWeights.member * 2) @@ -118,7 +137,7 @@ package struct JSONWritingCursor { var left = start, right = middle for target in start..> 4)], kind), meter: &meter) @@ -171,7 +194,10 @@ package struct JSONWritingCursor { case .boolean(let boolean): try push(.text((boolean ? "true" : "false").utf8.makeIterator(), .boolean), meter: &meter) case .string(let text): try quoted(text, meter: &meter) case .number(let number): - if options.numberStyle == .preserved, let lexeme = number.originalLexeme { try push(.text(lexeme.utf8.makeIterator(), .number), meter: &meter) } + if let policy = canonical { + let text = try number.canonicalText(policy: policy, meter: &meter) + try push(.text(text.utf8.makeIterator(), .number), meter: &meter) + } else if options.numberStyle == .preserved, let lexeme = number.originalLexeme { try push(.text(lexeme.utf8.makeIterator(), .number), meter: &meter) } else { let c = number.components if c.exponent != 0 { try push(.text(String(c.exponent).utf8.makeIterator(), .number), meter: &meter); try push(.byte(101, .number), meter: &meter) } @@ -199,7 +225,7 @@ package struct JSONWritingCursor { if index > 0 { try push(.byte(44), meter: &meter) } } try push(.byte(123), meter: &meter) - if options.keyOrder == .scalar { try meter.release(UInt64(members.count) * JSONLogicalWeights.member) } + if sortsKeys { try meter.release(UInt64(members.count) * JSONLogicalWeights.member) } } } } diff --git a/Sources/JSON/Documentation.docc/JSON.md b/Sources/JSON/Documentation.docc/JSON.md index 249447b..f11b993 100644 --- a/Sources/JSON/Documentation.docc/JSON.md +++ b/Sources/JSON/Documentation.docc/JSON.md @@ -12,7 +12,10 @@ and uses order-independent equality. A duplicate decoded key is a syntax error. JSONStreaming. Parsing requires one complete root, trailing whitespace and EOF. The writer supports compact/pretty output, insertion/scalar key order and preserved/normalized decimal spelling. Scalar sorting is deterministic JSON -output, not a cryptographic canonicalization standard. +output, not a cryptographic canonicalization standard. For that, use +``JSONCanonicalWriter``, which writes RFC 8785 (JCS) canonical bytes: UTF-16 key +order, short string escapes and ECMAScript binary64 numbers. By default it refuses +a number whose binary64 spelling would change its exact value. ``JSONValueEncoder`` and ``JSONValueDecoder`` implement native value-based Codable. Direct JSON/JSONObject round-trips preserve raw scalar keys and numbers. @@ -71,6 +74,7 @@ belongs exclusively to each invocation. - ``JSONParser`` - ``JSONWriter`` - ``JSONWritingOptions`` +- ``JSONCanonicalWriter`` - ``JSONIndentationUnit`` - ``JSONValueEncoder`` - ``JSONValueDecoder`` diff --git a/Sources/JSON/Number/JSONNumberCanonical.swift b/Sources/JSON/Number/JSONNumberCanonical.swift new file mode 100644 index 0000000..d9121d2 --- /dev/null +++ b/Sources/JSON/Number/JSONNumberCanonical.swift @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 +// Original implementation of the RFC 8785 section 3.2.2.3 number serialization, +// which is ECMAScript Number::toString applied to a binary64 value. + +extension JSONNumber { + /// The ECMAScript spelling of this number's binary64 value. + /// + /// Swift's `description` of a finite Double supplies the shortest digits that + /// round-trip, choosing the closest such digits, which is the digit string + /// ECMAScript also requires. Only the layout around those digits is rebuilt here. + package func canonicalText(policy: JSONCanonicalWriter.NumberPolicy, meter: inout JSONWorkMeter) throws -> String { + try meter.work(subsystem: .numericWorkUnits) + if isZero { return "0" } + let value = try doubleValue(policy: .rounded, meter: &meter, chargesOutput: false) + // A finite Double's description is at most 24 ASCII bytes; reserve a fixed + // bound around the synchronous foreign rendering. + let foreign: UInt64 = 32 + try meter.retain(foreign); try meter.checkpoint() + let shortest = value.magnitude.description + try meter.checkpoint() + var digits: [UInt8] = [], point: Int? = nil, exponent = 0, exponentNegative = false, inExponent = false + try meter.retain(foreign) + for byte in shortest.utf8 { + try meter.work(subsystem: .numericWorkUnits) + switch byte { + case 48...57 where inExponent: exponent = exponent * 10 + Int(byte - 48) + case 48...57: digits.append(byte - 48) + case 46: point = digits.count + case 101, 69: inExponent = true + case 45: exponentNegative = true + case 43: break + default: throw JSONFailure(.conversion, code: .inexactConversion) + } + } + var position = (point ?? digits.count) + (exponentNegative ? -exponent : exponent) + while digits.count > 1 && digits[0] == 0 { try meter.work(subsystem: .numericWorkUnits); digits.removeFirst(); position -= 1 } + while digits.count > 1 && digits[digits.count - 1] == 0 { try meter.work(subsystem: .numericWorkUnits); digits.removeLast() } + var output: [UInt8] = [] + func append(_ byte: UInt8, meter: inout JSONWorkMeter) throws { + try meter.work(subsystem: .numericWorkUnits); try meter.retain(1); output.append(byte) + } + func appendDigits(_ range: Range, meter: inout JSONWorkMeter) throws { + for index in range { try append(digits[index] + 48, meter: &meter) } + } + if digits == [0] { + try append(48, meter: &meter) + } else { + if value < 0 { try append(45, meter: &meter) } + let k = digits.count, n = position + if k <= n && n <= 21 { + try appendDigits(0.. 1 { try append(46, meter: &meter); try appendDigits(1.. String { + /// Canonical writing passes `chargesOutput: false`: this text is scratch, not output. + package func binaryConversionText(meter: inout JSONWorkMeter, chargesOutput: Bool = true) throws -> String { let c = components let kept = min(800,c.digits.count), shortened = c.digits.count > kept let coefficientCount = kept + (shortened ? 1 : 0) @@ -101,7 +102,7 @@ extension JSONNumber { guard !overflow else { throw JSONFailure(.resource,code: .counterOverflow) } var output: [UInt8] = [] func append(_ byte: UInt8,meter: inout JSONWorkMeter) throws { - try meter.work(subsystem: .numericWorkUnits); try meter.charge(.outputBytes) + try meter.work(subsystem: .numericWorkUnits); if chargesOutput { try meter.charge(.outputBytes) } try meter.retain(1); output.append(byte) } if c.negative { try append(45,meter: &meter) } @@ -149,8 +150,8 @@ extension JSONNumber { var meter = try JSONWorkMeter(limits: limits, domain: .core) return try doubleValue(policy: policy, meter: &meter) } - package func doubleValue(policy: JSONNumberConversionPolicy, meter: inout JSONWorkMeter) throws -> Double { - let text = try binaryConversionText(meter: &meter) + package func doubleValue(policy: JSONNumberConversionPolicy, meter: inout JSONWorkMeter, chargesOutput: Bool = true) throws -> Double { + let text = try binaryConversionText(meter: &meter, chargesOutput: chargesOutput) try meter.checkpoint() guard let value = Double(text), value.isFinite else { throw JSONFailure(.conversion, code: .inexactConversion) } try meter.checkpoint() diff --git a/Sources/JSONCLIEngine/CLIBuildIdentity.swift b/Sources/JSONCLIEngine/CLIBuildIdentity.swift index d2437cc..2c12cef 100644 --- a/Sources/JSONCLIEngine/CLIBuildIdentity.swift +++ b/Sources/JSONCLIEngine/CLIBuildIdentity.swift @@ -2,6 +2,6 @@ // Generated by Scripts/generate_cli_identity.py; excludes this file. // SHA-256 of the ordered Package.swift + Sources content manifest (DocC catalogs excluded), not a Git commit. package enum CLIBuildIdentity { - package static let revision = "sha256:3b65f6ca79902befb4c50649094739bf30838631c2f2fbc46515c2c6df64e164" + package static let revision = "sha256:528b7f15a54f6c25125196ffef9ec2979a2b05c549f214ac8c643503e0becc48" package static let kind = "source-sha256" } diff --git a/Tests/JSONTests/CanonicalizationTests.swift b/Tests/JSONTests/CanonicalizationTests.swift new file mode 100644 index 0000000..efc765b --- /dev/null +++ b/Tests/JSONTests/CanonicalizationTests.swift @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: Apache-2.0 +import Foundation +import Testing +@testable import JSON + +private func canonical(_ value: JSON, _ policy: JSONCanonicalWriter.NumberPolicy = .preserving) throws -> String { + String(decoding: try JSONCanonicalWriter(numberPolicy: policy).write(value), as: UTF8.self) +} +private func canonical(parsing text: String, _ policy: JSONCanonicalWriter.NumberPolicy = .preserving) throws -> String { + try canonical(JSONParser().parse(text), policy) +} +private func expectInexact(_ body: () throws -> Void) { + do { try body(); Issue.record("Expected an inexact-conversion failure") } + catch let failure as JSONFailure { #expect(failure.category == .conversion && failure.code == .inexactConversion) } + catch { Issue.record("Unexpected error: \(error)") } +} + +/// RFC 8785 Appendix B: binary64 bit patterns and their ECMAScript spelling. +@Test func canonicalNumbersMatchTheRFC8785AppendixBVectors() throws { + let vectors: [(UInt64, String)] = [ + (0x0000000000000000, "0"), (0x8000000000000000, "0"), + (0x0000000000000001, "5e-324"), (0x8000000000000001, "-5e-324"), + (0x7fefffffffffffff, "1.7976931348623157e+308"), (0xffefffffffffffff, "-1.7976931348623157e+308"), + (0x4340000000000000, "9007199254740992"), (0xc340000000000000, "-9007199254740992"), + (0x4430000000000000, "295147905179352830000"), + (0x44b52d02c7e14af5, "9.999999999999997e+22"), (0x44b52d02c7e14af6, "1e+23"), + (0x44b52d02c7e14af7, "1.0000000000000001e+23"), + (0x444b1ae4d6e2ef4e, "999999999999999700000"), (0x444b1ae4d6e2ef4f, "999999999999999900000"), + (0x444b1ae4d6e2ef50, "1e+21"), + (0x3eb0c6f7a0b5ed8c, "9.999999999999997e-7"), (0x3eb0c6f7a0b5ed8d, "0.000001"), + (0x41b3de4355555553, "333333333.3333332"), (0x41b3de4355555554, "333333333.33333325"), + (0x41b3de4355555555, "333333333.3333333"), (0x41b3de4355555556, "333333333.3333334"), + (0x41b3de4355555557, "333333333.33333343"), + (0xbecbf647612f3696, "-0.0000033333333333333333"), + (0x43143ff3c1cb0959, "1424953923781206.2"), + ] + for (bits, expected) in vectors { + let double = Double(bitPattern: bits) + // Swift's round-trip decimal is exactly the canonical value, so it is preserved. + #expect(try canonical(.number(.fromFiniteDouble(double))) == expected, "\(String(bits, radix: 16))") + // The exact binary expansion needs rounding to reach the same spelling. + var meter = try JSONWorkMeter(limits: .balanced, domain: .core) + let exact = try JSONNumber.exactBinary(double, meter: &meter) + #expect(try canonical(.number(exact), .rounding) == expected, "\(String(bits, radix: 16))") + } + for bits: UInt64 in [0x7fffffffffffffff, 0x7ff0000000000000] { + expectInexact { _ = try JSONNumber.fromFiniteDouble(Double(bitPattern: bits)) } + } +} + +/// RFC 8785 section 3.2.2: the complete sample, whose numbers need rounding. +@Test func canonicalOutputMatchesTheRFC8785Sample() throws { + let input = #""" + { + "numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], + "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", + "literals": [null, true, false] + } + """# + let expected = #"{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27],"string":"€$\u000f\nA'B\"\\\\\"/"}"# + #expect(try canonical(parsing: input, .rounding) == expected) + // 333333333.33333329 is not the binary64 value it rounds to, so preserving refuses it. + expectInexact { _ = try canonical(parsing: input) } +} + +/// RFC 8785 section 3.2.3: keys sort by UTF-16 code units, not scalar values. +@Test func canonicalKeysSortByUTF16CodeUnits() throws { + let input = #""" + {"\u20ac": "Euro Sign", "\r": "Carriage Return", "\ufb33": "Hebrew Letter Dalet With Dagesh", + "1": "One", "\ud83d\ude00": "Emoji: Grinning Face", "\u0080": "Control", + "\u00f6": "Latin Small Letter O With Diaeresis"} + """# + let text = try canonical(parsing: input) + let order = ["Carriage Return", "One", "Control", "Latin Small Letter O With Diaeresis", + "Euro Sign", "Emoji: Grinning Face", "Hebrew Letter Dalet With Dagesh"] + let positions = order.map { text.range(of: "\"\($0)\"")!.lowerBound } + #expect(positions == positions.sorted()) + // Scalar order puts U+FB33 before U+1F600; UTF-16 order puts the surrogate pair first. + let scalar = try JSONWriter(options: .init(keyOrder: .scalar)).writeString(JSONParser().parse(input)) + #expect(scalar.range(of: "Hebrew")!.lowerBound < scalar.range(of: "Emoji")!.lowerBound) + #expect(text.range(of: "Emoji")!.lowerBound < text.range(of: "Hebrew")!.lowerBound) +} + +@Test func canonicalStringsUseShortEscapesAndLiteralUTF8() throws { + let text = "\u{8}\t\n\u{C}\r\u{1}\u{1F}\"\\/\u{7F}é\u{1F600}" + #expect(try canonical(.string(text)) == "\"\\b\\t\\n\\f\\r\\u0001\\u001f\\\"\\\\/\u{7F}é\u{1F600}\"") + // The ordinary writer keeps its \u00xx spelling for every control character. + #expect(try JSONWriter().writeString(.string("\n")) == #""\u000a""#) +} + +@Test func preservingPolicyKeepsExactValuesAndRefusesChangedOnes() throws { + let accepted: [(String, String)] = [ + ("0.1", "0.1"), ("1E30", "1e+30"), ("4.50", "4.5"), ("-0", "0"), ("-0.0e7", "0"), ("100", "100"), + ("1e21", "1e+21"), ("1e20", "100000000000000000000"), ("0.000001", "0.000001"), ("1e-7", "1e-7"), + ("-1.5e-7", "-1.5e-7"), ("123.456", "123.456"), ("9007199254740992", "9007199254740992"), + ("1.0", "1"), ("12e-1", "1.2"), + ] + for (input, expected) in accepted { + #expect(try canonical(parsing: input) == expected, "\(input)") + #expect(try canonical(parsing: input, .rounding) == expected, "\(input)") + } + for input in ["9007199254740993", "0.10000000000000001", "1e-400", "333333333.33333329"] { + expectInexact { _ = try canonical(parsing: input) } + } + #expect(try canonical(parsing: "9007199254740993", .rounding) == "9007199254740992") + #expect(try canonical(parsing: "1e-400", .rounding) == "0") + for input in ["1e400", "-1e400"] { + expectInexact { _ = try canonical(parsing: input) } + expectInexact { _ = try canonical(parsing: input, .rounding) } + } +} + +@Test func equalValuesProduceEqualCanonicalBytes() throws { + let a = try JSONParser().parse(#"{"b":[1.0,{"y":true,"x":null}],"a":"é"}"#) + let b = try JSONParser().parse(#"{ "a" : "é", "b" : [ 1, { "x" : null, "y" : true } ] }"#) + #expect(a == b) + let bytes = try JSONCanonicalWriter().write(a) + #expect(try JSONCanonicalWriter().write(b) == bytes) + #expect(String(decoding: bytes, as: UTF8.self) == #"{"a":"é","b":[1,{"x":null,"y":true}]}"#) + // Canonical output parses back to an equal value. + #expect(try JSONParser().parse(bytes) == a) + #expect(try canonical(.array([])) == "[]") + #expect(try canonical(.object(JSONObject())) == "{}") +} + +@Test func canonicalWritingIsBoundedAndReported() throws { + let root = try JSONParser().parse(#"{"z":[1,2,3],"a":"text"}"#) + let report = try JSONCanonicalWriter().writeWithReport(root) + #expect(report.result.count == 24) + #expect(report.statistics[.outputBytes] == 24) + #expect(report.statistics.domain == .writing) + var limits = JSONLimits.balanced; limits[.outputBytes] = 24 + #expect(try JSONCanonicalWriter(limits: limits).write(root) == report.result) + limits[.outputBytes] = 23 + do { _ = try JSONCanonicalWriter(limits: limits).write(root); Issue.record("Expected an output limit failure") } + catch let failure as JSONFailure { #expect(failure.code == .limitExceeded && failure.resource?.metric == .outputBytes) } + limits = .balanced; limits[.maxDepth] = 1 + do { _ = try JSONCanonicalWriter(limits: limits).write(root); Issue.record("Expected a depth limit failure") } + catch let failure as JSONFailure { #expect(failure.category == .resource) } +} + +@Test func cancelledCanonicalWritingThrowsCancellation() async { + let root = JSON.array(Array(repeating: .string(String(repeating: "x", count: 64)), count: 4096)) + let task = Task { + while !Task.isCancelled { await Task.yield() } + _ = try JSONCanonicalWriter().write(root) + } + task.cancel() + do { try await task.value; Issue.record("Cancelled canonical writing must throw") } + catch { #expect(error is CancellationError) } +}