Skip to content

Instantly share code, notes, and snippets.

@magicien
Last active December 4, 2021 21:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save magicien/b0c87d26ffded8aa2161630c56853ca4 to your computer and use it in GitHub Desktop.
Save magicien/b0c87d26ffded8aa2161630c56853ca4 to your computer and use it in GitHub Desktop.
import Foundation
import SceneKit
let json = """
{
"position": [2.0, 5.0, 3.0],
"rotation": [0.0, 0.73, 0.0, 0.73]
}
""".data(using: .utf8)!
extension SCNVector3: Codable {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.x = try container.decode(CGFloat.self)
self.y = try container.decode(CGFloat.self)
self.z = try container.decode(CGFloat.self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(self.x)
try container.encode(self.y)
try container.encode(self.z)
}
}
extension SCNVector4: Codable {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.x = try container.decode(CGFloat.self)
self.y = try container.decode(CGFloat.self)
self.z = try container.decode(CGFloat.self)
self.w = try container.decode(CGFloat.self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(self.x)
try container.encode(self.y)
try container.encode(self.z)
try container.encode(self.w)
}
}
struct ModelState: Codable {
let position: SCNVector3
let rotation: SCNVector4
}
let decoder = JSONDecoder()
do {
let state = try decoder.decode(ModelState.self, from: json)
print(state)
} catch {
print ("\(error.localizedDescription)")
}
let encoder = JSONEncoder()
let state = ModelState(position: SCNVector3(1, 2, 3), rotation: SCNVector4(0.73, 0, 0, -0.73))
do {
let data = try encoder.encode(state)
let text = String(data: data, encoding: .utf8)!
print(text)
} catch {
print ("\(error.localizedDescription)")
}
@isaac-weisberg
Copy link

isaac-weisberg commented May 31, 2018

Fine piece of code, gonna utilize Codable support for SCNVector3
Thank you <3

@isaac-weisberg
Copy link

Actually, this whole Codable thing appears to be pretty easy now) thanks, really entertaining

@iMostfa
Copy link

iMostfa commented Dec 4, 2021

an updated version for latest swift version

extension SCNVector3: Codable {
    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let x = try container.decode(Float.self)
        let y = try container.decode(Float.self)
        let z = try container.decode(Float.self)
        self.init(x, y, z)

    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(self.x)
        try container.encode(self.y)
        try container.encode(self.z)
    }
}

extension SCNVector4: Codable {
    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let x = try container.decode(Float.self)
        let y = try container.decode(Float.self)
        let z = try container.decode(Float.self)
        let w = try container.decode(Float.self)
        self.init(x, y, z, w)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(self.x)
        try container.encode(self.y)
        try container.encode(self.z)
        try container.encode(self.w)
    }
}

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