Swiftyjson: Forward slashes escaped in quoted strings

Created on 21 Jan 2016  路  2Comments  路  Source: SwiftyJSON/SwiftyJSON

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.

bug

Most helpful comment

having similar issue. Find a solution?

All 2 comments

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
//  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MrLoveQD picture MrLoveQD  路  5Comments

dubiao picture dubiao  路  6Comments

kwstasna picture kwstasna  路  4Comments

amit-bhavsar picture amit-bhavsar  路  5Comments

posixrails picture posixrails  路  6Comments