반응형

iOS에서의 JSON 데이터 처리

최근 대부분의 통신 데이터를 JSON 포멧으로 처리 하는 경우가 많다. 
JSON 데이터를 처리하는데 유용한 함수들이다.

Array or Dictionary Object to JSON String

Array, Dictionary Object를 JSON 문자열로 변환 하기 위한 함수 
(Java의 Stringify 함수처럼)

/**
 JSON Object(NSArray, NSDictionary) -> String
 
 ```
 let inputJsonDictionary = ["first": true, "second": false]
 let resultDictionaryString = JSONStringify(value: inputJsonDictionary as AnyObject)
 print(resultDictionaryString)
 // {"second":false,"first":true}
 
 let inputJsonArray = [["first": true], ["second": false]]
 let resultArrayString = JSONStringify(value: inputJsonArray as AnyObject)
 print(resultArrayString)
 // [{"first":true},{"second":false}]
 ```
 
 - Parameter value: 변환할 Object(NSArray, NSDictionary)
 - Parameter prettyPrinted: 이쁘게 표현할지 여부 (default = false)
 - Returns : JSON 문자열
 */
func JSONStringify(value: AnyObject, prettyPrinted: Bool = false) -> String {
    
    let options = prettyPrinted ? JSONSerialization.WritingOptions.prettyPrinted : []
    
    if JSONSerialization.isValidJSONObject(value) {
        do {
            let data = try JSONSerialization.data(withJSONObject:value, options:options)
            if let jsonString = String(data: data, encoding: String.Encoding.utf8) {
                return jsonString
            }
        } catch {
            print("JSON serialization failed:  \(error)")
        }
    }
    return ""
}

Class Object to JSON String

Class에 대한 객체를 JSON 문자열로 변환하기 위해서는 따로 Serializer을 구현해야 한다.
고맙게도 오픈소스로 공개된게 있다. (MIT 라이센스)
https://github.com/peheje/JsonSerializerSwift

JsonSerializer.swift


/*The MIT License (MIT)

Copyright (c) 2015 Peter Helstrup Jensen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/

import Foundation

/// Handles Convertion from instances of objects to JSON strings. Also helps with casting strings of JSON to Arrays or Dictionaries.
open class JSONSerializer {
    
    /**
    Errors that indicates failures of JSONSerialization
    - JsonIsNotDictionary:  -
    - JsonIsNotArray:           -
    - JsonIsNotValid:           -
    */
    public enum JSONSerializerError: Error {
        case jsonIsNotDictionary
        case jsonIsNotArray
        case jsonIsNotValid
    }
    
    //http://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary
    /**
    Tries to convert a JSON string to a NSDictionary. NSDictionary can be easier to work with, and supports string bracket referencing. E.g. personDictionary["name"].
    - parameter jsonString: JSON string to be converted to a NSDictionary.
    - throws: Throws error of type JSONSerializerError. Either JsonIsNotValid or JsonIsNotDictionary. JsonIsNotDictionary will typically be thrown if you try to parse an array of JSON objects.
    - returns: A NSDictionary representation of the JSON string.
    */
    open static func toDictionary(_ jsonString: String) throws -> NSDictionary {
        if let dictionary = try jsonToAnyObject(jsonString) as? NSDictionary {
            return dictionary
        } else {
            throw JSONSerializerError.jsonIsNotDictionary
        }
    }
    
    /**
    Tries to convert a JSON string to a NSArray. NSArrays can be iterated and each item in the array can be converted to a NSDictionary.
    - parameter jsonString: The JSON string to be converted to an NSArray
    - throws: Throws error of type JSONSerializerError. Either JsonIsNotValid or JsonIsNotArray. JsonIsNotArray will typically be thrown if you try to parse a single JSON object.
    - returns: NSArray representation of the JSON objects.
    */
    open static func toArray(_ jsonString: String) throws -> NSArray {
        if let array = try jsonToAnyObject(jsonString) as? NSArray {
            return array
        } else {
            throw JSONSerializerError.jsonIsNotArray
        }
    }
    
    /**
    Tries to convert a JSON string to AnyObject. AnyObject can then be casted to either NSDictionary or NSArray.
    - parameter jsonString: JSON string to be converted to AnyObject
    - throws: Throws error of type JSONSerializerError.
    - returns: Returns the JSON string as AnyObject
    */
    fileprivate static func jsonToAnyObject(_ jsonString: String) throws -> Any? {
        var any: Any?
        
        if let data = jsonString.data(using: String.Encoding.utf8) {
            do {
                any = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            }
            catch let error as NSError {
                let sError = String(describing: error)
                NSLog(sError)
                throw JSONSerializerError.jsonIsNotValid
            }
        }
        return any
    }

    /**
    Generates the JSON representation given any custom object of any custom class. Inherited properties will also be represented.
    - parameter object: The instantiation of any custom class to be represented as JSON.
    - returns: A string JSON representation of the object.
    */
    open static func toJson(_ object: Any, prettify: Bool = false) -> String {
        var json = "{"
        let mirror = Mirror(reflecting: object)
        
        var children = [(label: String?, value: Any)]()
        
        if let mirrorChildrenCollection = AnyRandomAccessCollection(mirror.children) {
            children += mirrorChildrenCollection
        }
        else {
            let mirrorIndexCollection = AnyCollection(mirror.children)
            children += mirrorIndexCollection
        }
        
        var currentMirror = mirror
        while let superclassChildren = currentMirror.superclassMirror?.children {
            let randomCollection = AnyRandomAccessCollection(superclassChildren)!
            children += randomCollection
            currentMirror = currentMirror.superclassMirror!
        }
        
        var filteredChildren = [(label: String?, value: Any)]()
        
        for (optionalPropertyName, value) in children {

            if let optionalPropertyName = optionalPropertyName {

                if !optionalPropertyName.contains("notMapped_") {
                    filteredChildren.append((optionalPropertyName, value))
                }
                
            }
            else {
                filteredChildren.append((nil, value))
            }
        }
        
        var skip = false
        let size = filteredChildren.count
        var index = 0
        
        var first = true
        
        for (optionalPropertyName, value) in filteredChildren {
            skip = false
            
            let propertyName = optionalPropertyName
            let property = Mirror(reflecting: value)
            
            var handledValue = String()
            
            if propertyName != nil && propertyName == "some" && property.displayStyle == Mirror.DisplayStyle.struct {
                handledValue = toJson(value)
                skip = true
            }
            else if (value is Int || value is Double || value is Float || value is Bool) && property.displayStyle != Mirror.DisplayStyle.optional {
                handledValue = String(describing: value)
            }
            else if let array = value as? [Int?] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += value != nil ? String(value!) : "null"
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? [Double?] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += value != nil ? String(value!) : "null"
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? [Float?] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += value != nil ? String(value!) : "null"
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? [Bool?] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += value != nil ? String(value!) : "null"
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? [String?] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += value != nil ? "\"\(value!)\"" : "null"
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? [String] {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    handledValue += "\"\(value)\""
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if let array = value as? NSArray {
                handledValue += "["
                for (index, value) in array.enumerated() {
                    if !(value is Int) && !(value is Double) && !(value is Float) && !(value is Bool) && !(value is String) {
                        handledValue += toJson(value)
                    }
                    else {
                        handledValue += "\(value)"
                    }
                    handledValue += (index < array.count-1 ? ", " : "")
                }
                handledValue += "]"
            }
            else if property.displayStyle == Mirror.DisplayStyle.class ||
                property.displayStyle == Mirror.DisplayStyle.struct ||
                String(describing: value).contains("#") {
                handledValue = toJson(value)
            }
            else if property.displayStyle == Mirror.DisplayStyle.optional {
                let str = String(describing: value)
                if str != "nil" {
                    handledValue = String(str).substring(with: str.characters.index(str.startIndex, offsetBy: 9)..<str.characters.index(str.endIndex, offsetBy: -1))
                } else {
                    handledValue = "null"
                }
            }
            else {
                handledValue = String(describing: value) != "nil" ? "\"\(value)\"" : "null"
            }
            
            if !skip {
                
                // if optional propertyName is populated we'll use it
                if let propertyName = propertyName {
                    json += "\"\(propertyName)\": \(handledValue)" + (index < size-1 ? ", " : "")
                }
                // if not then we have a member an array
                else {
                    // if it's the first member we need to prepend ]
                    if first {
                        json += "["
                        first = false
                    }
                    // if it's not the last we need a comma. if it is the last we need to close ]
                    json += "\(handledValue)" + (index < size-1 ? ", " : "]")
                }
                
            } else {
                json = "\(handledValue)" + (index < size-1 ? ", " : "")
            }
            
            index += 1
        }
        
        if !skip {
            json += "}"
        }
        
        if prettify {
           let jsonData = json.data(using: String.Encoding.utf8)!
           let jsonObject = try! JSONSerialization.jsonObject(with: jsonData, options: [])
           let prettyJsonData = try! JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
           json = NSString(data: prettyJsonData, encoding: String.Encoding.utf8.rawValue)! as String
        }
        
        return json
    }
    
   
}

Class 객체를 JSON 문자열로 바꾸는 예제

class JsonItem: NSObject {
    var name = ""
    var value = ""
    
    init(name: String = "", value: String = ""){
        self.name = name
        self.value = value
    }
}

let jsonItem = JsonItem()
jsonItem.name = "abc"
jsonItem.value = "124"
    
let jsonString = JSONSerializer.toJson(jsonItem)
print(jsonString)
// {"name": "abc", "value": "124"}

JSON String to Dictionary or Array

JSON 문자열을 Dictionary, Array로 변환하기 위해서
개인적으로 다음과 같이 String을 extension하여 사용하고 있다.

extension String {
    
    /// JSON String -> Dictionary
    var jsonStringToDictionary: [String: AnyObject]? {
        if let data = data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }
    
    /// JSON String -> Array
    var jsonStringToArray: [AnyObject]? {
        if let data = data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [AnyObject]
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }
    
    /// JSON String -> AnyObject
    var jsonStringToAnyObject: AnyObject? {
        
        if let data = data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] as AnyObject?
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }
}

JSON 문자열을 클래스 객체로 만드는 예제

JSONSerializer과 jsonStringToDictionary 사용

class JsonItem: NSObject {
    var name = ""
    var value = ""
    
    init(name: String = "", value: String = ""){
        self.name = name
        self.value = value
    }
    
    init(_ dictionary: [String:AnyObject]) {
        super.init()
        
        for (key, value) in dictionary {
            if let intValue = value as? Int {
                let newValue = String(intValue)
                if (responds(to: NSSelectorFromString(key))) {
                    setValue(newValue, forKey: key)
                }
            } else {
                if (responds(to: NSSelectorFromString(key))) {
                    setValue(value, forKey: key)
                }
            }
        }
    }
}

let jsonItem = JsonItem()
jsonItem.name = "abc"
jsonItem.value = "124"
    
let jsonString = JSONSerializer.toJson(jsonItem)

if let data = jsonString.jsonStringToDictionary {
    let responseItem = JsonItem(data)
    print("name : \(responseItem.name), value : \(responseItem.value)")
    // name : abc, value : 124
}


반응형
Posted by 까칠코더
,