This project is read-only.
1
Vote

cannot serialize dynamic

description

I cannot round-trip a DynamicJson instance:

dynamic data = new DynamicJson();
data["surveyid"] = Guid.NewGuid().ToString("D");
data["page"] = 1;
data["complete"] = false;
string surveyData = DynamicJson.Serialize(data);

gives me
surveyData = {"IsObject":true,"IsArray":false}

I expect
surveyData = {"surveyid":"2e60aafb-825e-472d-9ae5-795eae7e3c70", "page":1, "complete":false}

static IEnumerable<XStreamingElement> CreateXObject(object obj) does not respect obj when it is IDynamicMetaObjectProvider (DynamicObject, ExpandoObject, or anything else that might be 'dynamic')

Using Dynamitey (https://github.com/ekonbenefits/dynamitey) I was able to do this, and get my expected result.
     if (obj is IDynamicMetaObjectProvider)
     {
        return Dynamic.GetMemberNames(obj, true).Select(name =>
        {
           object value = Dynamic.InvokeGet(obj, name);
           return new XStreamingElement(name, CreateTypeAttr(GetJsonType(value)), CreateJsonNode(value));
        });
     }
     else
     {
        return obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
             .Select(pi => new { Name = pi.Name, Value = pi.GetValue(obj, null) })
             .Select(a => new XStreamingElement(a.Name, CreateTypeAttr(GetJsonType(a.Value)), CreateJsonNode(a.Value)));
     }

comments