0

I have a problem with serialization/deserialization of object.

We have a class that has 3 properties, one of them is

    Dictionary <string, object1>. 

That object1 has only 1 property

    Dictionary<string, object2>

We have to do serialization/deserialization because I need to save that to database as a string.

I am trying to use JsonConvert to do it

    string jsonObject = JsonConvert.SerializeObject(windowsProperties);

to serialize and

    var rootObject = JsonConvert.DeserializeObject<WindowPropertiesM>(jsonObject);

to deserialize object.

When deserializing the dictionary remains empty.

Any tips on how to do it? Tried with TypeNameHandling = TypeNameHandling.All but no help

EDIT: jsonObject looks something like this after SerializeObject

{
  "GridProperties": {
    "myGridControl": {
      "Properties": {
        "Number": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 66.0,
          "VisibleIndex": 11,
          "Visible": true
        },
        "Active": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 21.0,
          "VisibleIndex": 9,
          "Visible": true
        },
        "I": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 21.0,
          "VisibleIndex": 4,
          "Visible": true
        }
      }
    }
  },
  "MDISize": "1243,602",
  "MDILocation": "50,50"
}

EDIT:

 public class WindowPropertiesM: BaseM
    {
        private Dictionary <string, Models.GridPropertiesM> _gridProperties;
        private System.Windows.Size _mdiSize;
        private System.Windows.Point _mdiLocation;

        public Dictionary <string, Models.GridPropertiesM> GridProperties
        {
            get { if (_gridProperties == null) _gridProperties = new Dictionary <string, Models.GridPropertiesM>(); return _gridProperties; }
            set { if (_gridProperties != value) { _gridProperties = value; } }
        }

        public System.Windows.Size MDISize
        {
            get { return _mdiSize; }
            set { if (_mdiSize != value) { _mdiSize = value; OnPropertyChanged(); } }
        }

        public System.Windows.Point MDILocation
        {
            get { return _mdiLocation; }
            set { if (_mdiLocation != value) { _mdiLocation = value; OnPropertyChanged(); } }
        }
    }



public class GridPropertiesM : BaseM
    {
        private Dictionary <string, Models.GridPropertyM> _Properties;

        public Dictionary <string, Models.GridPropertyM> Properties
        {
            get { if (_Properties == null) _Properties = new Dictionary <string, Models.GridPropertyM>(); return _Properties; }
            set { if (_Properties != value) { _Properties = value; } }
        }

    }
    public class GridPropertyM : BaseM
    {
        private int? _sortIndex;
        private DevExpress.Data.ColumnSortOrder _sortOrder;
        private double _width;
        private bool _visible;
        private int _visibleIndex;

        #region Properties
        public Int32? SortIndex
        {
            get { return _sortIndex; }
            set { if (_sortIndex != value) { _sortIndex = value; OnPropertyChanged(); } }
        }
        public DevExpress.Data.ColumnSortOrder SortOrder
        {
            get { return _sortOrder; }
            set { if (_sortOrder != value) { _sortOrder = value; OnPropertyChanged(); } }
        }
        public double Width
        {
            get { return _width; }
            set { if (_width != value) { _width = value; OnPropertyChanged(); } }
        }
        public Int32 VisibleIndex
        {
            get { return _visibleIndex; }
            set { if (_visibleIndex != value) { _visibleIndex = value; OnPropertyChanged(); } }
        }
        public bool Visible
        {
            get { return _visible; }
            set { if (_visible != value) { _visible = value; OnPropertyChanged(); } }
        }
        #endregion
    }
user2516578
  • 19
  • 2
  • 10
  • What does you JSON look like? – theduck Nov 22 '19 at 14:00
  • added a part of JSON to question – user2516578 Nov 22 '19 at 14:04
  • Thanks. Can you also expand on your model a bit. What does the definition look like and which part of the JSON are you trying to map to the dictionary? Just trying to get to something everyone can try out and test. – theduck Nov 22 '19 at 14:09
  • added to the question – user2516578 Nov 22 '19 at 14:18
  • Your question is answered here: https://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c –  Nov 22 '19 at 14:21
  • Your code and model work fine for me. After deserializing using your demo JSON I can see `rootObject.GridProperties.First().Value.Properties.Count` is equal to 3 and I can see each of the sub-objects for instance `rootObject.GridProperties.First().Value.Properties.First().Value.Width` I see as 66. – theduck Nov 22 '19 at 14:44
  • @theduck you used serialize and deserialize like I wrote in the question? It is still not working for me. Maybe because I am using our own type of ObservableConcurentDictionary – user2516578 Nov 22 '19 at 15:03
  • 1
    [Here](https://github.com/zaveoco/StackOverflow/tree/master/questions/58995774) is a working POC. I had to change some small items but I don't think they impact the overall process. – theduck Nov 22 '19 at 15:06
  • 1
    I can't reproduce this either, see https://dotnetfiddle.net/PHLlMr. I had to comment out the `MDISize` and `MDILocation` properties because `System.Windows.Size` and `System.Windows.Point` are not available on https://dotnetfiddle.net/, but if you paste the code into a visual studio project, it passes even with them included. We need to see a [mcve] to help you. – dbc Nov 22 '19 at 17:09

0 Answers0