-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSON.swift
202 lines (165 loc) · 4.44 KB
/
GSON.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// GSON.swift
// Web
// Original source code found at Github Arrow
// Created by Sumeet Kumar on 16/12/2016.
// Copyright © 2016 Sumeet.Kumar. All rights reserved.
//
//
import Foundation
/**
JSON Helper class that parse string response to JSON
Simple JsonObject and JsonArray Parser
It provives a way to access JSON values via subscripting
*/
open class GSON {
/// This is the raw data of the JSON
open var json: Any?
/// This build a JSON object with raw data.
public init?(_ json: Any?) {
guard json != nil else {
return nil
}
self.json = json
}
public static func with(_ data: Data) -> GSON {
let jsonArray = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
return GSON.init(jsonArray)!
}
public func debug() {
print("GSON", json!)
}
/**
Get JSON array
*/
open func JsonArray() -> [GSON]?{
guard let a = json as? [Any] else {
return nil
}
return a.map { GSON($0) }.flatMap {$0}
}
/**
Get JSON Object
*/
open func JsonObject() -> GSON?{
if let json = self[0] {
return json
}
return self
}
open func getJsonObject() -> GSON? {
return JsonObject()
}
open func get(_ key: String) -> String
{
if let str = self[key]?.json as? String {
return str
}
return ""
}
open func getBool(_ key: String) -> Bool
{
if let b = self[key]?.json as? Bool {
return b
}
// if the given data type is not bool try to get as string and parse it as Boolean value
else if let i = Bool(get(key).lowercased()) {
return i
}
return false
}
open func getInt(_ key: String) -> Int
{
if let num = self[key]?.json as? Int {
return num
}
// if the given data type is not integer try to get as string and parse it as integer
else if let i = Int(get(key)) {
return i
}
return -1
}
open func isNotNull() -> Bool
{
if self.json != nil {
return true
}
return false
}
public func success() -> Int {
if let c = Int(get("ResponseCode")) {
return c
}
return -1
}
open func code() -> Int{
return success()
}
func hasKey(_ key: String) -> Bool {
return key.characters.split {$0 == "."}.count > 1
}
func parseKey(_ keyPath: String) -> GSON? {
if var intermediateValue = GSON(json) {
for k in keysForKeyPath(keyPath) {
if !tryParseJSONKey(k, intermediateValue: &intermediateValue) {
return nil
}
}
return intermediateValue
}
return nil
}
func keysForKeyPath(_ keyPath: String) -> [String] {
return keyPath.characters.split {$0 == "."}.map(String.init)
}
func tryParseJSONKey(_ key: String, intermediateValue: inout GSON) -> Bool
{
if let ik = Int(key), let value = intermediateValue[ik]
{
intermediateValue = value
}
else if let value = intermediateValue[key]
{
intermediateValue = value
}
else
{
return false
}
return true
}
func regularParsing(_ key: String) -> GSON? {
guard let d = json as? [String: Any], let x = d[key], let gson = GSON(x) else {
return nil
}
return gson
}
open subscript(key: String) -> GSON? {
get { return hasKey(key) ? parseKey(key) : regularParsing(key) }
set(obj) {
if var d = json as? [String:Any] {
d[key] = obj
}
}
}
open subscript(index: Int) -> GSON? {
get {
guard let array = json as? [Any], array.count > index else {
return nil
}
return GSON(array[index])
}
}
}
/// USAGE
/*
// array
if let array = json.JsonArray
{
for jsonObject in array {
}
}
// get object at postion
JsonObject json = GSON.JsonObject() // from array to obj
var str: String = gson[0]?["PicURl"]?.data as! String
*/