swift-extras-json icon indicating copy to clipboard operation
swift-extras-json copied to clipboard

Support Embedded Swift

Open ephemer opened this issue 2 months ago • 0 comments

Hallo aus Berlin @fabianfett,

We're interested in using JSONValue with Embedded Swift to manually encode and decode values like in your example here. We could just "rip out" the bits we need, but I thought this functionality might be useful for some of your audience as well.

Note that Codable and co are not available on Embedded at all, so the library without these changes does not compile. With this change, users have a minimal but totally usable JSON implementation for Embedded platforms and devices.

Note that the resulting Embedded Swift binary must be compiled with -l swiftUnicodeDataTables, found in the respective embedded library directory of the Swift toolchain, for example:

export TOOLCHAIN_DIR="$(dirname "$(dirname "$(swiftly run which swift)")")"

swift run \
  -Xswiftc -target -Xswiftc arm64-apple-macos15.0 \
  -Xswiftc -enable-experimental-feature -Xswiftc Embedded \
  -Xswiftc -lswiftUnicodeDataTables -Xswiftc -L"$TOOLCHAIN_DIR/lib/swift/embedded/arm64-apple-macos"

With this, I'm able to build a test file via Embedded Swift:

import ExtrasJSON

func print(_ value: JSONValue) {
    var str: [UInt8] = []
    result.appendBytes(to: &str)
    print(String(decoding: str, as: UTF8.self))
}

let result = try! JSONParser().parse(bytes: """
    {
        "test": 123.123,
        "test2": true
    }
""".utf8)

print(result)

What do you think?

ephemer avatar Oct 14 '25 17:10 ephemer