This repository includes some useful tools for Codable protocol and data decoding.
Usage
JSON
JSON enum makes it easy to deal with JSON data.
Use String, Int subscripts and dynamic member lookup (“dot” syntax) to retrieve a value:
if let name = json.root.array[0]?.name.string {...}
//or if let name = json["root"]["array"][0]["name"]?.string {...}
JSON enum uses purely Swift JSON serialization based on Swift Protobuf implementation, which is extremely fast.
Confirms to Codable.
VDJSONDecoder
An object that decodes instances of a data type from JSON objects.
Main differences from Foundation JSONDecoder:
Decoding non-string types from quoted values (like “true”, “0.0”)
Custom JSON parsing via (([CodingKey], JSON) -> JSON) closure
Purely Swift and faster
VDJSONEncoder
Purely Swift version of JSONEncoder.
URLQueryEncoder and URLQueryDecoder
Encoder and decoder for query strings.
struct SomeStruct: Codable {
var title = "Query_string"
let number = 0
}
let baseURL = URL(string: "https://base.url")!
let value = SomeStruct()
let url = try? URLQueryEncoder().encode(value, for: baseURL)
//url = "https://base.url?title=Query_string&number=0"
DictionaryDecoder and DictionaryEncoder
NSManagedDecodable, NSManagedEncodable and NSManagedCodable protocols
Protocols that make your NSManagedObject subclasses confirm to Codable protocol.
PlainCodingKey
Simple CodingKey struct.
Type reflection for Decodable types
let properties: [String: Any.Type] = Mirror.reflect(SomeType.self)
//or Mirror(SomeType.self).children
Tools for creating custom encoders/decoders
Based on similar logic when writing different encoders/decoders DecodingUnboxer and EncodingBoxer protocols were implemented.
Examples of usage are all encoders in decoders in this repo.
VDCodable
Description
This repository includes some useful tools for
Codable
protocol and data decoding.Usage
JSON
JSON
enum makes it easy to deal with JSON data. UseString
,Int
subscripts and dynamic member lookup (“dot” syntax) to retrieve a value:JSON
enum uses purely Swift JSON serialization based on Swift Protobuf implementation, which is extremely fast. Confirms toCodable
.VDJSONDecoder
An object that decodes instances of a data type from JSON objects. Main differences from Foundation
JSONDecoder
:(([CodingKey], JSON) -> JSON)
closureVDJSONEncoder
Purely Swift version of
JSONEncoder
.URLQueryEncoder
andURLQueryDecoder
Encoder and decoder for query strings.
DictionaryDecoder
andDictionaryEncoder
NSManagedDecodable
,NSManagedEncodable
andNSManagedCodable
protocolsProtocols that make your
NSManagedObject
subclasses confirm toCodable
protocol.PlainCodingKey
Simple
CodingKey
struct.Decodable
typesBased on similar logic when writing different encoders/decoders
DecodingUnboxer
andEncodingBoxer
protocols were implemented. Examples of usage are all encoders in decoders in this repo.Installation
Add the following line to your Podfile:
and run
pod update
from the podfile directory first.Create a
Package.swift
file.Author
Voidilov, voidilov@gmail.com
License
VDCodable is available under the MIT license. See the LICENSE file for more info.