Skip to content

Add JSONBridge example - #49

Open
MaxDesiatov wants to merge 2 commits into
mainfrom
maxd/jsonbridge
Open

Add JSONBridge example#49
MaxDesiatov wants to merge 2 commits into
mainfrom
maxd/jsonbridge

Conversation

@MaxDesiatov

Copy link
Copy Markdown
Contributor

Added a JSONBridge example demonstrating how the result of JavaScript's JSON.parse maps onto a Swift type via BridgeJS built with Embedded Swift for WebAssembly. Sources/JSONBridge/main.swift declares a @JSClass struct User whose @JSGetter properties read typed fields, imports globalThis.JSON.parse statically as a @JSClass(from: .global) namespace, and exports run(), which parses three fixtures exercising present, null, and absent nickname values plus Double to Int narrowing. It depends on JavaScriptKit 0.54.1, builds with the PackageToJS plugin via swift package ... js --use-cdn.

Adds a `JSONBridge` example demonstrating how the result of JavaScript's `JSON.parse` maps onto a static Swift type via BridgeJS under Embedded Swift for WebAssembly. `Sources/JSONBridge/main.swift` declares a `@JSClass struct User` whose `@JSGetter` properties read typed fields, imports `globalThis.JSON.parse` statically as a `@JSClass(from: .global)` namespace, and exports `run()`, which parses three fixtures exercising present, `null`, and absent `nickname` values plus `Double` to `Int` narrowing. It depends on JavaScriptKit `0.54.1`, builds with the `PackageToJS` plugin via `swift package ... js --use-cdn`.
Comment thread JSONBridge/Sources/JSONBridge/main.swift Outdated
Co-authored-by: Max Desiatov <m_desiatov@apple.com>
@MaxDesiatov

MaxDesiatov commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@krodak this is not the final version and I'm curious how BridgeJS could be extended to make this more versatile, ideally we want to showcase de-serialization for multiple types here. Maybe JSON.parse could be made generic somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant