https://github.com/theolampert/jsvaluecoder
A Codable implementation for JavascriptCore's JSValue
https://github.com/theolampert/jsvaluecoder
codable javascript javascriptcore swift
Last synced: about 1 year ago
JSON representation
A Codable implementation for JavascriptCore's JSValue
- Host: GitHub
- URL: https://github.com/theolampert/jsvaluecoder
- Owner: theolampert
- License: mit
- Created: 2023-05-13T07:32:31.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-07T13:05:00.000Z (about 2 years ago)
- Last Synced: 2025-01-28T01:36:19.780Z (about 1 year ago)
- Topics: codable, javascript, javascriptcore, swift
- Language: Swift
- Homepage:
- Size: 49.8 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### JSValueCoder

Codable implementation for JavascriptCore's `JSValue`
Note: This was written pre the `swift-foundation` project's adoption into macOS and iOS, JSON decoding was very slow and this library made more sense. If you're targeting newer platforms, you can probably just use JSONDecoder/Encoder.
#### Usage:
```swift
let context = JSContext()!
struct Note: Codable {
let id: Int
}
struct User: Codable {
let id: String
let name: String
let score: Double
}
let user = User(
id: "7",
name: "John",
score: 1.3,
note: Note(id: 1)
)
let encoder = JSValueEncoder()
let decoder = JSValueDecoder()
let jsValue = try encoder.encode(user, in: context)
let decoded = try decoder.decode(User.self, from: jsValue)
```
#### Notes:
This was originally based off of work here https://github.com/byss/KBJSValueCoding with the following changes:
- Support was added for nested structs
- Support was added for arrays
- Bug was fixed when decoding Bools
- Bug was fixed when decoding with different key coding strategies
- Tests added