Skip to content

Commit 64f6532

Browse files
authored
Prefix Encoder and Decoder with PS (#48)
1 parent ddb2753 commit 64f6532

File tree

10 files changed

+43
-43
lines changed

10 files changed

+43
-43
lines changed

PerfTests/Sources/CodingPerfTests/main.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func timing(name: String, execute: () throws -> Void) rethrows -> TimeInterval {
2020

2121
let sampleString = SampleStructure.sampleJSON
2222
let sampleBytes = [UInt8](sampleString.utf8)
23-
let sampleStruct = try PureSwiftJSON.JSONDecoder().decode([SampleStructure].self, from: sampleBytes)
23+
let sampleStruct = try PSJSONDecoder().decode([SampleStructure].self, from: sampleBytes)
2424
let sampleJSON = try JSONParser().parse(bytes: sampleBytes)
2525

2626
print("Number of invocations: \(runs)")
@@ -39,7 +39,7 @@ let toBytes = timing(name: "PureSwift ") {
3939
print("------------------------------------------")
4040
print("Encoding")
4141

42-
let foundationEncoder = Foundation.JSONEncoder()
42+
let foundationEncoder = JSONEncoder()
4343
let foundationEncoding = try timing(name: "Foundation ") {
4444
for _ in 1 ... runs {
4545
_ = try foundationEncoder.encode(sampleStruct)
@@ -53,7 +53,7 @@ let ikigaEncoding = try timing(name: "Ikiga ") {
5353
}
5454
}
5555

56-
let pureEncoder = PureSwiftJSON.JSONEncoder()
56+
let pureEncoder = PSJSONEncoder()
5757
let pureEncoding = try timing(name: "PureSwift ") {
5858
for _ in 1 ... runs {
5959
_ = try pureEncoder.encode(sampleStruct)
@@ -128,7 +128,7 @@ let pureParsingBuffer = try timing(name: "PureSwift on NIO.ByteBuffer ") {
128128
print("------------------------------------------")
129129
print("Decoding")
130130

131-
let foundationDecoder = Foundation.JSONDecoder()
131+
let foundationDecoder = JSONDecoder()
132132
let foundationDecoding = try timing(name: "Foundation on Foundation.Data") {
133133
for _ in 1 ... runs {
134134
_ = try foundationDecoder.decode([SampleStructure].self, from: sampleData)
@@ -165,7 +165,7 @@ let ikigaDecodingBuffer = try timing(name: "IkigaJSON on NIO.ByteBuffer ") {
165165
}
166166
}
167167

168-
let pureDecoder = PureSwiftJSON.JSONDecoder()
168+
let pureDecoder = PSJSONDecoder()
169169
let pureDecoding = try timing(name: "PureSwift on [UInt8] ") {
170170
for _ in 1 ... runs {
171171
_ = try pureDecoder.decode([SampleStructure].self, from: sampleBytes)

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Use it as you would use the Foundation encoder and decoder.
5454
```swift
5555
import PureSwiftJSON
5656

57-
let bytesArray = try JSONEncoder().encode(myEncodable)
58-
let myDecodable = try JSONDecoder().decode(MyDecodable.self, from: bytes)
57+
let bytesArray = try PSJSONEncoder().encode(myEncodable)
58+
let myDecodable = try PSJSONDecoder().decode(MyDecodable.self, from: bytes)
5959
```
6060

6161
### Use with SwiftNIO ByteBuffer
@@ -83,7 +83,7 @@ Increase the performance of your Vapor 4 API by using `pure-swift-json` instead
8383
import Vapor
8484
import PureSwiftJSON
8585

86-
extension PureSwiftJSON.JSONEncoder: ContentEncoder {
86+
extension PSJSONEncoder: ContentEncoder {
8787
public func encode<E: Encodable>(
8888
_ encodable: E,
8989
to body: inout ByteBuffer,
@@ -96,7 +96,7 @@ extension PureSwiftJSON.JSONEncoder: ContentEncoder {
9696
}
9797
}
9898

99-
extension PureSwiftJSON.JSONDecoder: ContentDecoder {
99+
extension PSJSONDecoder: ContentDecoder {
100100
public func decode<D: Decodable>(
101101
_ decodable: D.Type,
102102
from body: ByteBuffer,
@@ -114,10 +114,10 @@ extension PureSwiftJSON.JSONDecoder: ContentDecoder {
114114
Next, register the encoder and decoder for use in Vapor:
115115

116116
```swift
117-
let decoder = PureSwiftJSON.JSONDecoder()
117+
let decoder = PSJSONDecoder()
118118
ContentConfiguration.global.use(decoder: decoder, for: .json)
119119

120-
let encoder = PureSwiftJSON.JSONEncoder()
120+
let encoder = PSJSONEncoder()
121121
ContentConfiguration.global.use(encoder: encoder, for: .json)
122122
```
123123

Sources/PureSwiftJSON/Decoding/JSONDecoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
public struct JSONDecoder {
2+
public struct PSJSONDecoder {
33
@usableFromInline var userInfo: [CodingUserInfoKey: Any] = [:]
44

55
public init() {}

Sources/PureSwiftJSON/Encoding/JSONEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class JSONObject {
9595
}
9696
}
9797

98-
public struct JSONEncoder {
98+
public struct PSJSONEncoder {
9999
var userInfo: [CodingUserInfoKey: Any] = [:]
100100

101101
public init() {}

Tests/PureSwiftJSONTests/DateCodingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DateCodingTests: XCTestCase {
4646
do {
4747
let dateString = "2020-03-18T13:11:10.000Z"
4848
let json = #"{"eventTime": "\#(dateString)"}"#
49-
let result = try PureSwiftJSON.JSONDecoder().decode(MyEvent.self, from: [UInt8](json.utf8))
49+
let result = try PSJSONDecoder().decode(MyEvent.self, from: [UInt8](json.utf8))
5050

5151
let components = DateComponents(
5252
calendar: Calendar(identifier: .gregorian), timeZone: TimeZone(secondsFromGMT: 0),
@@ -69,7 +69,7 @@ class DateCodingTests: XCTestCase {
6969
)
7070

7171
let event = MyEvent(eventTime: components.date!)
72-
let bytes = try PureSwiftJSON.JSONEncoder().encode(event)
72+
let bytes = try PSJSONEncoder().encode(event)
7373
XCTAssertEqual(String(decoding: bytes, as: Unicode.UTF8.self), json)
7474
} catch {
7575
XCTFail("Unexpected error: \(error)")

Tests/PureSwiftJSONTests/Decoding/JSONDecoderTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class JSONDecoderTests: XCTestCase {
3232
}
3333
"""
3434

35-
let result = try JSONDecoder().decode(HelloWorld.self, from: [UInt8](string.utf8))
35+
let result = try PSJSONDecoder().decode(HelloWorld.self, from: [UInt8](string.utf8))
3636

3737
XCTAssertEqual(result.hello, "world")
3838
XCTAssertEqual(result.subStruct, HelloWorld.SubStruct(name: "hihi"))
@@ -53,7 +53,7 @@ class JSONDecoderTests: XCTestCase {
5353
}
5454

5555
let json = #"{"hello":"world"}"#
56-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
56+
XCTAssertThrowsError(_ = try PSJSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
5757
error in
5858
guard case let Swift.DecodingError.typeMismatch(type, context) = error else {
5959
XCTFail("Unexpected error: \(error)"); return
@@ -77,7 +77,7 @@ class JSONDecoderTests: XCTestCase {
7777
}
7878

7979
let json = #"["haha", "hihi"]"#
80-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
80+
XCTAssertThrowsError(_ = try PSJSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
8181
error in
8282
guard case let Swift.DecodingError.typeMismatch(type, context) = error else {
8383
XCTFail("Unexpected error: \(error)"); return
@@ -100,7 +100,7 @@ class JSONDecoderTests: XCTestCase {
100100
}
101101

102102
let json = #"{"hello👩‍👩‍👧‍👧" 123 }"#
103-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
103+
XCTAssertThrowsError(_ = try PSJSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
104104
error in
105105
guard case let Swift.DecodingError.dataCorrupted(context) = error else {
106106
XCTFail("Unexpected error: \(error)"); return

Tests/PureSwiftJSONTests/Encoding/JSONEncoderTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class JSONEncoderTests: XCTestCase {
1212
func testEncodeHelloWorld() {
1313
let hello = HelloWorld(hello: "world")
1414
var result: [UInt8]?
15-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(hello))
15+
XCTAssertNoThrow(result = try PSJSONEncoder().encode(hello))
1616
var parsed: JSONValue?
1717
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(result)))
1818

@@ -26,30 +26,30 @@ class JSONEncoderTests: XCTestCase {
2626

2727
func testEncodeTopLevel12() {
2828
var result: [UInt8]?
29-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(12))
29+
XCTAssertNoThrow(result = try PSJSONEncoder().encode(12))
3030
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), "12")
3131
}
3232

3333
func testEncodeTopLevelTrue() {
3434
var result: [UInt8]?
35-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(true))
35+
XCTAssertNoThrow(result = try PSJSONEncoder().encode(true))
3636
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), "true")
3737
}
3838

3939
func testEncodeTopLevelNull() {
4040
var result: [UInt8]?
41-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(nil as String?))
41+
XCTAssertNoThrow(result = try PSJSONEncoder().encode(nil as String?))
4242
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), "null")
4343
}
4444

4545
func testEncodeTopLevelString() {
4646
var result: [UInt8]?
47-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode("Hello World"))
47+
XCTAssertNoThrow(result = try PSJSONEncoder().encode("Hello World"))
4848
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), #""Hello World""#)
4949
}
5050

5151
func testEncodeDoubleNAN() {
52-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONEncoder().encode(Double.nan)) { error in
52+
XCTAssertThrowsError(_ = try PSJSONEncoder().encode(Double.nan)) { error in
5353
guard case let Swift.EncodingError.invalidValue(value as Double, context) = error else {
5454
XCTFail("Unexpected error: \(error)"); return
5555
}
@@ -61,7 +61,7 @@ class JSONEncoderTests: XCTestCase {
6161
}
6262

6363
func testEncodeDoubleInf() {
64-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONEncoder().encode(Double.infinity)) { error in
64+
XCTAssertThrowsError(_ = try PSJSONEncoder().encode(Double.infinity)) { error in
6565
guard case let Swift.EncodingError.invalidValue(value as Double, context) = error else {
6666
XCTFail("Unexpected error: \(error)"); return
6767
}
@@ -73,7 +73,7 @@ class JSONEncoderTests: XCTestCase {
7373
}
7474

7575
func testEncodeFloatNAN() {
76-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONEncoder().encode(Float.nan)) { error in
76+
XCTAssertThrowsError(_ = try PSJSONEncoder().encode(Float.nan)) { error in
7777
guard case let Swift.EncodingError.invalidValue(value as Float, context) = error else {
7878
XCTFail("Unexpected error: \(error)"); return
7979
}
@@ -85,7 +85,7 @@ class JSONEncoderTests: XCTestCase {
8585
}
8686

8787
func testEncodeFloatInf() {
88-
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONEncoder().encode(Float.infinity)) { error in
88+
XCTAssertThrowsError(_ = try PSJSONEncoder().encode(Float.infinity)) { error in
8989
guard case let Swift.EncodingError.invalidValue(value as Float, context) = error else {
9090
XCTFail("Unexpected error: \(error)"); return
9191
}
@@ -98,7 +98,7 @@ class JSONEncoderTests: XCTestCase {
9898

9999
func testEncodeQuote() {
100100
var result: [UInt8]?
101-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode("\""))
101+
XCTAssertNoThrow(result = try PSJSONEncoder().encode("\""))
102102
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: String.Encoding.utf8), "\"\\\"\"")
103103
}
104104

@@ -127,7 +127,7 @@ class JSONEncoderTests: XCTestCase {
127127
let object = Object(sub: SubObject(value: 12))
128128

129129
var result: [UInt8]?
130-
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(object))
130+
XCTAssertNoThrow(result = try PSJSONEncoder().encode(object))
131131
var parsed: JSONValue?
132132
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(result)))
133133
XCTAssertEqual(parsed, .object(["sub": .object(["key": .string("sub"), "value": .number("12")])]))

Tests/PureSwiftJSONTests/Encoding/JSONKeyedEncodingContainerTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
1010

1111
func testEncodeDoubleNAN() {
1212
do {
13-
let result = try PureSwiftJSON.JSONEncoder().encode(DoubleBox(number: .nan))
13+
let result = try PSJSONEncoder().encode(DoubleBox(number: .nan))
1414
XCTFail("Did not expect to have a result: \(result)")
1515
} catch let Swift.EncodingError.invalidValue(value as Double, context) {
1616
XCTAssert(value.isNaN) // expected
@@ -24,7 +24,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
2424

2525
func testEncodeDoubleInf() {
2626
do {
27-
let result = try PureSwiftJSON.JSONEncoder().encode(DoubleBox(number: .infinity))
27+
let result = try PSJSONEncoder().encode(DoubleBox(number: .infinity))
2828
XCTFail("Did not expect to have a result: \(result)")
2929
} catch let Swift.EncodingError.invalidValue(value as Double, context) {
3030
XCTAssert(value.isInfinite) // expected
@@ -43,7 +43,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
4343

4444
func testEncodeFloatNAN() {
4545
do {
46-
let result = try PureSwiftJSON.JSONEncoder().encode(FloatBox(number: .nan))
46+
let result = try PSJSONEncoder().encode(FloatBox(number: .nan))
4747
XCTFail("Did not expect to have a result: \(result)")
4848
} catch let Swift.EncodingError.invalidValue(value as Float, context) {
4949
XCTAssert(value.isNaN) // expected
@@ -57,7 +57,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
5757

5858
func testEncodeFloatInf() {
5959
do {
60-
let result = try PureSwiftJSON.JSONEncoder().encode(FloatBox(number: .infinity))
60+
let result = try PSJSONEncoder().encode(FloatBox(number: .infinity))
6161
XCTFail("Did not expect to have a result: \(result)")
6262
} catch let Swift.EncodingError.invalidValue(value as Float, context) {
6363
XCTAssert(value.isInfinite) // expected
@@ -98,7 +98,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
9898

9999
do {
100100
let object = Object(firstName: "Adam", surname: "Fowler")
101-
let json = try PureSwiftJSON.JSONEncoder().encode(object)
101+
let json = try PSJSONEncoder().encode(object)
102102

103103
let parsed = try JSONParser().parse(bytes: json)
104104
XCTAssertEqual(parsed, .object(["name": .object(["firstName": .string("Adam"), "surname": .string("Fowler")])]))
@@ -127,7 +127,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {
127127

128128
do {
129129
let object = NumberStruct()
130-
let json = try PureSwiftJSON.JSONEncoder().encode(object)
130+
let json = try PSJSONEncoder().encode(object)
131131

132132
let parsed = try JSONParser().parse(bytes: json)
133133
XCTAssertEqual(parsed, .object(["numbers": .array([.number("1"), .number("2"), .number("3"), .number("4")])]))

Tests/PureSwiftJSONTests/Encoding/JSONSingleValueEncodingContainerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class JSONSingleValueEncodingContainerTests: XCTestCase {
1919

2020
let object = Object(name: Name(firstName: "Adam", surname: "Fowler"))
2121
var json: [UInt8]?
22-
XCTAssertNoThrow(json = try PureSwiftJSON.JSONEncoder().encode(object))
22+
XCTAssertNoThrow(json = try PSJSONEncoder().encode(object))
2323

2424
var parsed: JSONValue?
2525
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(json)))

Tests/PureSwiftJSONTests/Encoding/JSONUnkeyedEncodingContainerTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
1515

1616
func testEncodeDoubleNAN() {
1717
do {
18-
let result = try PureSwiftJSON.JSONEncoder().encode(DoubleInArrayBox(number: .nan))
18+
let result = try PSJSONEncoder().encode(DoubleInArrayBox(number: .nan))
1919
XCTFail("Did not expect to have a result: \(result)")
2020
} catch let Swift.EncodingError.invalidValue(value as Double, context) {
2121
XCTAssert(value.isNaN) // expected
@@ -29,7 +29,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
2929

3030
func testEncodeDoubleInf() {
3131
do {
32-
let result = try PureSwiftJSON.JSONEncoder().encode(DoubleInArrayBox(number: .infinity))
32+
let result = try PSJSONEncoder().encode(DoubleInArrayBox(number: .infinity))
3333
XCTFail("Did not expect to have a result: \(result)")
3434
} catch let Swift.EncodingError.invalidValue(value as Double, context) {
3535
XCTAssert(value.isInfinite) // expected
@@ -53,7 +53,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
5353

5454
func testEncodeFloatNAN() {
5555
do {
56-
let result = try PureSwiftJSON.JSONEncoder().encode(FloatInArrayBox(number: .nan))
56+
let result = try PSJSONEncoder().encode(FloatInArrayBox(number: .nan))
5757
XCTFail("Did not expect to have a result: \(result)")
5858
} catch let Swift.EncodingError.invalidValue(value as Float, context) {
5959
XCTAssert(value.isNaN) // expected
@@ -67,7 +67,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
6767

6868
func testEncodeFloatInf() {
6969
do {
70-
let result = try PureSwiftJSON.JSONEncoder().encode(FloatInArrayBox(number: .infinity))
70+
let result = try PSJSONEncoder().encode(FloatInArrayBox(number: .infinity))
7171
XCTFail("Did not expect to have a result: \(result)")
7272
} catch let Swift.EncodingError.invalidValue(value as Float, context) {
7373
XCTAssert(value.isInfinite) // expected
@@ -102,7 +102,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
102102

103103
do {
104104
let object = ObjectInArray(firstName: "Adam", surname: "Fowler")
105-
let json = try PureSwiftJSON.JSONEncoder().encode(object)
105+
let json = try PSJSONEncoder().encode(object)
106106

107107
let parsed = try JSONParser().parse(bytes: json)
108108
XCTAssertEqual(parsed, .array([.object(["firstName": .string("Adam"), "surname": .string("Fowler")])]))
@@ -128,7 +128,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {
128128

129129
do {
130130
let object = NumbersInArray(numbers: [1, 2, 3, 4])
131-
let json = try PureSwiftJSON.JSONEncoder().encode(object)
131+
let json = try PSJSONEncoder().encode(object)
132132

133133
let parsed = try JSONParser().parse(bytes: json)
134134
XCTAssertEqual(parsed, .array([.array([.number("1"), .number("2"), .number("3"), .number("4")])]))

0 commit comments

Comments
 (0)