Skip to content

Commit

Permalink
Prefix Encoder and Decoder with PS
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett committed Jun 24, 2020
1 parent ddb2753 commit a5eab9c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ Use it as you would use the Foundation encoder and decoder.
```swift
import PureSwiftJSON

let bytesArray = try JSONEncoder().encode(myEncodable)
let myDecodable = try JSONDecoder().decode(MyDecodable.self, from: bytes)
let bytesArray = try PSJSONEncoder().encode(myEncodable)
let myDecodable = try PSJSONDecoder().decode(MyDecodable.self, from: bytes)
```

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

extension PureSwiftJSON.JSONEncoder: ContentEncoder {
extension PSJSONEncoder: ContentEncoder {
public func encode<E: Encodable>(
_ encodable: E,
to body: inout ByteBuffer,
Expand All @@ -96,7 +96,7 @@ extension PureSwiftJSON.JSONEncoder: ContentEncoder {
}
}

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

```swift
let decoder = PureSwiftJSON.JSONDecoder()
let decoder = PSJSONDecoder()
ContentConfiguration.global.use(decoder: decoder, for: .json)

let encoder = PureSwiftJSON.JSONEncoder()
let encoder = PSJSONEncoder()
ContentConfiguration.global.use(encoder: encoder, for: .json)
```

Expand Down
2 changes: 1 addition & 1 deletion Sources/PureSwiftJSON/Decoding/JSONDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

public struct JSONDecoder {
public struct PSJSONDecoder {
@usableFromInline var userInfo: [CodingUserInfoKey: Any] = [:]

public init() {}
Expand Down
2 changes: 1 addition & 1 deletion Sources/PureSwiftJSON/Encoding/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class JSONObject {
}
}

public struct JSONEncoder {
public struct PSJSONEncoder {
var userInfo: [CodingUserInfoKey: Any] = [:]

public init() {}
Expand Down
4 changes: 2 additions & 2 deletions Tests/PureSwiftJSONTests/DateCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DateCodingTests: XCTestCase {
do {
let dateString = "2020-03-18T13:11:10.000Z"
let json = #"{"eventTime": "\#(dateString)"}"#
let result = try PureSwiftJSON.JSONDecoder().decode(MyEvent.self, from: [UInt8](json.utf8))
let result = try PSJSONDecoder().decode(MyEvent.self, from: [UInt8](json.utf8))

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

let event = MyEvent(eventTime: components.date!)
let bytes = try PureSwiftJSON.JSONEncoder().encode(event)
let bytes = try PSJSONEncoder().encode(event)
XCTAssertEqual(String(decoding: bytes, as: Unicode.UTF8.self), json)
} catch {
XCTFail("Unexpected error: \(error)")
Expand Down
8 changes: 4 additions & 4 deletions Tests/PureSwiftJSONTests/Decoding/JSONDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class JSONDecoderTests: XCTestCase {
}
"""

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

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

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

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

let json = #"{"hello👩‍👩‍👧‍👧" 123 }"#
XCTAssertThrowsError(_ = try PureSwiftJSON.JSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
XCTAssertThrowsError(_ = try PSJSONDecoder().decode(HelloWorld.self, from: json.data(using: .utf8)!)) {
error in
guard case let Swift.DecodingError.dataCorrupted(context) = error else {
XCTFail("Unexpected error: \(error)"); return
Expand Down
22 changes: 11 additions & 11 deletions Tests/PureSwiftJSONTests/Encoding/JSONEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JSONEncoderTests: XCTestCase {
func testEncodeHelloWorld() {
let hello = HelloWorld(hello: "world")
var result: [UInt8]?
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(hello))
XCTAssertNoThrow(result = try PSJSONEncoder().encode(hello))
var parsed: JSONValue?
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(result)))

Expand All @@ -26,30 +26,30 @@ class JSONEncoderTests: XCTestCase {

func testEncodeTopLevel12() {
var result: [UInt8]?
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(12))
XCTAssertNoThrow(result = try PSJSONEncoder().encode(12))
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), "12")
}

func testEncodeTopLevelTrue() {
var result: [UInt8]?
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(true))
XCTAssertNoThrow(result = try PSJSONEncoder().encode(true))
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: .utf8), "true")
}

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

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

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

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

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

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

func testEncodeQuote() {
var result: [UInt8]?
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode("\""))
XCTAssertNoThrow(result = try PSJSONEncoder().encode("\""))
XCTAssertEqual(String(bytes: try XCTUnwrap(result), encoding: String.Encoding.utf8), "\"\\\"\"")
}

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

var result: [UInt8]?
XCTAssertNoThrow(result = try PureSwiftJSON.JSONEncoder().encode(object))
XCTAssertNoThrow(result = try PSJSONEncoder().encode(object))
var parsed: JSONValue?
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(result)))
XCTAssertEqual(parsed, .object(["sub": .object(["key": .string("sub"), "value": .number("12")])]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class JSONKeyedEncodingContainerTests: XCTestCase {

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

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

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

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

do {
let object = Object(firstName: "Adam", surname: "Fowler")
let json = try PureSwiftJSON.JSONEncoder().encode(object)
let json = try PSJSONEncoder().encode(object)

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

do {
let object = NumberStruct()
let json = try PureSwiftJSON.JSONEncoder().encode(object)
let json = try PSJSONEncoder().encode(object)

let parsed = try JSONParser().parse(bytes: json)
XCTAssertEqual(parsed, .object(["numbers": .array([.number("1"), .number("2"), .number("3"), .number("4")])]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JSONSingleValueEncodingContainerTests: XCTestCase {

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

var parsed: JSONValue?
XCTAssertNoThrow(parsed = try JSONParser().parse(bytes: XCTUnwrap(json)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JSONUnkeyedEncodingContainerTests: XCTestCase {

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

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

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

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

do {
let object = ObjectInArray(firstName: "Adam", surname: "Fowler")
let json = try PureSwiftJSON.JSONEncoder().encode(object)
let json = try PSJSONEncoder().encode(object)

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

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

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

0 comments on commit a5eab9c

Please sign in to comment.