0

i am experimenting with backbone javascript after seeing the Tekpub MVC3 screencasts by Rob Connery

i like his Massive database access, but as soon as it is getting a bit more complex than a video can possibly show you.

i added extra fields to my database, being datetime fields. however, this javascript serializer, converts them into strings

public string toJson(dynamic content) {
  var serializer = new JavaScriptSerializer();
  serializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoObjectConverter() });
  var json = serializer.Serialize(content);
  return json.ToString();
}

this makes a datetime from this: {19/10/2011 1:58:27} into this: "19/10/2011" (*values taken from the quickwatch window on runtime..., basicly comes down to a loss in precision and it now being a basic string.

after backbone pushes that back to the server (on a model.save() call), i try to update the model like Rob does:

[HttpPut]
public ActionResult Edit()
{
  var model = SqueezeJson();
  model.UpdatedAt = DateTime.Now;
  _movies.Update(model, model.Id);
  return CmoJSON(model);
}

for the SqueezeJson function, check his source

resulting in an error like this:

Arithmetic overflow error converting expression to data type datetime.

i kind of expected this to happen since i noticed the dates being dumped into strings, i had no idea how it would go back into a date time using massive.

has anyone worked with massive and dates, in a context like this (serializing to and from json)? i know the problem isn't necessarily massive itself, it's the json serializiation that dumbs it down into a string with loss of data, and doesn't return it to a proper date.

but still, maybe someone has a better way of doing this... any idea's are welcome...

Sander
  • 13,301
  • 15
  • 72
  • 97
  • 1
    No, the problem is actually with Massive itself: https://github.com/tekpub/mvc3/blob/master/Source/VidPub.Web/Infrastructure/ExpandoObjectConverter.cs#L28 Why it truncates a `DateTime` down to `ToShortDateString()` is something you should bring up with Rob. – Crescent Fresh Nov 16 '11 at 04:26
  • hm technically that isn't massive anymore right? you took a sample of code from his mvc3 tekpub screencast source code. massive is a library in itself. the problem lies within the custom JavascriptConverter shipped with his sourcecode for the tekpub video then... but thanks for pointing out where the problem lies, either way i had to go to Rob. – Sander Nov 16 '11 at 07:46

1 Answers1

0

I have encountered the same question with you.

You can change Serialize method in ExpandoObjectConverter like:

public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
    ExpandoObject expando = (ExpandoObject) obj;

    if(expando!=null)
    {
        Dictionary<string,object> result = new Dictionary<string, object>();
        foreach (KeyValuePair<string, object> item in expando)
        {
            var value = item.Value ?? "";
            if (value is DateTime)
                result.Add(item.Key, ((DateTime) value).ToString("yyyy.MM.dd"));
            else
            {
                result.Add(item.Key, value.ToString());
            }
        }

        return result;
    }

    return new Dictionary<string, object>();
}
Sander
  • 13,301
  • 15
  • 72
  • 97
Alex Chen
  • 645
  • 2
  • 10
  • 25
  • it passes but still isn't 100%, a `Date` field would be perfect with your solution, but a `DateTime` still loses any and all time data, `19/10/2011 1:56:56` would be reset in the DB to `19/10/2011 0:00:00` – Sander Nov 16 '11 at 22:57
  • thats depends on your localization setting as I am currently in China Mainland. You could use ToString("o") to get ISO 8610 timestamp format and have a try. – Alex Chen Nov 17 '11 at 02:38
  • nope the ISO string fails again when importing it back to the server, i'm going to try a bunch of different formats tonight, we'll see what works and what not. thanks for the tips – Sander Nov 17 '11 at 07:01
  • @Sander if u put more src code, may be I can give some more help. And it seems that u are a subscriber of tekpub, just mail rob and ask this question (or give him the link on stackoverlfow). – Alex Chen Nov 18 '11 at 18:28
  • i did inquire more input from tekpub, can't hurt to ask on 2 platforms at once. more source won't really help, it comes down to finding how to give a date(with time) from mvc to backbone trough that serialize method you show me. so that when backbone posts it back to the edit action (as seen in my code example) can put it back in the database (_movies is a table, and it has a CreatedAt field which is a datetime) your example works, but in pushing it through the converter, you already drop the time details in your toString. so its normal that they get lost. – Sander Nov 18 '11 at 19:35