Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 82c88b9

Browse files
committed
added JSONSerializationType
support for multiple serialization engines
1 parent 98327f9 commit 82c88b9

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

Sources/FoundationConvertible/NSJSONSerialization.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public extension JSON.Value {
2929
}
3030

3131
/// Serializes the JSON into a string.
32-
public func toString() -> Swift.String? {
32+
public func toString(options: NSJSONWritingOptions = NSJSONWritingOptions.PrettyPrinted) -> Swift.String? {
3333

34-
guard let data = self.toData()
34+
guard let data = self.toData(options)
3535
else { return nil }
3636

3737
return Swift.String(UTF8Data: Data(foundation: data))!
3838
}
3939

4040
/// Serializes the JSON into data.
41-
public func toData() -> NSData? {
41+
public func toData(options: NSJSONWritingOptions = NSJSONWritingOptions.PrettyPrinted) -> NSData? {
4242

4343
switch self {
4444

@@ -47,8 +47,7 @@ public extension JSON.Value {
4747
default: return nil
4848
}
4949

50-
return try! NSJSONSerialization.dataWithJSONObject(self.toFoundation().rawValue, options: NSJSONWritingOptions.PrettyPrinted)
51-
50+
return try! NSJSONSerialization.dataWithJSONObject(self.toFoundation().rawValue, options: options)
5251
}
5352
}
5453

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// JSONSerialization.swift
3+
// SwiftFoundation
4+
//
5+
// Created by Alsey Coleman Miller on 12/19/15.
6+
// Copyright © 2015 PureSwift. All rights reserved.
7+
//
8+
9+
/// JSON Serialization engine type.
10+
public protocol JSONSerializationType {
11+
12+
typealias WritingOptions
13+
14+
/// Serializes a ```JSON.Object``` to
15+
static func serialize(object: JSON.Object, options: WritingOptions) -> String
16+
17+
static func serialize(array: JSON.Array, options: WritingOptions) -> String
18+
19+
static func parse(JSONString: String) -> JSON.Value?
20+
}
21+
22+
public extension JSON.Value {
23+
24+
/// Deserialize JSON from a string.
25+
public init?<T: JSONSerializationType>(string: Swift.String, _ engine: T.Type) {
26+
27+
guard let parsedJSON = engine.parse(string)
28+
else { return nil }
29+
30+
self = parsedJSON
31+
}
32+
33+
/// Convenience method for serializing JSON.
34+
public func toString<T: JSONSerializationType>(options: T.WritingOptions, _ engine: T.Type) -> Swift.String? {
35+
36+
switch self {
37+
38+
case let .Array(value): return engine.serialize(value, options: options)
39+
case let .Object(value): return engine.serialize(value, options: options)
40+
default: return nil
41+
}
42+
}
43+
}

Sources/SwiftFoundation/POSIXUUID.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ public func POSIXUUIDConvertStringToUUID(string: String) -> uuid_t? {
8787
}
8888

8989
return uuidPointer.memory
90-
}
90+
}

Xcode/SwiftFoundation.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
6E2C2C361C212C5700B2C995 /* NSString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E2C2C311C212C2200B2C995 /* NSString.swift */; };
219219
6E30BA111B40F543009B1B49 /* SwiftFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E30BA101B40F543009B1B49 /* SwiftFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; };
220220
6E30BA181B40F543009B1B49 /* SwiftFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E30BA0D1B40F543009B1B49 /* SwiftFoundation.framework */; };
221+
6EE7092F1C260A3900573EE4 /* JSONSerialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE7092E1C260A3900573EE4 /* JSONSerialization.swift */; };
222+
6EE709301C260A3900573EE4 /* JSONSerialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE7092E1C260A3900573EE4 /* JSONSerialization.swift */; };
223+
6EE709311C260A3900573EE4 /* JSONSerialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE7092E1C260A3900573EE4 /* JSONSerialization.swift */; };
221224
/* End PBXBuildFile section */
222225

223226
/* Begin PBXContainerItemProxy section */
@@ -343,6 +346,7 @@
343346
6E30BA121B40F543009B1B49 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
344347
6E30BA171B40F543009B1B49 /* SwiftFoundationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftFoundationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
345348
6E30BA1E1B40F543009B1B49 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
349+
6EE7092E1C260A3900573EE4 /* JSONSerialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONSerialization.swift; sourceTree = "<group>"; };
346350
/* End PBXFileReference section */
347351

348352
/* Begin PBXFrameworksBuildPhase section */
@@ -457,6 +461,7 @@
457461
6E2C2B2C1C20F6EB00B2C995 /* Formatter.swift */,
458462
6E2C2C281C20FC6B00B2C995 /* HTTP */,
459463
6E2C2B341C20F6EB00B2C995 /* JSON.swift */,
464+
6EE7092E1C260A3900573EE4 /* JSONSerialization.swift */,
460465
6E2C2B351C20F6EB00B2C995 /* JSONExtensions.swift */,
461466
6E2C2B361C20F6EB00B2C995 /* Null.swift */,
462467
6E2C2B371C20F6EB00B2C995 /* Order.swift */,
@@ -827,6 +832,7 @@
827832
6E2C2BDA1C20F76600B2C995 /* Task.swift in Sources */,
828833
6E2C2BB51C20F76600B2C995 /* ComparatorSortDescriptor.swift in Sources */,
829834
6E2C2BDD1C20F76600B2C995 /* URLClient.swift in Sources */,
835+
6EE709301C260A3900573EE4 /* JSONSerialization.swift in Sources */,
830836
6E2C2B681C20F6F600B2C995 /* NSURLSession.swift in Sources */,
831837
6E2C2BC41C20F76600B2C995 /* HTTPStatusCode.swift in Sources */,
832838
6E2C2BDB1C20F76600B2C995 /* Transformer.swift in Sources */,
@@ -894,6 +900,7 @@
894900
6E2C2BA61C20F76600B2C995 /* Task.swift in Sources */,
895901
6E2C2B811C20F76600B2C995 /* ComparatorSortDescriptor.swift in Sources */,
896902
6E2C2BA91C20F76600B2C995 /* URLClient.swift in Sources */,
903+
6EE709311C260A3900573EE4 /* JSONSerialization.swift in Sources */,
897904
6E2C2B5F1C20F6F500B2C995 /* NSURLSession.swift in Sources */,
898905
6E2C2B901C20F76600B2C995 /* HTTPStatusCode.swift in Sources */,
899906
6E2C2BA71C20F76600B2C995 /* Transformer.swift in Sources */,
@@ -981,6 +988,7 @@
981988
6E2C2C0E1C20F76700B2C995 /* Task.swift in Sources */,
982989
6E2C2BE91C20F76700B2C995 /* ComparatorSortDescriptor.swift in Sources */,
983990
6E2C2C111C20F76700B2C995 /* URLClient.swift in Sources */,
991+
6EE7092F1C260A3900573EE4 /* JSONSerialization.swift in Sources */,
984992
6E2C2B711C20F6F700B2C995 /* NSURLSession.swift in Sources */,
985993
6E2C2BF81C20F76700B2C995 /* HTTPStatusCode.swift in Sources */,
986994
6E2C2C0F1C20F76700B2C995 /* Transformer.swift in Sources */,

0 commit comments

Comments
 (0)