Skip to content

API driven JSON mapper. Generates and auto-imports model files into your project based on, JSON or PLIST, mapping files configured against an arbitrary dictionary. 2 Birds, 1 Stone.

License

Notifications You must be signed in to change notification settings

AntonTheDev/JSONModelKit

Repository files navigation

Cocoapods Compatible Carthage compatible Build Status Platform License Join the chat at https://gitter.im/AntonTheDev/JSONModelKit

JSONModelKit

alt tag

JSONModelKit is an API centric mapping framework that uses, JSON or Plist, mapping files to define a model against an arbitrary dictionary. At build time, the library will generate the model files based on the definitions, and add them to the project automatically, or update any existing files.

By reducing the focus on the model layer itself, and keeping it continuously in sync with an API response in a single place, we can now treat the model as just a "data bucket", and extend it as needed. It comes really handy when implementing MVVM as part of a project's architecture, as it basically reduces the model(s) to ingestible wrapper(s) around dictionary data.

Features

  • Auto-Model Generation .json/.plists configuration files
  • Optional & Non-Optional Property Mapping:
    • String, Int, Double, Float, Bool, Array, Dictionary
    • Collections Types: Array, Dictionary
    • Structs, Enums, Closures, Tuples via Transformations
  • Supports Nested Types
  • Mapping Nested Values
  • Predefine Default Values

Installation

Communication

  • If you found a bug, or have a feature request, open an issue.
  • If you need help or a general question, use Stack Overflow. (tag 'json-modelkit')
  • If you want to contribute, review the Contribution Guidelines, and submit a pull request.

Basic Use

Once configured per the Installation instructions, run the build script once ⌘+B. You will see the Model folder appears within your project.

All JSON Mappings will go into the Model/Mappings directory, and your Classes will be generated into the Model directory, then automatically added to your project during build time.

Simple Example

Let look as a simple mapping that defines a class below in JSON.

Input File: JSOModelKit/Mappings/Business.json

{
    "uuid" : {
        "key" : "identifier",
        "type" : "Double",
        "nonoptional" : "true"
    },
    "businessName" : {
        "key" : "business_name",
        "type" : "String"
    },
    "ratings" : {
        "key" : "ratings",
        "type" : "Array",
        "subtype" : "Double"
    },
    "metaTags" : {
        "key" : "metadata.tags",
        "type" : "Array",
        "subtype" : "String"
    },
    "open" : {
        "key" : "open",
        "type" : "Bool",
        "default" : "0"
    }
}

Run the build script once ⌘+B. and you will see that it generated the following files in the output directory. This will also be reflected in the Project structure within the Model group

NOTE: Every time a new mapping configurations is added, the following build will always be canceled by Xcode, and needs to be run again. This is due to the project file changing in the middle of a build, since a new file is added. If no new mapping is added, it will build as usual

Observe the internal file that was generated

class _Business  {

	var uuid : Double

	var metaTags : [Float]?
	var ratings : [Float]?
	var locations : [Coordinate]?
	var dateOpened : Date?
	var businessName : String?

	..........

	required init(uuid  _uuid : Double)  {
		// Required init with all non-optionals defined
	}

	convenience init?(_ dictionary: Dictionary<String, AnyObject>) {
		// Failable initializer, returns nil when any non-optional values is not defined
	}

	func updateWithDictionary(dictionary: Dictionary<String, AnyObject>) {
		// Helper methods to updated an instance with a new dictionary of values
	}
}

Once a JSON response is received call the following method, and all the properties will be parsed and mapped accordingly.

let newInstance = TestModelObject(dataDictionary)

More Examples

Below is a list of examples for the supported features by JSONModelKit. Each provides an overall view on how to setup the model mapping file, and short examples of the outputs generated by pre-packaged script within the framework.

License

JSONModelKit is released under the MIT license. See License for details.

About

API driven JSON mapper. Generates and auto-imports model files into your project based on, JSON or PLIST, mapping files configured against an arbitrary dictionary. 2 Birds, 1 Stone.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages