lundi 6 septembre 2021

JsonConvert methods Serializeobject and DeserializeObject with Reflection

Im trying to save layouts with json format in the Database.

Exception Message: Newtonsoft.Json.JsonSerializationException: 'Error converting value "System.Windows.Media.DrawingBrush" to type 'System.Windows.Media.Brush'. Path 'ZeroLineBrush'.' Could not cast or convert from System.String to System.Windows.Media.Brush.

More info. the deserialzation of the object only save the type of "DrawingBrush" which is a child from "Brush" but cant cast it back. The property ZeroLineBrush has the type of "Brush".

additional Question:

Why does Serializeobject save only the type name of the property not the valued structure of it?

Link: https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.drawingbrush?view=net-5.0

Method that gives the string json Format to be saved in the Database.

public string GetFormatValuetoJsonString(object obj)
        {
            var objvalue = obj.GetType().GetProperty("Format").GetValue(obj);                      
            return JsonConvert.SerializeObject(objvalue,settings); 
        }

Method that gives back a type FormatConditionBase for adding in the Format Collection.

public FormatConditionBase GetJsonStringtoFormatValue(string formatConditionClass, string formatClass, string expression, string fieldname, string format)
        {
           
            var formatType = Type.GetType(formatClass);

            if (formatType != null)
            {

                Type formatclasstype = Type.GetType(formatClass);

                var test = JsonConvert.DeserializeObject(format);
                
                var formatJObj = JsonConvert.DeserializeObject(format) as Newtonsoft.Json.Linq.JObject;
               //Here comes the error out
                var formatObj = formatJObj.ToObject(formatclasstype);

        // some rest codes
                }
        }   

The Json that was serialize and then deserialize. Some attribute was deleted for example purposes.

{
    "$id": "1",
    "ZeroLineBrush": "System.Windows.Media.DrawingBrush",
    "ZeroLineThickness": 1.0,       
    "Fill": "System.Windows.Media.LinearGradientBrush",
    "FillNegative": "System.Windows.Media.LinearGradientBrush",     
    "DependencyObjectType": {
        "Id": 583,
        "SystemType": "DevExpress.Xpf.Core.ConditionalFormatting.DataBarFormat, DevExpress.Xpf.Core.v19.1, Version=19.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
        "BaseType": {
            "Id": 577,
            "SystemType": "DevExpress.Xpf.Core.ConditionalFormatting.IndicatorFormatBase, DevExpress.Xpf.Core.v19.1, Version=19.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
            "BaseType": {
                "Id": 1,
                "SystemType": "System.Windows.Freezable, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
                "BaseType": {
                    "Id": 0,
                    "SystemType": "System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
                    "BaseType": null,
                    "Name": "DependencyObject"
                },
                "Name": "Freezable"
            },
            "Name": "IndicatorFormatBase"
        },
        "Name": "DataBarFormat"
    }       
}




Aucun commentaire:

Enregistrer un commentaire