Unicode Conversion

Apr 28, 2008 at 7:50 PM
I am trying out Json.Net and so far I like it pretty well. I notice that it doesn't seem to handle Unicode (UTF-16) and presumably it can't handle UTF-32 either. Am I missing something that is already there or is this something you are considering for the future?
Coordinator
May 3, 2008 at 4:36 AM
Hi Tom

I don't have much experience with writing multilingual applications so I wouldn't be surprised if there are cases that Json.NET can't handle.

Could you post an example of what you are trying to do and what didn't work as expected?
May 6, 2008 at 11:17 PM
Okay, as it turns out, I created another test and Unicode does work. I didn't save an example of what I was doing before since I needed to continue that project and I don't have time to try to reproduce the problem. It had something to do with using UnicodeEncoding.Unicode.GetString() I think. This time I tested by creating a string containing '\u20a4' (a British pound symbol). Json serializes and deserializes correctly.

While on the subject of testing, I noticed that when I downloaded your solution source code and ran the unit tests, the XmlNodeconverterTest OtherElementDataTypes() test failed. It failed because you hard-coded +12 for the UTC offset. This works for you because it's your timezone in New Zealand. My timezone is -7 (US Pacific Time). I fixed your test in a generic way with the following lines of code:

TimeSpan ts = DateTimeOffset.Now.Offset;
string expected =
@"<?xml version=""1.0"" standalone=""no""?><root><person id=""1""><Float>2.5</Float><Integer>99</Integer></person><person id=""2""><Boolean>true</Boolean><date>2000-03-30T00:00:00.0000000" +
(ts.TotalHours < 0d ? "-" : "+") +
Math.Abs(ts.Hours).ToString().PadLeft(2, '0') + @":00</date></person></root>";

The change is to create a TimeSpan with the local offset to UTC. In the string I check whether a minus or plus sign is needed and place a zero-filled absolute value for the hours.
Coordinator
May 7, 2008 at 7:15 AM
Are you using Json.NET 2.0? I believe fixed that test timezone error in 2.0.
May 7, 2008 at 3:43 PM
Edited May 7, 2008 at 4:08 PM
No, because we have a rule about not using beta software. However, I notice that you just released 2.0 on the 4th so I think I'll upgrade now. :-) Thanks for a library well done!

I guess I'll have to wait to upgrade. We haven't moved beyond .Net 2.0 yet, but hope to soon.