When I encode a URL string, SwiftyJSON is escaping all my forward slashes. So this
http://path/to/some/thing.png
becomes this (when viewing the JSON as text, it's fine when unencoded):
http:\/\/path\/to\/some\/thing.png
Which gets quite ugly and unreadable for long URLs. With JSON, it's nice to maintain the "human-readable" part of it, and it's handy to be able to copy-paste URLs from JSON into a browser. Is there any option to NOT escape forward slashes in SwiftyJSON? As I understand it, the JSON spec does not require it, and the whole string will be quoted in the file anyway.
So I guess this is a feature request to add that option, unless I'm missing it somewhere.
having similar issue. Find a solution?
This is NSJSONSerialization.dataWithJSONObject that likes to insert additional backslashes. Escaped forward slashes is also valid json.
Workaround
func serialize(json: JSON) -> String {
let s0: String = json.rawString() ?? ""
let s1: String = s0.stringByReplacingOccurrencesOfString("\\/", withString: "/")
return s1
}
It's not perfect, since testForwardSlashes1 fails
class SwiftyJSON_Workaround_Test: XCTestCase {
func testForwardSlashes0() {
let json0 = "\"dir0/dir1/file\""
let json1 = serialize(JSON(json0))
XCTAssertEqual(json0, json1)
}
// func testForwardSlashes1() {
// let json0 = "\"dir0\\/dir1/file\""
// let json1 = serialize(JSON(json0))
// XCTAssertEqual(json0, json1) // BOOM doesn't work
// }
}
Most helpful comment
having similar issue. Find a solution?