Himotoki
Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift. This library is highly inspired by the popular Swift JSON parsing libraries: Argo and ObjectMapper.
Himotoki has the same meaning of 'decoding' in Japanese.
- Just do JSON decoding (deserialization) well. JSON encoding (serialization) will not be supported going forward. :wink:
- Much simpler API.
- Fail-fast conditional model building. This is useful for some
struct
s with non-optionallet
properties. - No external dependencies.
Let's take a look at a simple example:
:warning: Please note that you should need to add the module name Himotoki
to Decodable
(Himotoki.Decodable
) to avoid type name collision with Foundation.Decodable
in Xcode 9 or later. :warning:
Implementing the decode
method for your models
To implement the decode
method for you models conforming to the Decodable
protocol, you can use the following Extractor
's extraction methods:
public func value<T: Decodable>(_ keyPath: KeyPath) throws -> T
public func valueOptional<T: Decodable>(_ keyPath: KeyPath) throws -> T?
public func array<T: Decodable>(_ keyPath: KeyPath) throws -> [T]
public func arrayOptional<T: Decodable>(_ keyPath: KeyPath) throws -> [T]?
public func dictionary<T: Decodable>(_ keyPath: KeyPath) throws -> [String: T]
public func dictionaryOptional<T: Decodable>(_ keyPath: KeyPath) throws -> [String: T]?
Extraction Operators
Himotoki also supports the following operators to decode JSON elements, where T
is a generic type conforming to the Decodable
protocol.
Operator | Decode element as | Remarks |
---|---|---|
<\| |
T |
A value |
<\|? |
T? |
An optional value |
<\|\| |
[T] |
An array of values |
<\|\|? |
[T]? |
An optional array of values |
<\|-\| |
[String: T] |
A dictionary of values |
<\|-\|? |
[String: T]? |
An optional dictionary of values |
Value Transformation
You can transform an extracted value to an instance of non-Decodable
types by passing the value to a Transformer
instance as follows:
Requirements
Himotoki 4.x requires / supports the following environments:
- Swift 4.2 / Xcode 10.1 or later
- OS X 10.9 or later
- iOS 8.0 or later
- tvOS 9.0 or later
- watchOS 2.0 or later
- Linux is also supported
Installation
Currently Himotoki supports installation via the package managers Carthage and CocoaPods.
Carthage
Himotoki is Carthage compatible.
- Add
github "ikesyo/Himotoki" ~> 3.1
to your Cartfile. - Run
carthage update
.
CocoaPods
Himotoki also can be used by CocoaPods.
-
Add the followings to your Podfile:
-
Run
pod install
.
License
Himotoki is released under the [MIT License].