Skip to content

JSONDecoder and JSONEncoder in nativ Swift

Notifications You must be signed in to change notification settings

janhendry/JSONCoderIO

Repository files navigation

JSONCoderIO

JSONCopderIO is a implementation of JSONDecoder and JSONEncoder. Is implemented natively in Swift and support the Codable Protocol. I write this Libary for the SwiftIO Board from MadMachineIO. Big thanks for this borad!

Instalation

MadMachine IDE

Here you can find a good tutorial that explains how to install a library in MadMachien IDE.

quicktype

quicktype is a amazing tool to generate Codable Struct/Class for your JSON File. Check it out!

Example

import JSONCoderIO

var json = """
    {
      "Name" : "Kevin",
      "Age" : 16,
      "Nickname" : "Kivi",
      "Single" : true
    }
    """

// We need the right Codable Structs  https://app.quicktype.io

struct Person: Codable {
    let name: String
    let age: Int
    let nickname: String
    let single: Bool

    enum CodingKeys: String, CodingKey {
        case name = "Name"
        case age = "Age"
        case nickname = "Nickname"
        case single = "Single"
    }
}

// Decode String

let decoder = try! JSONDecoderIO(json)
let person = try! Person(from: decoder)
print(person)

//Encode the Struct 

let encoder = JSONEncoderIO()
try! person.encode(to: encoder)
let jsonString = try? encoder.getJson()
print(jsonString)

Overview

An overview of what has been tested with this library. If you have found errors or formats that I need to test. Let me know.

Libary Type Decodable Encodable
Swift Standard Library Array true true
Swift Standard Library Bool true true
Swift Standard Library Dictionary true true
Swift Standard Library Double true true
Swift Standard Library Float true true
Swift Standard Library Int true true
Swift Standard Library Int8 true true
Swift Standard Library Int16 true true
Swift Standard Library Int32 true true
Swift Standard Library Int64 true true
Swift Standard Library Optional true true
Swift Standard Library Set true true
Swift Standard Library String true true

Test Missing:

Libary Type
Swift Standard Library ClosedRange
Swift Standard Library CollectionDifference
Swift Standard Library CollectionDifference.Change
Swift Standard Library ContiguousArray
Swift Standard Library Float16
Swift Standard Library PartialRangeThrough
Swift Standard Library PartialRangeUpTo
Swift Standard Library Range

About

JSONDecoder and JSONEncoder in nativ Swift

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages