nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

Cannot emit JSON string, possibly another escaping issue #111

Closed elmateo487 closed 8 years ago

elmateo487 commented 8 years ago

Hi,

A few weeks back we ran into an issue where JSON string handling was not being emitted in certain cases because of escaped characters. That issue was fixed rapidly. I believe I have run into a new issue, where escaped quotes are causing the message to not send. Here is the JSON that cannot be sent. Keep in mind this is valid JSON by all means.

I believe this is the portion in questions "Cute Without the \"E\" (Cut From the Team)"

Thanks

`{
"nowPlaying":{
"trackUri":"spotify:track:1aYWQrIT8XPdtV4hhUjczN", "trackSong":"Cute Without the \"E\" (Cut From the Team)", "trackAlbumArtUri":"https:\/\/i.scdn.co\/image\/8d6f455c78dff3184c8e4009b3a33f4878d442a6", "startTime":1457070317.674355, "trackArtist":"Taking Back Sunday" }, "userId":"redacted", "userProfilePicUri":"redacted", "userName":"redacted", "lastTenSongs":[

] }`

nuclearace commented 8 years ago

I'm able to send something like:

socket.emit("jsonStringTest", "{\"myEvent\":\"Cute Without the \"E\" (Cut From the Team)\"}")

But that will explode the server when it tries to parse it.

If you were trying to add quotes to the string, then you'd have to do this:

"{\"myEvent\":\"Cute Without the \\\"E\\\" (Cut From the Team)\"}"
elmateo487 commented 8 years ago

For me that above example does not work. Reacts the same. What does work is sending invalid JSON, as follows (see notes below. The editor is removing my escaped characters)

{"nowPlaying":{"trackUri":"spotify:track:1aYWQrIT8XPdtV4hhUjczN","trackSong":"Cute Without the \"E\" (Cut From the Team)","trackAlbumArtUri":"https:\/\/i.scdn.co\/image\/8d6f455c78dff3184c8e4009b3a33f4878d442a6","startTime":1457109123.103975,"trackArtist":"Taking Back Sunday"},"userId":"redacted","userProfilePicUri":"redacted","userName":"redacted","lastTenSongs":[]}

note that "trackSong":"Cute Without the \"E\" (Cut From the Team)" is not valid JSON. And since I am using a third party library for the JSON, I had to do the follow to the string.

let newString = localUserNew.stringByReplacingOccurrencesOfString("\"", withString: "\\"", options: NSStringCompareOptions.LiteralSearch, range: nil)

So it seems to me that escaped characters are not being handled correctly.

edit: it appears the github text editor is taking out my escaped characters.... \"E\" is what I have entered up there in both of those examples. But it only shows \"E\" Edit2: it actually won't let me type \ + \ + " + E + \ + \ + "

nuclearace commented 8 years ago

Use github's markdown to post code.

And like I said previously, in order to send a JSON string with escaped quotes, you'll have to properly escape them.

"{\"myEvent\":\"Cute Without the \\\"E\\\" (Cut From the Team)\"}"

Would be the proper way to encode \"E\" if you want to send it through json. All the library is doing is putting whatever is in emit through NSJSONSerialization, it's up to you to make sure that it's a proper JSON string.

Furthermore, sending JSON strings isn't really taking advantage of the library or Swift. Since Swift supports dictionary literals, and this client can turn those into proper JSON strings before being sent.

nuclearace commented 8 years ago

My test emit is

socket.emit("jsonStringTest", "{\"myEvent\":\"Cute Without the \\\"E\\\" (Cut From the Team)\"}")

and on the server it's

socket.on("jsonStringTest", (json) => {
    console.log("json string: " + JSON.parse(json)['myEvent'])
})
elmateo487 commented 8 years ago

Hum. Interesting. I tried exactly that this morning, and had the same affect (though I was using the full example string below) I will try again this afternoon. What is throwing me for a loop with your example is this...

This is perfectly valid json, and is exactly what I am sending in the emit.

{"nowPlaying":{"trackUri":"spotify:track:1aYWQrIT8XPdtV4hhUjczN","trackSong":"Cute Without the \"E\" (Cut From the Team)","trackAlbumArtUri":"https:\/\/i.scdn.co\/image\/8d6f455c78dff3184c8e4009b3a33f4878d442a6","startTime":1457109123.103975,"trackArtist":"Taking Back Sunday"},"userId":"redacted","userProfilePicUri":"redacted","userName":"redacted","lastTenSongs":[]}

\"E\" inside of a string is valid escaping inside of JSON. and \\\"E\\\" is also. Yet neither of them can be parsed on the server when included with the above example.

Maybe running a more complex, already valid JSON string through NSJSONSerialization, is causing something to happen that the server cannot parse?

Also, I would use dictionaries if I could, but these JSON strings need to be saved and used elsewhere in other services.

elmateo487 commented 8 years ago

Wow. Thanks for your help. I was sitting here literally running your example exactly to the T, and the string could not be emitted. So I updated my socket.io swift client, and it works now...... Both your example and my original string, no other change. I was only a few weeks old, so I am surprised.

This can be closed. Thanks again

nuclearace commented 8 years ago

Oh, yeah that's probably because you were on a pre-https://github.com/nuclearace/Socket.IO-Client-Swift/commit/c795f9fb259b67e41d1077abed18b402cae5f968 version. Where there probably was an escaping problem

juanitozambada commented 7 years ago

I have the same problem, I'm using nativescript-socketio that use socket.io-client-swift version 9.0.1