Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse escaped chars #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
37 changes: 34 additions & 3 deletions Source/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ open class JSON {
let value: Value & JSONStringRepresentable
}

public static func parse(data: Data) throws -> Value {
let str = String(data: data, encoding: .utf8)
if let str = str {
return try JSON.parse(string: str)
} else {
throw SerializationError.invalidJSON
}
}

/**
Return a JSON structure value

Expand Down Expand Up @@ -583,8 +592,7 @@ open class JSON {
while index != jsonString.endIndex {
if jsonString[index] == "\\" {
index = jsonString.index(after: index)

if jsonString[index] == "\"" {
if jsonString[index] == "\"" || jsonString[index] == "\\" {
index = jsonString.index(after: index)
} else {
continue
Expand All @@ -596,7 +604,30 @@ open class JSON {
}
}

let parsedString = String(jsonString[startingIndex ..< index])
var parsedString = String(jsonString[startingIndex ..< index])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could stay as a constant and be given a different name and built upon, e.g.
let initialParsedString = String()
let parsedString = initialParsedString.someOperation()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is useful. I mean how would you implement this for the hexEncodedUnicodeCharRegex parsedString? Feel free to push your solution on this branch.

.replacingOccurrences(of: "\\/", with: "/", range: nil)
.replacingOccurrences(of: "\\\\", with: "\\", range: nil)
.replacingOccurrences(of: "\\n", with: "\n", range: nil)
.replacingOccurrences(of: "\\t", with: "\t", range: nil)
.replacingOccurrences(of: "\\r", with: "\r", range: nil)
.replacingOccurrences(of: "\\\"", with: "\"", range: nil)
if let hexEncodedUnicodeCharRegex = try? NSRegularExpression(pattern: "\\\\u[0-9A-Fa-f]{4}") {
let parsedStringNs = parsedString as NSString
for match in hexEncodedUnicodeCharRegex.matches(in: parsedString, range: NSMakeRange(0, parsedStringNs.length)) {
let matchString = parsedStringNs.substring(with: match.range) as String
let matchStringHex = matchString.suffix(4)
var replaceData = Data()
if let byte = UInt8(matchStringHex.prefix(2), radix: 16), byte > 0 {
replaceData.append(contentsOf: [byte])
}
if let byte = UInt8(matchStringHex.suffix(2), radix: 16), byte > 0 {
replaceData.append(contentsOf: [byte])
}
if let replacementString = String(data: replaceData, encoding: .isoLatin1) {
parsedString = parsedString.replacingOccurrences(of: matchString, with: replacementString)
}
}
}

guard index != jsonString.endIndex else {
index = startingIndex
Expand Down
38 changes: 27 additions & 11 deletions Tests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,40 @@ class StringTests: TisanderTest {
XCTAssertEqual(json[0] as? String, "\\a")
}

func testEscapedQuoteString() {
func testEscapedCharsString() {
let input = """
["\\\""]
["\\n", "\\t", "\\r", "\\\"", "\\u00fc", "\\\\"]
"""
var json: Value
do { json = try JSON.parse(string: input) } catch let e { XCTFail((e as? SerializationError)?.rawValue ?? "Unknown exception"); return }

XCTAssertEqual(json[0] as? String, "\\\"")
XCTAssertEqual(json[0] as? String, "\n")
XCTAssertEqual(json[1] as? String, "\t")
XCTAssertEqual(json[2] as? String, "\r")
XCTAssertEqual(json[3] as? String, "\"")
XCTAssertEqual(json[4] as? String, "ü")
XCTAssertEqual(json[5] as? String, "\\")
}

// func testEscapedSlashString() {
// let input = """
//["\\\\"]
//"""
// do { json = try JSON.parse(string: input) } catch let e { XCTFail((e as? SerializationError)?.rawValue ?? "Unknown exception"); return }
//
// XCTAssertEqual(json[0] as? String, nil)
// }
func testUnterminatedStringError() {
// invalid JSON ["a\b\"] should throw SerializationError.unterminatedString
let input = """
["a\\b\\"]
"""
XCTAssertThrowsError(try JSON.parse(string: input)) { error in
XCTAssertEqual(error as! SerializationError, SerializationError.unterminatedString)
}
}

func testUnterminatedStringError2() {
// invalid JSON ["a\\\"] should throw SerializationError.unterminatedString
let input = """
["a\\\\\\"]
"""
XCTAssertThrowsError(try JSON.parse(string: input)) { error in
XCTAssertEqual(error as! SerializationError, SerializationError.unterminatedString)
}
}

func testUnterminatedString() {
let input = """
Expand Down