-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Escaping backslash incorrectly when backslash is used to escape forward slash as in ASP.NET JSON date format #21
Comments
I can't say I'm familiar with Microsofts "solution" to encoding dates, but their "solution" has a few potential problems. Well, at least for anything that wants to interoperate with Microsofts "solution". Technically, this is not a bug in JSONKit, it's really more of a design flaw on Microsofts part. When you ask JSONKit to encode The Now consider the Then consider Microsofts "solution"- A JSON string of If Microsoft absolutely, unconditionally requires the JSON string to be Not really sure how to deal with this issue. All of the solutions I can think of suck, and I have to admit that it really rubs me the wrong way to hack up the string encoding functions with what amounts to a one-off hack just to work around Microsofts mistake- a hack that would add at least another branch and compare required for every single character for every single string that needs to be encoded. sigh |
It's definitely a pain and adds overhead to JSONKit which is highly optimized. However, Microsoft does use this loophole in their new WCF REST APIs and other places. What about adding a parsing option? Bavarious has demonstrated a patch to the code (without a parse option) here: http://stackoverflow.com/questions/5844525/how-to-prevent-jsonkit-from-escaping-backslash-from-asp-net-json-date-format |
I’d answered that question on Stack Overflow before having read John’s comment on this bug report. I agree with John’s arguments. That said, would it be possible to implement this feature as preprocessor dependent code? This would leave JSONKit as is unless a preprocessor macro is defined. |
The solution in stackoverflow answer doesn't work. Well, it works in that it handles the immediate problem, but it just shifts the problem somewhere else and actually causes more problems than it fixes. The gist of the problem is this: Suppose you have a So far the "best" solution I can think of is a flag that optionally turns on backslash escaping of If you need a solution right now, you can do the following: Use a NSString *escapedJSONString = [convertedToJSONString stringByReplacingOccurrencesOfString:@"/" withString:@"\\/"]; Crude, not terribly efficient, but I think that might be your best bet for now. I'll need to think about how to deal with the issue in JSONKit. |
That's kind of how the post here: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx says to deal with it. I can understand if you don't want to put this case in JSONKit. I just wanted to make everyone aware of the issue. I'm looking forward to the final decision. |
I will probably add an encoding option that allows you to optionally escape forward slashes. Even though I don't much care for the reason why it has to be done, people have to use the library in the real world, and realistically, this can't be avoided. And since you pretty much have to bite the bullet, you're stuck with either putting it in to the guts of JSONKit or using something like the ... though don't expect me to "get right on it". :) Let me know if the proposed |
Thanks for being so pragmatic in your approach. I have implemented a workaround but look forward to the option being implemented in the future. |
I've committed a change that should "fix" this. When serializing a Cocoa collection with one of JSONKits serialization methods that accepts options, you can pass it |
* 'master' of github.com:playup/JSONKit: Adds a serializing option to backslash escape forward slashes. This is for issue johnezang#21. Change a NSException from exceptionWithName: to raise:. Closes johnezang#20. Fixes a bug when removing items from a JKDictionary. Since JKDictionary is implemented using a hash table that uses linear probing, the removal function needs to "re-add" items that follow the removed item so that linear probe hash collisions are not "lost". Closes johnezang#17
@mattspradley, did the committed change resolve this problem to your satisfaction? If so, go ahead and close this issue. Otherwise, let me know what else I can do to help. |
That fixed it. |
When JSONData is called on a NSDictionary that has a key value pair that has a string encoded with ASP.NET Date format (http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx) the backslash in the date format is incorrectly escaped with another backslash.
For example:
"/Date(1303502009425)/"
is encoded as:
"/Date(1303502009425)/"
This should not be the case because the JSON spec indicates that the forward slash can optionally be escaped.
The text was updated successfully, but these errors were encountered: