Skip to content

Commit 8b55c65

Browse files
committed
Fixed issue #36 - escape quotes in string.
1 parent 6d4a3b7 commit 8b55c65

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/JSValue.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ extension JSValue {
245245
return "\(number)"
246246

247247
case .JSString(let string):
248-
return "\"\(string)\""
248+
let escaped = string.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
249+
return "\"\(escaped)\""
249250

250251
case .JSArray(let array):
251252
return "[\(newline)" + join(",\(newline)", array.map({ "\(nextIndent)\($0.prettyPrint(indent, level + 1))" })) + "\(newline)\(currentIndent)]"

tests/JSValueTests.Parsing.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,14 @@ class JSValueParsingTests : XCTestCase {
393393
let json = JSON.parse(string as! String)
394394
XCTAssertTrue(json.error == nil, json.error?.userInfo?.description ?? "No error message found")
395395
}
396+
397+
func testStringifyEscaping() {
398+
var json: JSON = [
399+
"url" : "should escape double quotes \""
400+
]
401+
402+
let str = json.stringify(0)
403+
let expected = "{\"url\":\"should escape double quotes \\\"\"}"
404+
XCTAssertEqual(str, expected)
405+
}
396406
}

0 commit comments

Comments
 (0)