Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Mapper static func to MapperParse #1061

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ObjectMapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
C135CABA1D7631DB00BA9338 /* DataTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C135CAB31D762F6900BA9338 /* DataTransform.swift */; };
C135CABB1D7631DB00BA9338 /* DataTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C135CAB31D762F6900BA9338 /* DataTransform.swift */; };
C135CABC1D7631DC00BA9338 /* DataTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C135CAB31D762F6900BA9338 /* DataTransform.swift */; };
C7A1C2B322B3A48100858848 /* MapperParse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7A1C2B222B3A48100858848 /* MapperParse.swift */; };
C7A1C2B422B3A48100858848 /* MapperParse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7A1C2B222B3A48100858848 /* MapperParse.swift */; };
C7A1C2B522B3A48100858848 /* MapperParse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7A1C2B222B3A48100858848 /* MapperParse.swift */; };
C7A1C2B622B3A48100858848 /* MapperParse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7A1C2B222B3A48100858848 /* MapperParse.swift */; };
CD16030A1AC023D6000CD69A /* ObjectMapper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD1602FF1AC023D5000CD69A /* ObjectMapper.framework */; };
CD1603181AC02437000CD69A /* ObjectMapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 6AAC8F7B19F03C2900E7A677 /* ObjectMapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
CD1603191AC02451000CD69A /* Mapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AAC8FC419F048FE00E7A677 /* Mapper.swift */; };
Expand Down Expand Up @@ -300,6 +304,7 @@
BC1E7F361ABC44C000F9B1CF /* EnumTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EnumTransform.swift; sourceTree = "<group>"; };
C135CAB31D762F6900BA9338 /* DataTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTransform.swift; sourceTree = "<group>"; };
C135CAB61D76303E00BA9338 /* DataTransformTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DataTransformTests.swift; path = ObjectMapperTests/DataTransformTests.swift; sourceTree = "<group>"; };
C7A1C2B222B3A48100858848 /* MapperParse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapperParse.swift; sourceTree = "<group>"; };
CD1602FF1AC023D5000CD69A /* ObjectMapper.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ObjectMapper.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CD1603091AC023D6000CD69A /* ObjectMapper-macOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ObjectMapper-macOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
CD44374C1AAE9C1100A271BA /* NestedKeysTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = NestedKeysTests.swift; path = ObjectMapperTests/NestedKeysTests.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -478,6 +483,7 @@
3BAD2C0B1BDDB10D00E6B203 /* Mappable.swift */,
030114A81D95187600FBFD4F /* ImmutableMappable.swift */,
6AAC8FC419F048FE00E7A677 /* Mapper.swift */,
C7A1C2B222B3A48100858848 /* MapperParse.swift */,
6AF148961D99A912002BEA2C /* Operators */,
6AAC8FC319F048FE00E7A677 /* FromJSON.swift */,
6AAC8FC719F048FE00E7A677 /* ToJSON.swift */,
Expand Down Expand Up @@ -839,6 +845,7 @@
6AC692361BE3FD3A004C119A /* Operators.swift in Sources */,
84D4F8551CC3B643008B0FB6 /* NSDecimalNumberTransform.swift in Sources */,
6AC692371BE3FD3A004C119A /* FromJSON.swift in Sources */,
C7A1C2B622B3A48100858848 /* MapperParse.swift in Sources */,
6AF148951D99A7CA002BEA2C /* EnumOperators.swift in Sources */,
997B4A4A1D3FA20D005E3F31 /* DictionaryTransform.swift in Sources */,
6AC692381BE3FD3A004C119A /* ToJSON.swift in Sources */,
Expand Down Expand Up @@ -898,6 +905,7 @@
6ACB15D41BC7F1D0006C029C /* Map.swift in Sources */,
84D4F8541CC3B643008B0FB6 /* NSDecimalNumberTransform.swift in Sources */,
6A2AD0471B2C786C0097E150 /* FromJSON.swift in Sources */,
C7A1C2B522B3A48100858848 /* MapperParse.swift in Sources */,
6AF148941D99A7CA002BEA2C /* EnumOperators.swift in Sources */,
997B4A491D3FA20D005E3F31 /* DictionaryTransform.swift in Sources */,
6A2AD0481B2C786C0097E150 /* ToJSON.swift in Sources */,
Expand Down Expand Up @@ -929,6 +937,7 @@
6ACB15D21BC7F1D0006C029C /* Map.swift in Sources */,
84D4F8521CC3B643008B0FB6 /* NSDecimalNumberTransform.swift in Sources */,
6AAC8FD319F048FE00E7A677 /* DateTransform.swift in Sources */,
C7A1C2B322B3A48100858848 /* MapperParse.swift in Sources */,
6AF148921D99A7CA002BEA2C /* EnumOperators.swift in Sources */,
997B4A471D3FA20D005E3F31 /* DictionaryTransform.swift in Sources */,
BC1E7F371ABC44C000F9B1CF /* EnumTransform.swift in Sources */,
Expand Down Expand Up @@ -989,6 +998,7 @@
CD1603201AC02461000CD69A /* CustomDateFormatTransform.swift in Sources */,
CD16031E1AC02461000CD69A /* DateFormatterTransform.swift in Sources */,
84D4F8531CC3B643008B0FB6 /* NSDecimalNumberTransform.swift in Sources */,
C7A1C2B422B3A48100858848 /* MapperParse.swift in Sources */,
6AF148931D99A7CA002BEA2C /* EnumOperators.swift in Sources */,
6ACB15D31BC7F1D0006C029C /* Map.swift in Sources */,
997B4A481D3FA20D005E3F31 /* DictionaryTransform.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Sources/ImmutableMappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public extension Mapper where N: ImmutableMappable {
}

func mapArray(JSONString: String) throws -> [N] {
guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
guard let JSONObject = MapperParse.toJSONObject(JSONString: JSONString) else {
throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
}

Expand All @@ -285,7 +285,7 @@ public extension Mapper where N: ImmutableMappable {
// MARK: Dictionary mapping functions

func mapDictionary(JSONString: String) throws -> [String: N] {
guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
guard let JSONObject = MapperParse.toJSONObject(JSONString: JSONString) else {
throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
}

Expand Down Expand Up @@ -350,7 +350,7 @@ internal extension Mapper {
}

func mapOrFail(JSONString: String) throws -> N {
guard let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) else {
guard let JSON = MapperParse.toJSONDictionary(JSONString: JSONString) else {
throw MapError(key: nil, currentValue: JSONString, reason: "Cannot parse into '[String: Any]'")
}
return try mapOrFail(JSON: JSON)
Expand Down
20 changes: 12 additions & 8 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class Mapper<N: BaseMappable> {

/// Map a JSON string onto an existing object
public func map(JSONString: String, toObject object: N) -> N {
if let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) {
if let JSON = MapperParse.toJSONDictionary(JSONString: JSONString) {
return map(JSON: JSON, toObject: object)
}
return object
Expand All @@ -76,7 +76,7 @@ public final class Mapper<N: BaseMappable> {

/// Map a JSON string to an object that conforms to Mappable
public func map(JSONString: String) -> N? {
if let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) {
if let JSON = MapperParse.toJSONDictionary(JSONString: JSONString) {
return map(JSON: JSON)
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public final class Mapper<N: BaseMappable> {

/// Maps a JSON array to an object that conforms to Mappable
public func mapArray(JSONString: String) -> [N]? {
let parsedJSON: Any? = Mapper.parseJSONString(JSONString: JSONString)
let parsedJSON: Any? = MapperParse.toJSONObject(JSONString: JSONString)

if let objectArray = mapArray(JSONObject: parsedJSON) {
return objectArray
Expand Down Expand Up @@ -172,7 +172,7 @@ public final class Mapper<N: BaseMappable> {

/// Maps a JSON object to a dictionary of Mappable objects if it is a JSON dictionary of dictionaries, or returns nil.
public func mapDictionary(JSONString: String) -> [String: N]? {
let parsedJSON: Any? = Mapper.parseJSONString(JSONString: JSONString)
let parsedJSON: Any? = MapperParse.toJSONObject(JSONString: JSONString)
return mapDictionary(JSONObject: parsedJSON)
}

Expand Down Expand Up @@ -260,12 +260,14 @@ public final class Mapper<N: BaseMappable> {
// MARK: Utility functions for converting strings to JSON objects

/// Convert a JSON String into a Dictionary<String, Any> using NSJSONSerialization
@available(*, deprecated, message:"Use MapperParse")
public static func parseJSONStringIntoDictionary(JSONString: String) -> [String: Any]? {
let parsedJSON: Any? = Mapper.parseJSONString(JSONString: JSONString)
return parsedJSON as? [String: Any]
}

/// Convert a JSON String into an Object using NSJSONSerialization
@available(*, deprecated, message:"Use MapperParse")
public static func parseJSONString(JSONString: String) -> Any? {
let data = JSONString.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let data = data {
Expand Down Expand Up @@ -361,17 +363,18 @@ extension Mapper {
public func toJSONString(_ object: N, prettyPrint: Bool = false) -> String? {
let JSONDict = toJSON(object)

return Mapper.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
return MapperParse.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
}

/// Maps an array of Objects to a JSON string with option of pretty formatting
public func toJSONString(_ array: [N], prettyPrint: Bool = false) -> String? {
let JSONDict = toJSONArray(array)

return Mapper.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
return MapperParse.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
}

/// Converts an Object to a JSON string with option of pretty formatting
@available(*, deprecated, message:"Use MapperParse")
public static func toJSONString(_ JSONObject: Any, prettyPrint: Bool) -> String? {
let options: JSONSerialization.WritingOptions = prettyPrint ? .prettyPrinted : []
if let JSON = Mapper.toJSONData(JSONObject, options: options) {
Expand All @@ -382,6 +385,7 @@ extension Mapper {
}

/// Converts an Object to JSON data with options
@available(*, deprecated, message:"Use MapperParse")
public static func toJSONData(_ JSONObject: Any, options: JSONSerialization.WritingOptions) -> Data? {
if JSONSerialization.isValidJSONObject(JSONObject) {
let JSONData: Data?
Expand All @@ -403,7 +407,7 @@ extension Mapper where N: Hashable {

/// Maps a JSON array to an object that conforms to Mappable
public func mapSet(JSONString: String) -> Set<N>? {
let parsedJSON: Any? = Mapper.parseJSONString(JSONString: JSONString)
let parsedJSON: Any? = MapperParse.toJSONObject(JSONString: JSONString)

if let objectArray = mapArray(JSONObject: parsedJSON) {
return Set(objectArray)
Expand Down Expand Up @@ -449,7 +453,7 @@ extension Mapper where N: Hashable {
public func toJSONString(_ set: Set<N>, prettyPrint: Bool = false) -> String? {
let JSONDict = toJSONSet(set)

return Mapper.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
return MapperParse.toJSONString(JSONDict as Any, prettyPrint: prettyPrint)
}
}

Expand Down
64 changes: 64 additions & 0 deletions Sources/MapperParse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// MapperParse.swift
// ObjectMapper-iOS
//
// Created by yixiaojichunqiu on 2019/6/13.
// Copyright © 2019 hearst. All rights reserved.
//

import Foundation

public final class MapperParse
{
/// Converts an Object to a JSON string with option of pretty formatting
public static func toJSONString(_ JSONObject: Any, prettyPrint: Bool) -> String? {
let options: JSONSerialization.WritingOptions = prettyPrint ? .prettyPrinted : []
if let JSON = MapperParse.toJSONData(JSONObject, options: options) {
return String(data: JSON, encoding: String.Encoding.utf8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pass just .utf8 instead of String.Encoding.utf8

return String(data: JSON, encoding: .utf8)

}

return nil
}

/// Converts an Object to JSON data with options
public static func toJSONData(_ JSONObject: Any, options: JSONSerialization.WritingOptions) -> Data? {
if JSONSerialization.isValidJSONObject(JSONObject) {
let JSONData: Data?
do {
JSONData = try JSONSerialization.data(withJSONObject: JSONObject, options: options)
} catch let error {
print(error)
JSONData = nil
}

return JSONData
}

return nil
}

// MARK: Utility functions for converting strings to JSON objects

/// Convert a JSON String into a Dictionary<String, Any> using NSJSONSerialization
public static func toJSONDictionary(JSONString: String) -> [String: Any]? {
let parsedJSON: Any? = MapperParse.toJSONObject(JSONString: JSONString)
return parsedJSON as? [String: Any]
}

/// Convert a JSON String into an Object using NSJSONSerialization
public static func toJSONObject(JSONString: String) -> Any? {
let data = JSONString.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let data = data {
let parsedJSON: Any?
do {
parsedJSON = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
} catch let error {
print(error)
parsedJSON = nil
}
return parsedJSON
}

return nil
}
}