Skip to content

A Codable implementation for JavascriptCore's JSValue

License

Notifications You must be signed in to change notification settings

theolampert/JSValueCoder

Repository files navigation

JSValueCoder

Test

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:

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

About

A Codable implementation for JavascriptCore's JSValue

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages