mirror of
https://github.com/zhigang1992/JSON.git
synced 2026-01-12 22:39:47 +08:00
Add JSON swift
This commit is contained in:
@@ -8,12 +8,14 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
04AF95DC1EA5001A0002C72D /* JSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 04AF95DA1EA5001A0002C72D /* JSON.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
04AF95E31EA500490002C72D /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04AF95E21EA500490002C72D /* JSON.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
04AF95D71EA5001A0002C72D /* JSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSON.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
04AF95DA1EA5001A0002C72D /* JSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
|
||||
04AF95DB1EA5001A0002C72D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
04AF95E21EA500490002C72D /* JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSON.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -47,6 +49,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04AF95DA1EA5001A0002C72D /* JSON.h */,
|
||||
04AF95E21EA500490002C72D /* JSON.swift */,
|
||||
04AF95DB1EA5001A0002C72D /* Info.plist */,
|
||||
);
|
||||
path = JSON;
|
||||
@@ -95,6 +98,7 @@
|
||||
TargetAttributes = {
|
||||
04AF95D61EA5001A0002C72D = {
|
||||
CreatedOnToolsVersion = 8.3.1;
|
||||
LastSwiftMigration = 0830;
|
||||
ProvisioningStyle = Automatic;
|
||||
};
|
||||
};
|
||||
@@ -131,6 +135,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
04AF95E31EA500490002C72D /* JSON.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -241,6 +246,8 @@
|
||||
04AF95E01EA5001A0002C72D /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CODE_SIGN_IDENTITY = "";
|
||||
DEFINES_MODULE = YES;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
@@ -248,10 +255,12 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = JSON/Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.matrix.JSON;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 3.0;
|
||||
};
|
||||
name = Debug;
|
||||
@@ -259,6 +268,8 @@
|
||||
04AF95E11EA5001A0002C72D /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CODE_SIGN_IDENTITY = "";
|
||||
DEFINES_MODULE = YES;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
@@ -266,6 +277,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = JSON/Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.matrix.JSON;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
|
||||
@@ -5,6 +5,22 @@
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "04AF95D61EA5001A0002C72D"
|
||||
BuildableName = "JSON.framework"
|
||||
BlueprintName = "JSON"
|
||||
ReferencedContainer = "container:JSON.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
@@ -26,6 +42,15 @@
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "04AF95D61EA5001A0002C72D"
|
||||
BuildableName = "JSON.framework"
|
||||
BlueprintName = "JSON"
|
||||
ReferencedContainer = "container:JSON.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
@@ -35,6 +60,15 @@
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "04AF95D61EA5001A0002C72D"
|
||||
BuildableName = "JSON.framework"
|
||||
BlueprintName = "JSON"
|
||||
ReferencedContainer = "container:JSON.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
@@ -4,7 +4,7 @@
|
||||
<dict>
|
||||
<key>SchemeUserState</key>
|
||||
<dict>
|
||||
<key>JSON.xcscheme</key>
|
||||
<key>JSON.xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
|
||||
122
JSON/JSON.swift
Normal file
122
JSON/JSON.swift
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// JSON.swift
|
||||
// JSON
|
||||
//
|
||||
// Created by Zhigang Fang on 4/17/17.
|
||||
// Copyright © 2017 matrix. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
public struct JSON {
|
||||
|
||||
var value: Optional<NSObject>
|
||||
|
||||
public init(value: Optional<NSObject>) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public subscript(index: Int) -> JSON {
|
||||
get {
|
||||
return JSON(value: value.flatMap({ value in
|
||||
return (value as? NSArray).flatMap({
|
||||
if $0.count > index && index >= 0 {
|
||||
return $0[index] as? NSObject
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}))
|
||||
}
|
||||
set {
|
||||
let newValueToSet = newValue.value ?? NSNull()
|
||||
guard let array = self.value as? NSArray else {
|
||||
self.value = NSMutableArray(object: newValueToSet)
|
||||
return
|
||||
}
|
||||
let mutableArray: NSMutableArray
|
||||
if let array = array as? NSMutableArray {
|
||||
mutableArray = array
|
||||
} else {
|
||||
mutableArray = array.mutableCopy() as! NSMutableArray
|
||||
}
|
||||
guard (mutableArray.count > index && index >= 0) else { return }
|
||||
mutableArray.replaceObject(at: index, with: newValueToSet)
|
||||
self.value = mutableArray
|
||||
}
|
||||
}
|
||||
|
||||
public subscript(key: String) -> JSON {
|
||||
get {
|
||||
return JSON(value: value.flatMap({ value in
|
||||
return (value as? NSDictionary).flatMap({$0[key] as? NSObject})
|
||||
}))
|
||||
}
|
||||
set {
|
||||
let newValueToSet = newValue.value ?? NSNull()
|
||||
guard let dictionary = self.value as? NSDictionary else {
|
||||
self.value = NSMutableDictionary(object: newValueToSet, forKey: key as NSString)
|
||||
return
|
||||
}
|
||||
let mutableDictionary: NSMutableDictionary
|
||||
if let dictionary = dictionary as? NSMutableDictionary {
|
||||
mutableDictionary = dictionary
|
||||
} else {
|
||||
mutableDictionary = dictionary.mutableCopy() as! NSMutableDictionary
|
||||
}
|
||||
mutableDictionary.setValue(newValueToSet, forKeyPath: key as String)
|
||||
self.value = mutableDictionary
|
||||
}
|
||||
}
|
||||
|
||||
public var int: Int? {
|
||||
get {
|
||||
return self.value as? Int
|
||||
}
|
||||
set {
|
||||
self.value = newValue.flatMap({ $0 as NSObject})
|
||||
}
|
||||
}
|
||||
|
||||
public var float: Float? {
|
||||
get {
|
||||
return self.value as? Float
|
||||
}
|
||||
set {
|
||||
self.value = newValue.flatMap({ $0 as NSObject })
|
||||
}
|
||||
}
|
||||
|
||||
public var string: String? {
|
||||
get {
|
||||
return self.value as? String
|
||||
}
|
||||
set {
|
||||
self.value = newValue.flatMap({ $0 as NSObject })
|
||||
}
|
||||
}
|
||||
|
||||
public var bool: Bool? {
|
||||
get {
|
||||
return self.value as? Bool
|
||||
}
|
||||
set {
|
||||
self.value = newValue.flatMap({ $0 as NSObject })
|
||||
}
|
||||
}
|
||||
|
||||
public var array: Array<JSON>? {
|
||||
return (self.value as? NSArray).map({ array in
|
||||
array.map({JSON(value: $0 as? NSObject)})
|
||||
})
|
||||
}
|
||||
|
||||
public var dictionary: Array<(String, JSON)>? {
|
||||
return (self.value as? NSDictionary).map({ dictionary in
|
||||
dictionary.flatMap({ key, value in
|
||||
guard let key = key as? String else { return nil }
|
||||
|
||||
return (key, JSON(value: value as? NSObject))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user