-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace AnyCodable with custom JSONValue
- Loading branch information
1 parent
55071da
commit 5da9787
Showing
5 changed files
with
204 additions
and
30 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import Foundation | ||
|
||
public enum JSONValue: Codable, Hashable, Sendable { | ||
case null | ||
case bool(Bool) | ||
case number(Double) | ||
case string(String) | ||
case array([JSONValue]) | ||
case hash([String: JSONValue]) | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
|
||
switch self { | ||
case .null: | ||
try container.encodeNil() | ||
case .bool(let value): | ||
try container.encode(value) | ||
case .number(let value): | ||
try container.encode(value) | ||
case .string(let value): | ||
try container.encode(value) | ||
case .array(let value): | ||
try container.encode(value) | ||
case .hash(let value): | ||
try container.encode(value) | ||
} | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let single = try? decoder.singleValueContainer() | ||
|
||
if let value = try? single?.decode([String: JSONValue].self) { | ||
self = .hash(value) | ||
return | ||
} | ||
|
||
if let value = try? single?.decode([JSONValue].self) { | ||
self = .array(value) | ||
return | ||
} | ||
|
||
if let value = try? single?.decode(String.self) { | ||
self = .string(value) | ||
return | ||
} | ||
|
||
if let value = try? single?.decode(Double.self) { | ||
self = .number(value) | ||
return | ||
} | ||
|
||
if let value = try? single?.decode(Bool.self) { | ||
self = .bool(value) | ||
return | ||
} | ||
|
||
if single?.decodeNil() == true { | ||
self = .null | ||
return | ||
} | ||
|
||
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "failed to decode JSON object")) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByNilLiteral { | ||
public init(nilLiteral: ()) { | ||
self = .null | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByDictionaryLiteral { | ||
public init(dictionaryLiteral elements: (String, JSONValue)...) { | ||
var hash = [String: JSONValue]() | ||
|
||
for element in elements { | ||
hash[element.0] = element.1 | ||
} | ||
|
||
self = .hash(hash) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByStringLiteral { | ||
public init(stringLiteral: String) { | ||
self = .string(stringLiteral) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByIntegerLiteral { | ||
public init(integerLiteral value: IntegerLiteralType) { | ||
self = .number(Double(value)) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByFloatLiteral { | ||
public init(floatLiteral value: FloatLiteralType) { | ||
self = .number(value) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByArrayLiteral { | ||
public init(arrayLiteral elements: JSONValue...) { | ||
var array = [JSONValue]() | ||
|
||
for element in elements { | ||
array.append(element) | ||
} | ||
|
||
self = .array(array) | ||
} | ||
} | ||
|
||
extension JSONValue: ExpressibleByBooleanLiteral { | ||
public init(booleanLiteral value: BooleanLiteralType) { | ||
self = .bool(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import XCTest | ||
import JSONRPC | ||
|
||
final class JSONValueTests: XCTestCase { | ||
func testNullEncoding() throws { | ||
let obj = JSONValue.null | ||
|
||
let data = try JSONEncoder().encode(obj) | ||
let string = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
|
||
XCTAssertEqual(string, "null") | ||
} | ||
|
||
func testBoolEncoding() throws { | ||
let obj = JSONValue.bool(true) | ||
|
||
let data = try JSONEncoder().encode(obj) | ||
let string = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
|
||
XCTAssertEqual(string, "true") | ||
} | ||
|
||
func testIntEncoding() throws { | ||
let obj = JSONValue.number(45) | ||
|
||
let data = try JSONEncoder().encode(obj) | ||
let string = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
|
||
XCTAssertEqual(string, "45") | ||
} | ||
|
||
func testArrayEncoding() throws { | ||
let obj = JSONValue.array([1,2,3]) | ||
|
||
let data = try JSONEncoder().encode(obj) | ||
let string = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
|
||
XCTAssertEqual(string, "[1,2,3]") | ||
} | ||
|
||
func testNullInDictionary() throws { | ||
let obj = JSONValue.hash(["abc": nil]) | ||
|
||
let data = try JSONEncoder().encode(obj) | ||
let string = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
|
||
XCTAssertEqual(string, "{\"abc\":null}") | ||
} | ||
|
||
func testDecoding() throws { | ||
let string = """ | ||
{ | ||
"string": "abc", | ||
"bool": true, | ||
"null": null, | ||
"int": 145, | ||
"double": 145.0, | ||
"array": [1,2,3] | ||
} | ||
""" | ||
let value = try JSONDecoder().decode(JSONValue.self, from: string.data(using: .utf8)!) | ||
|
||
let expected: JSONValue = [ | ||
"string": "abc", | ||
"bool": true, | ||
"null": nil, | ||
"int": 145, | ||
"double": 145.0, | ||
"array": [1,2,3] | ||
] | ||
XCTAssertEqual(value, expected) | ||
} | ||
} |