Skip to content

Commit

Permalink
Merge pull request #33 from orlandos-nl/feature/jo/fix-formfeed-backs…
Browse files Browse the repository at this point in the history
…pace

Fix support for backspace and formfeed characters
  • Loading branch information
Joannis authored Aug 3, 2023
2 parents 115092d + 13be32a commit 844b186
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/IkigaJSON/Codable/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fileprivate struct KeyedJSONDecodingContainer<Key: CodingKey>: KeyedDecodingCont
func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { return try decodeInt(type, forKey: key) }
func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { return try decodeInt(type, forKey: key) }

private func subDecoder<Key: CodingKey>(forKey key: Key, orThrow failureError: Error, appendingToPath: Bool = true) throws -> _JSONDecoder {
private func subDecoder<NestedKey: CodingKey>(forKey key: NestedKey, orThrow failureError: Error, appendingToPath: Bool = true) throws -> _JSONDecoder {
guard let (_, offset) = self.decoder.description.valueOffset(
forKey: self.decoder.string(forKey: key),
convertingSnakeCasing: self.decoder.snakeCasing,
Expand Down
12 changes: 8 additions & 4 deletions Sources/IkigaJSON/Core/Bounds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ internal struct Bounds {
data[i] = .newLine
// Move past this character
i = i &+ 1
case .f: // form feed, will just be passed on
return nil
case .b: // backspace, will just be passed on
return nil
case .f:
data[i] = .formFeed
// Move past this character
i = i &+ 1
case .b:
data[i] = .backspace
// Move past this character
i = i &+ 1
default:
throw JSONParserError.unexpectedEscapingToken
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/IkigaJSON/Core/Pointer+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ extension ByteBuffer {
internal let allocator = ByteBufferAllocator()

internal extension UInt8 {
static let backspace: UInt8 = 0x08
static let tab: UInt8 = 0x09
static let newLine: UInt8 = 0x0a
static let formFeed: UInt8 = 0x0c
static let carriageReturn: UInt8 = 0x0d
static let space: UInt8 = 0x20
static let quote: UInt8 = 0x22
Expand Down
8 changes: 8 additions & 0 deletions Sources/IkigaJSON/Types/JSONArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ extension String {
escaped = true
characters[i] = .backslash
characters.insert(.t, at: i &+ 1)
case .backspace:
escaped = true
characters[i] = .backslash
characters.insert(.b, at: i &+ 1)
case .formFeed:
escaped = true
characters[i] = .backslash
characters.insert(.f, at: i &+ 1)
case .backslash:
escaped = true
characters.insert(.backslash, at: i &+ 1)
Expand Down
38 changes: 38 additions & 0 deletions Tests/IkigaJSONTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,44 @@ final class IkigaJSONTests: XCTestCase {
let test = try Foundation.JSONDecoder().decode(Test.self, from: object.data)
XCTAssertEqual(test.yes, "b")
}

func testFormFeedParsing() throws {
let string = #"""
{
"hi": "\f"
}
"""#.data(using: .utf8)!

let object = try JSONObject(data: string)
XCTAssertEqual(object["hi"].string, "\u{c}")
}

func testFormFeedEncoding() throws {
let object: JSONObject = [
"hi": "\u{c}"
]

XCTAssertEqual(object.string, #"{"hi":"\f"}"#)
}

func testBackspaceParsing() throws {
let string = #"""
{
"hi": "\b"
}
"""#.data(using: .utf8)!

let object = try JSONObject(data: string)
XCTAssertEqual(object["hi"].string, "\u{8}")
}

func testBackspaceEncoding() throws {
let object: JSONObject = [
"hi": "\u{8}"
]

XCTAssertEqual(object.string, #"{"hi":"\b"}"#)
}

func testBackslashWorks() throws {
let string = #"""
Expand Down

0 comments on commit 844b186

Please sign in to comment.