Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on May 17, 2024. It is now read-only.
/mapperPublic archive

A JSON deserialization library for Swift

License

NotificationsYou must be signed in to change notification settings

lyft/mapper

Repository files navigation

Mapper is a simple Swift library to convert JSON to strongly typedobjects. One advantage to Mapper over some other libraries is you canhave immutable properties.

Installation

use_frameworks!pod"ModelMapper"
github "lyft/mapper"

Usage

Simple example:

import Mapper// Conform to the Mappable protocolstructUser:Mappable{letid:StringletphotoURL:URL?  // Implement this initializerinit(map:Mapper)throws{try id= map.from("id")    photoURL= map.optionalFrom("avatar_url")}}// Create a user!letJSON:NSDictionary=...letuser=User.from(JSON) // This is a 'User?'

Using with enums:

enumUserType:String{case Normal="normal"case Admin="admin"}structUser:Mappable{letid:Stringlettype:UserTypeinit(map:Mapper)throws{try id= map.from("id")try type= map.from("user_type")}}

NestedMappable objects:

structUser:Mappable{letid:Stringletname:Stringinit(map:Mapper)throws{try id= map.from("id")try name= map.from("name")}}structGroup:Mappable{letid:Stringletusers:[User]init(map:Mapper)throws{try id= map.from("id")    users= map.optionalFrom("users")??[]}}

UseConvertible to transparently convert other types from JSON:

extensionCLLocationCoordinate2D:Convertible{publicstaticfunc fromMap(_ value:Any)throws->CLLocationCoordinate2D{guardlet location= valueas?NSDictionary,let latitude=location["lat"]as?Double,let longitude=location["lng"]as?Doubleelse{throwMapperError.convertibleError(value: value, type:[String:Double].self)}returnCLLocationCoordinate2D(latitude: latitude, longitude: longitude)}}structPlace:Mappable{letname:Stringletlocation:CLLocationCoordinate2Dinit(map:Mapper)throws{try name= map.from("name")try location= map.from("location")}}letJSON:NSDictionary=["name":"Lyft HQ","location":["lat":37.7603392,"lng":-122.41267249999999,],]letplace=Place.from(JSON)

Custom Transformations

privatefunc extractFirstName(object:Any?)throws->String{guardlet fullName= objectas?Stringelse{throwMapperError.convertibleError(value: object, type:String.self)}letparts= fullName.characters.split{ $0==""}.map(String.init)iflet firstName= parts.first{return firstName}throwMapperError.customError(field:nil, message:"Couldn't split the string!")}structUser:Mappable{letfirstName:Stringinit(map:Mapper)throws{try firstName= map.from("name", transformation: extractFirstName)}}

Parse nested or entire objects

structUser:Mappable{letname:StringletJSON:AnyObjectinit(map:Mapper)throws{    // Access the 'first' key nested in a 'name' dictionarytry name= map.from("name.first")    // Access the original JSON (maybe for use with a transformation)try JSON= map.from("")}}

See the docstrings and tests for more information and examples.

Open Radars

These radars have affected the current implementation of Mapper

  • rdar://23376350Protocol extensions with initializers do not work in extensions
  • rdar://23358609Protocol extensions with initializers do not play well with classes
  • rdar://23226135Can't conform to protocols with similar generic function signatures
  • rdar://23147654Generic functions are not differentiated by their ability to throw
  • rdar://23695200Using the?? operator many times is unsustainable.
  • rdar://23697280Lazy collection elements can be evaluated multiple times.
  • rdar://23718307Non final class with protocol extensions returningSelf don't work

License

Mapper is maintained byLyft and released underthe Apache 2.0 license. See LICENSE for details

About

A JSON deserialization library for Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp