1

I'm trying to deserialize my Firefox bookmarks so I can do things with them. Surprisingly, this worked flawlessly:

class Bookmark
{
    public string title;
    public int? id;
    public int? parent;
    public string dateAdded;
    public string lastModified;
    public string type;
    public string root;
    public string uri;
    public List<Bookmark> children; 
}

class Program
{
    static void Main(string[] args)
    {
        var jss = new JavaScriptSerializer();
        var json = File.ReadAllText(@"T:\bookmarks-2012-08-08.json");
        var root = jss.Deserialize<Bookmark>(json);
    }
}

I can inspect the "root" object and everything comes out fine. The dates, however, look something like "1260492675000000". I'd like to convert those to DateTime objects instead. I'm guessing those are just stored as number of milliseconds to epoch, which should be easy enough to convert, but how would I implement a custom deserializer for just those two date fields without compromising the rest of the fields, which were done for me automagically?

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Could you not do a transform on the fields in the file? It might be a lot easier. – Burt Aug 12 '12 at 00:58
  • @Burt: What do you mean? Pre-process the .json file to put the dates into a format the JS deserializer will understand? Even if I wanted to go that route (which I don't), how would I pre-process the dates without having already deserialized them? Read in the file with dates as strings, loop over them, write back out to another json file, and read back in? That's just ridiculous. – mpen Aug 12 '12 at 01:05
  • How about adding an extra field "dateAddedConverted" and doing the conversion there? – Daniel Aug 12 '12 at 06:53
  • @Daniel: Yeah, I could loop over it once I've deserialized it and fill in the data how I want it, but then I'm essentially storing the same data twice, unless I move it into a separate structure. Not keen on that solution. – mpen Aug 12 '12 at 07:04
  • 2
    no i mean adding a field: public DateTime ConvertedDate{get {return convert(dateadded)}} – Daniel Aug 12 '12 at 07:08
  • @Daniel: That's called a "property" isn't it? I thought "fields" were non-property member variables. Anyway, yeah, I guess that makes sense. I guess the convert function wouldn't be too expensive. I'd still like to know if the JavaScriptSerializer has hooks to convert it as it reads it in though. – mpen Aug 13 '12 at 00:22
  • @Mark: sorry, i was getting my Terms muddled up. – Daniel Aug 13 '12 at 04:33

1 Answers1

4

You should add a field to the Bookmark class (like what Daniel said). Fields do not affect serialization in C#.

Example:

class Bookmark
{
    ...
    public DateTime ConvertedDateTime { get { return yourConversionMethod(dateAdded); } }
}
Nathan Albrecht
  • 155
  • 1
  • 6
  • 1
    You mean "Properties" no? This will work, but I'm still interested in learning if the library has any more neat features to offer. – mpen Aug 13 '12 at 00:24