Description
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 alternatives and similar libraries
Based on the "JSON" category.
Alternatively, view Himotoki alternatives based on common mentions on social networks and blogs.
-
JSONModel
Magical Data Modeling Framework for JSON - allows rapid creation of smart data models. You can use it in your iOS, macOS, watchOS and tvOS apps. -
JSONExport
JSONExport is a desktop application for Mac OS X which enables you to export JSON objects as model classes with their associated constructors, utility methods, setters and getters in your favorite language. -
JSON-Framework
This framework implements a strict JSON parser and generator in Objective-C. -
Gloss
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021 -
SwiftyJSONAccelerator
macOS app to generate Swift 5 code for models from JSON (with Codeable) -
TouchJSON
A humane JSON Objective-C un-framework. (TouchJSON has been deprecated - see README) -
Decodable
[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right -
Genome
A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux) -
CodableAlamofire
An extension for Alamofire that converts JSON data into Decodable objects. -
FastEasyMapping
A tool for fast serializing & deserializing of JSON -
Elevate
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. -
Coolie
Coolie(苦力) helps you to create models (& their constructors) from a JSON file. -
OCMapper
Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON -
AlamofireJsonToObjects
An Alamofire extension which converts JSON response data into swift objects using EVReflection -
Jay
Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization. -
A JSON parser with concise API written in Swift
A JSON parser with concise API written in Swift. -
LazyObject
Lazily deserialize JSON into strongly typed Swift objects
Appwrite - The Open Source Firebase alternative introduces iOS support
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Himotoki or a related project?
README
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:
struct Group: Himotoki.Decodable {
let name: String
let floor: Int
let locationName: String
let optional: [String]?
// MARK: Himotoki.Decodable
static func decode(_ e: Extractor) throws -> Group {
return try Group(
name: e <| "name",
floor: e <| "floor",
locationName: e <| [ "location", "name" ], // Parse nested objects
optional: e <|? "optional" // Parse optional arrays of values
)
}
}
func testGroup() {
var JSON: [String: AnyObject] = [ "name": "Himotoki", "floor": 12 ]
let g = try? Group.decodeValue(JSON)
XCTAssert(g != nil)
XCTAssert(g?.name == "Himotoki")
XCTAssert(g?.floor == 12)
XCTAssert(g?.optional == nil)
JSON["name"] = nil
do {
try Group.decodeValue(JSON)
} catch let DecodeError.MissingKeyPath(keyPath) {
XCTAssert(keyPath == "name")
} catch {
XCTFail()
}
}
: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 |
`<\ | ?` | T? |
`<\ | \ | ` |
`<\ | \ | ?` |
`<\ | -\ | ` |
`<\ | -\ | ?` |
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:
// Creates a `Transformer` instance.
let URLTransformer = Transformer<String, URL> { urlString throws -> URL in
if let url = URL(string: urlString) {
return url
}
throw customError("Invalid URL string: \(urlString)")
}
let url: URL = try URLTransformer.apply(e <| "foo_url")
let otherURLs: [URL] = try URLTransformer.apply(e <| "bar_urls")
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:
use_frameworks! pod "Himotoki", "~> 3.1"
Run
pod install
.
License
Himotoki is released under the [MIT License](LICENSE.md).
*Note that all licence references and agreements mentioned in the Himotoki README section above
are relevant to that project's source code only.