tristanhimmelman /ObjectMapper

Simple JSON Object mapping written in Swift

★ 9,119⑂ 1,024👁 210⑃ 338● 811▤ 60
SwiftMITCreated 2014-10-16
下载:4.4.34.4.3 ↓4.4.32024-05-02
+0
日增长
9,119
stars
1,024
forks
210
watchers
338
prs
811
issues
60
releases

README

ObjectMapper
============





ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from JSON. 

- Features
- The Basics
- Mapping Nested Objects
- Custom Transformations
- Subclassing
- Generic Objects
- Mapping Context
- ObjectMapper + Alamofire 
- ObjectMapper + Realm
- Projects using ObjectMapper
- To Do
- Contributing
- Installation

 Features:
- Mapping JSON to objects
- Mapping objects to JSON
- Nested Objects (stand alone, in arrays or in dictionaries)
- Custom transformations during mapping
- Struct support
- Immutable support

 The Basics
To support mapping, a class or struct just needs to implement the Mappable protocol which includes the following functions:
swift
init?(map: Map)
mutating func mapping(map: Map)

ObjectMapper uses the ().map(JSONString: JSONString)
// Create JSON String from Model
let JSONString = Mapper().toJSONString(user, prettyPrint: true)


ObjectMapper can map classes composed of the following types:
- Int
- Bool
- Double
- Float
- String
- RawRepresentable (Enums)
- Array
- Dictionary
- Object
- Array
- Array
- Set 
- Dictionary
- Dictionary
- Optionals of all the above
- Implicitly Unwrapped Optionals of the above

 Mappable Protocol

 mutating func mapping(map: Map) 
This function is where all mapping definitions should go. When parsing JSON, this function is executed after successful object creation. When generating JSON, it is the only function that is called on the object.

 init?(map: Map) 
This failable initializer is used by ObjectMapper for object creation. It can be used by developers to validate JSON prior to object serialization. Returning nil within the function will prevent the mapping from occuring. You can inspect the JSON stored within the Map object to do your validation:
swift
required init?(map: Map){
	// check if a required "name" property exists within the JSON.
	if map.JSON["name"] == nil {
		return nil
	}
}


 StaticMappable Protocol
StaticMappable is an alternative to Mappable. It provides developers with a static function that is used by ObjectMapper for object initialization instead of init?(map: Map). 

Note: StaticMappable, like Mappable, is a sub protocol of BaseMappable which is where the mapping(map: Map) function is defined.

 static func objectForMapping(map: Map) - BaseMappable? 
ObjectMapper uses this function to get objects to use for mapping. Developers should return an instance of an object that conforms to BaseMappable in this function. This function can also be used to:
- validate JSON prior to object serialization
- provide an existing cached object to be used for mapping
- return an object of another type (which also conforms to BaseMappable) to be used for mapping. For instance, you may inspect the JSON to infer the type of object that should be used for mapping (see examples in ClassClusterTests.swift)

If you need to implement ObjectMapper in an extension, you will need to adopt this protoc

Top issues

Latest release

4.4.3 4.4.3 2024-05-02
What's Changed
 Update Package.swift to swift-tools-version 5.5 by @kaulex99 in https://github.com/tristanhimmelman/ObjectMapper/pull/1148

 New Contributors
 @kaulex99 made their first contribution in https://github.com/tristanhimmelman/ObjectMapper/pull/1148

Full Changelog: https://github.com/tristanhimmelman/ObjectMapper/compare/4.4.2...4.4.3

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON