Latest 0.9.0
Homepage https://github.com/fen-x/OMM
License MIT
Platforms ios 8.0, tvos 9.0, watchos 2.0, requires ARC
Authors

OMM GitHub MIT License GitHub Release Carthage compatible CocoaPods Version CocoaPods License [CocoaPods Platform]()

OMM is a one more mapper that helps to map JSON and property list 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 also provides one-way conversion from property list.

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 distinguish absence of value and wrong value.

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

Quick Start

Initialization

import OMM

let jsonDataNode = makeNode(forJSON: someData)
let propertyListDataNode = makeNode(forPropertyList: anotherData)
let anyObjectNode = makeNode(for: anyObject)

Subscript

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

Materialization

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

Error handling

do {
    let intValue: Int = try node.value()
    // 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.self)
    // intValue contains optional integer value
} catch {
    // Value is not number
}

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

Transforms

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

struct LengthTransform: Transform {
    func apply(to node: Node) throws -> Double {
        let value = try node.value(Double.self)
        if value < 0 {
            throw error(reason: "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: Node) throws {
        identifier = try node["user_id"].required.value(transformedWith: Int64Transform)
        name = try node["user_name"].optional?.value(String.self) ?? ""
    }
}
let user = try node.value(User)

License

OMM is released under the MIT License.

Installation

pod 'OMM'

# to get property list support as well
pod 'OMM/PropertyList'
github "fen-x/OMM"
  1. Use OMM4JSON.framework for JSON only support.
  2. Use OMM4PropertyList.framework for property list support.
  3. Use OMM.framework for both JSON and property list support.

Do not forget to use appropriate module name for import declaration: OMM4JSON, OMM4PropertyList, and OMM, respectively.

Latest podspec

{
    "name": "OMM",
    "version": "0.9.0",
    "summary": "OMM is a one more mapper that helps to map JSON and property list objects to Swift instances.",
    "description": "OMM has clear compact API, supports Swift error handling, provides subscript syntax, allows to to distinguish absence of value and wrong value, and gives opportunity to reuse mapping using mappable types and custom transforms.",
    "homepage": "https://github.com/fen-x/OMM",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Ivan Nikitin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/fen-x/OMM.git",
        "tag": "v0.9.0"
    },
    "requires_arc": true,
    "swift_version": "4.2",
    "cocoapods_version": ">= 1.4.0",
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "default_subspecs": [
        "JSON",
        "Mappable"
    ],
    "subspecs": [
        {
            "name": "Core",
            "source_files": "OMM/Core/*.swift"
        },
        {
            "name": "JSON",
            "source_files": "OMM/{JSON,Scalars,Transforms}/*.swift",
            "exclude_files": "OMM/Scalars/Scalar+{Date,Data}.swift",
            "dependencies": {
                "OMM/Core": []
            }
        },
        {
            "name": "PropertyList",
            "source_files": "OMM/{PropertyList,Scalars,Transforms}/*.swift",
            "exclude_files": "OMM/Transforms/Date*Transform.swift",
            "dependencies": {
                "OMM/Core": []
            }
        },
        {
            "name": "Mappable",
            "source_files": "OMM/Mappable/*.swift",
            "dependencies": {
                "OMM/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This