i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Json serialization with escaped characters.

Open rickardliljeberg opened this issue 10 years ago • 4 comments

I have this MVC controller that returns an object to a AJAX call with JSON serialization as you can see here: https://db.tt/ppJJmS5x

However this is what reaches the client

{"Success":2,"Message":"Unable to delete the tag while it\u0027s in use. The tag is used in 1 places","PermissionAccessValid":true,"PermissionLevel":0,"PermissionResponseId":null,"PermissionResponseBool":null,"PermissionResponseText":null}

you can see that the "it's" has been encoded to "it\u0027s" and for that reason (I think at least) it does not find the nugget.

I don't quite know what is the best action here and would happily listen to advice. One possibility is to allow the i18n project to find escaped characters but i see definite drawbacks with that as well. Any suggestions?

rickardliljeberg avatar Feb 05 '15 18:02 rickardliljeberg

Would it not be reasonable to write the nugget [[[it's]]] as [[[it\u0027s]]] in the first place?

turquoiseowl avatar Feb 05 '15 19:02 turquoiseowl

Well in my case it would be OK. but it feels annoying that I need to think of that when it will be jsoned I could also rewrite it's with it is.

It would just loose that freedom that this project gives about not caring about delivery just make nuggets and it will be served right.

But I don't have a great solution myself so yeah it deserves some thinking. On Feb 5, 2015 8:42 PM, "Martin Connell" [email protected] wrote:

Would it not be reasonable to write the nugget [[[it's]]] as [[[it\u0027s]]] in the first place?

— Reply to this email directly or view it on GitHub https://github.com/turquoiseowl/i18n/issues/182#issuecomment-73112374.

rickardliljeberg avatar Feb 05 '15 22:02 rickardliljeberg

Perhaps a best-practices approach would be to say:

We recommend you always escape: nugget markup, html markup, and ' and " for json.

turquoiseowl avatar Feb 09 '15 14:02 turquoiseowl

Well you could but i see several problems with this.

  1. Human error
  2. Annoying to look up what ' and " are every time
  3. But perhaps biggest, it's not at all unlikely that i want to pipe the same data to a view and to a mobile application via json or xml.

Encoding the text as you write it seems both cumbersome and error prone.

I am more thinking along the lines of detecting json format, then looking for ' and " and anything else that breaks. Or perhaps just look for any encoded characters to see if encoding is going on for any reason.

So if the reply seems to be json and contains {varName:"[[[it's fine]]]"} then look for translation key both "it\u0007s fine" and "it's fine"

You could also configure when to trigger this behaviour (json for instance) and which characters to look out for and have interchangable.

We solved it with pure "it is" workaround, but could still be interesting.

rickardliljeberg avatar Feb 09 '15 14:02 rickardliljeberg