An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

### JSValueCoder

![Test](https://github.com/theolampert/JSValueCoder/actions/workflows/swift.yml/badge.svg)

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