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.
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)
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