CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 04/24/2008 in C#.NET


LitJson is great - lightweight, fast, easy to incorporate, etc. Thanks Leonard Boshell!

I actually found the need to serialize Hashtables to a JSON string that contained Keys that were of the DateTime datatype. LitJson 0.5.0 assumes that the Keys of Hashtable or IDictionary can be cast into a string by default. The DateTime object cannot be CAST into a string, but it can be converted into a string using Convert or ToString().

Case in point - I want to convert:
DateTime currDate = new DateTime();
Hashtable dateTest = new Hashtable();
dateTest.Add(currDate, "random value");
to be able to serialize as (with the default DateTime format):
{"4/24/2008 12:00:00 AM":"random Value"}


Here's a simple patch to the JsonMapper.cs file:
--- JsonMapper.cs       24 Apr 2008 23:16:16 -0000      1.1
+++ JsonMapper.cs       24 Apr 2008 23:16:54 -0000
@@ -739,7 +739,11 @@
             if (obj is IDictionary) {
                 writer.WriteObjectStart ();
                 foreach (DictionaryEntry entry in (IDictionary) obj) {
-                    writer.WritePropertyName ((string) entry.Key);
+                       if (entry.Key is DateTime)
+                           writer.WritePropertyName ( entry.Key.ToString() );
+                    else
+                           writer.WritePropertyName ((string) entry.Key);
+
                     WriteValue (entry.Value, writer, writer_is_private,
                                 depth + 1);
                 }
Download patch: JsonMapper.cs.qwnu.diff
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar