Search code examples
iosjsonswiftencode

JSON encode non primitive types in Swift


Helo I would like to encode non standard types such as sims_float3x3 and [vector_float3]. What is the recommended way to this?

I tried to use Struct like so,

struct Example: Codable {
    var test_var1:simd_float3x3
    var test_var2:[vector_float3]
}

I get the error, does not conform to protocol 'Decodable'

I also tried,

let data = try encoder.encode(test_var1)

I get the error - Argument type 'simd_float3x3' does not conform to expected type 'Encodable'

I currently can do like so,

let data_col1 = try encoder.encode(test_var1.columns.0) // simd_float3
let data_col2 = try encoder.encode(test_var1.columns.1) // simd_float3
let data_col3 = try encoder.encode(test_var1.columns.2) // simd_float3

But is there any way to do this more elegantly / efficiently?


Solution

  • You can use the same technique as shown in this answer for the outer array:

    import SceneKit
    
    extension simd_float3x3: Codable {
        public init(from decoder: Decoder) throws {
            var container = try decoder.unkeyedContainer()
            try self.init(container.decode([float3].self))
        }
        public func encode(to encoder: Encoder) throws {
            var container = encoder.unkeyedContainer()
            try container.encode([columns.0, columns.1, columns.2])
         }
     }
    

    Playground testing

    let simdFloat = simd_float3x3(float3(0, 1, 2), float3(3,4, 5), float3(6, 7, 8))
    do {
        let data = try JSONEncoder().encode(simdFloat)
        let decodedObject = try JSONDecoder().decode(simd_float3x3.self, from: data)
        print(decodedObject)  // simd_float3x3([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
    
    } catch {
        print(error)
    }