Skip to content

Commit 1eb1236

Browse files
authored
Merge pull request #7 from chrisschlitt/feature/linux-support
Linux Support
2 parents e6d6437 + 505e253 commit 1eb1236

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ JSONPatch is a a swift module implements json-patch [RFC6902](https://tools.ietf
88
The implementation uses the [JSON Patch Tests](https://github.com/json-patch/json-patch-tests) project for unit tests to validate its correctness.
99

1010
# Release
11-
1.0.5 - Updated minimum deployment targets in podspac for compatibility with Xcode 15.
11+
1.0.6 - Added support for Linux.
1212

1313
# Installation
1414

RMJSONPatch.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "RMJSONPatch"
19-
s.version = "1.0.5"
19+
s.version = "1.0.6"
2020
s.summary = "JSONPatch is a swift library for applying and generating RFC-6902 compliant JSON patches."
2121
s.module_name = "JSONPatch"
2222

Sources/JSONPatch/JSONElement.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ extension JSONElement {
158158
private mutating func makeMutable() {
159159
switch self {
160160
case .object(let dictionary):
161+
#if os(Linux)
162+
let mutable = dictionary.mutableCopy() as! NSMutableDictionary
163+
#else
161164
let mutable = NSMutableDictionary(dictionary: dictionary)
165+
#endif
162166
self = .mutableObject(value: mutable)
163167
case .array(let array):
164168
let mutable = NSMutableArray(array: array)

Sources/JSONPatch/JSONEquality.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ protocol JSONEquatable {
2626

2727
extension NSNumber: JSONEquatable {
2828
var isBoolean: Bool {
29+
#if os(Linux)
30+
return objCType.pointee == 0x63 // character code for 'c'
31+
#else
2932
return CFNumberGetType(self) == .charType
33+
#endif
3034
}
3135

3236
func isJSONEquals(to element: JSONElement) -> Bool {
@@ -55,6 +59,12 @@ extension NSString: JSONEquatable {
5559
}
5660
}
5761

62+
extension String: JSONEquatable {
63+
func isJSONEquals(to element: JSONElement) -> Bool {
64+
return (self as NSString).isJSONEquals(to: element)
65+
}
66+
}
67+
5868
extension NSNull: JSONEquatable {
5969
func isJSONEquals(to element: JSONElement) -> Bool {
6070
guard case .null = element else {
@@ -85,6 +95,12 @@ extension NSArray: JSONEquatable {
8595
}
8696
}
8797

98+
extension Array: JSONEquatable {
99+
func isJSONEquals(to element: JSONElement) -> Bool {
100+
return (self as NSArray).isJSONEquals(to: element)
101+
}
102+
}
103+
88104
extension NSDictionary: JSONEquatable {
89105
func isJSONEquals(to element: JSONElement) -> Bool {
90106
switch element {
@@ -108,3 +124,9 @@ extension NSDictionary: JSONEquatable {
108124
}
109125
}
110126
}
127+
128+
extension Dictionary: JSONEquatable {
129+
func isJSONEquals(to element: JSONElement) -> Bool {
130+
return (self as NSDictionary).isJSONEquals(to: element)
131+
}
132+
}

Sources/JSONPatch/JSONPatchCodable.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ extension JSONPatch.Operation: Codable {
154154
extension SingleValueEncodingContainer {
155155

156156
fileprivate mutating func encodeNSNumber(_ value: NSNumber) throws {
157+
#if os(Linux)
158+
switch value.objCType.pointee {
159+
case 0x63:
160+
try encode(value.boolValue)
161+
case 0x64, 0x71, 0x51, 0x4C:
162+
try encode(value.doubleValue)
163+
case 0x66, 0x49:
164+
try encode(value.floatValue)
165+
default:
166+
try encode(value.int64Value)
167+
}
168+
#else
157169
switch CFNumberGetType(value) {
158170
case .charType:
159171
try encode(value.boolValue)
@@ -164,6 +176,7 @@ extension SingleValueEncodingContainer {
164176
default:
165177
try encode(value.int64Value)
166178
}
179+
#endif
167180
}
168181
}
169182

Sources/JSONPatch/NSDictionary+DeepCopy.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ extension NSDictionary {
2626
let result = NSMutableDictionary()
2727

2828
self.enumerateKeysAndObjects { (key, value, stop) in
29+
#if os(Linux)
30+
switch value {
31+
case let array as NSArray:
32+
result.setObject(array.deepMutableCopy(), forKey: key as! NSString)
33+
case let dict as NSDictionary:
34+
result.setObject(dict.deepMutableCopy(), forKey: key as! NSString)
35+
case let str as NSMutableString:
36+
result.setObject(str, forKey: key as! NSString)
37+
case let obj as NSObject:
38+
result.setObject(obj.copy(), forKey: key as! NSString)
39+
default:
40+
result.setObject(value, forKey: key as! NSString)
41+
}
42+
#else
2943
switch value {
3044
case let array as NSArray:
3145
result[key] = array.deepMutableCopy()
@@ -38,6 +52,7 @@ extension NSDictionary {
3852
default:
3953
result[key] = value
4054
}
55+
#endif
4156
}
4257

4358
return result

0 commit comments

Comments
 (0)