FransBouma / Massive

A small, happy, dynamic MicroORM for .NET that will love you forever.
Other
1.8k stars 323 forks source link

Support for return type of MS JavascriptSerializer #199

Closed yelouafi closed 11 years ago

yelouafi commented 11 years ago

Hi,

i'm using your Code in a small Webmatrix Project and ran into a 'minor' issue wile working on json deserialization.

actually i'm using System.Web.Script.Serialization.JavaScriptSerializer to parse my json payloads sent from the browser.

it seems the return type of JavaScriptSerializer.DeserializeObject is not supported by the 'RecordToExpando' method. tracing execution lead me to this bloc in the method


         public static dynamic RecordToExpando(this IDataReader rdr) {
           .....
           else {
                           var props = o.GetType().GetProperties();

But actually the type returned by the deserializer doesnt handle this method and just returns an empty array of PropertyInfo

Actually the return type is of some System.Collections.Generic.Dictionary`2[System.String,System.Object]

so i tried by inserting this piece of code in the RecordToExpando method


           else if(typeof(IDictionary<string, object>).IsAssignableFrom(o.GetType())) {
                           var dic= (IDictionary<string,object>)o;
                            foreach(var kv in dic)  {
                               d.Add(kv.Key, kv.Value);
                            }               
                       }

           else {
            var props = o.GetType().GetProperties();

and it worked

EDIT just

           else if(typeof(IDictionary<string, object>).IsAssignableFrom(o.GetType())) {                              
               return o;
           } else {
            var props = o.GetType().GetProperties();

works fine too

Before i tried the Json.Decode helper method in webmatrix but i couldn't figure how to iterate over the properties of the the type returned (some 'System.Web.Helpers.DynamicJsonObject')

so i let you know in case of you where willing to add support of IDictionary int the RecordToExpando method

robconery commented 11 years ago

Part of the reason Massive is a single code file is just this - you can go in and edit for your needs :). I do appreciate you letting me know about this - but I don't think it's a change I want to put into the core.