Skip to content
forked from fen-x/OMM

OMM is a one more mapper that helps to map JSON objects to Swift instances

License

Notifications You must be signed in to change notification settings

stephencelis/OMM

 
 

Repository files navigation

OMM GitHub MIT License GitHub Release Carthage compatible

OMM is a one more mapper that helps to map JSON objects to Swift instances.

Features

Firstly, OMM provides only one-way conversion from JSON. For both to and from conversions try something else like SwiftyJSON, ObjectMapper or whatever.

OMM does not extend any standard types. It allows to keep clear compact API.

OMM supports Swift error handling.

OMM provides subscript syntax to get values from both objects and arrays.

OMM allows to to distinguish absence of value and wrong value.

OMM gives opportunity to reuse mapping uging mappable types and custom transforms.

Quick Start

Initialization

import OMM

let binaryDataNode = NodeForJSONObjectWithData(someNSData)
let anyObjectNode = NodeForObject(anyObject)

Subscript

let nodeForArrayElement = node[42]
let nodeForPopertyValue = node["propertyName"]
let nodeForMixedPath = node["property", "anotherProperty", 0]

Materialization

let intValue = try node.value(Int)
let arrayOfStrings = try node.array(String)

Error handling

do {
	let intValue = try node.value(Int)
	// intValue contains non-optional integer value	
} catch let error as MappingError {
	// There is no value or value is not number
} catch {
	// Actually, never happens
}

Optionals

do {
	let intValue = try node.optional?.value(Int)
	// intValue contains optional integer value
} catch {
	// Value is not number
}

let stringValue = try? node.value(String)
// stringValue contains optional string value anyway

Transforms

let URLValue = try node.value(transformedWith: URLTransform())

struct LengthTransform: TransformType {
	func applyToNode(node: NodeType) throws -> Double {
		let value = try node.value(Double)
		if value < 0 {
			throw errorWithReason("Length should be non-negative")
		}
		return value
	}
}
let length = try node.value(transformedWith: LengthTransform())

Mappable types

struct User: Mappable {
	let identifier: Int64
	let name: String 

	init(node: NodeType) throws {
		identifier = try node["user_id"].required.value(transformedWith: Int64Transform)
		name = try node["user_name"].optional?.value(String) ?? ""
	}
}
let user = try node.value(User)

License

OMM is released under the MIT License.

Installation

pod 'OMM'
github "fen-x/OMM"

About

OMM is a one more mapper that helps to map JSON objects to Swift instances

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 97.1%
  • Ruby 2.0%
  • Objective-C 0.9%