Created
May 14, 2018 00:36
-
-
Save randyzhao/b5193fbbce7d768e6566f1cc481f1fdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Coordinate: Codable { | |
| let latitude: Double | |
| let longitude: Double | |
| let elevation: Double | |
| let hasGoogleMap: Bool | |
| private enum CodingKeys: String, CodingKey { | |
| case latitude | |
| case longitude | |
| case additionalInfo = "additional_info" | |
| } | |
| private enum AdditionalInfoKeys: String, CodingKey { | |
| case elevation | |
| case hasGoogleMap = "has_google_map" | |
| } | |
| init(from decoder: Decoder) throws { | |
| let values = try decoder.container(keyedBy: CodingKeys.self) | |
| latitude = try values.decode(Double.self, forKey: .latitude) | |
| longitude = try values.decode(Double.self, forKey: .longitude) | |
| let additionalInfo = try values.nestedContainer(keyedBy: AdditionalInfoKeys.self, forKey: .additionalInfo) | |
| elevation = try additionalInfo.decode(Double.self, forKey: .elevation) | |
| hasGoogleMap = try additionalInfo.decode(Bool.self, forKey: .hasGoogleMap) | |
| } | |
| func encode(to encoder: Encoder) throws { | |
| var container = encoder.container(keyedBy: CodingKeys.self) | |
| try container.encode(latitude, forKey: .latitude) | |
| try container.encode(longitude, forKey: .longitude) | |
| var additionalInfo = container.nestedContainer(keyedBy: AdditionalInfoKeys.self, forKey: .additionalInfo) | |
| try additionalInfo.encode(elevation, forKey: .elevation) | |
| try additionalInfo.encode(hasGoogleMap, forKey: .hasGoogleMap) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment