1

I am trying to serialize a object defined below. This happens correctly using Newtonsoft JsonConvert. This returns a string. When i try to deserialize the string back into the defined object it doesn't work and throws exception

private class PlotSetFeatureStateInfo
{                    
    public IDictionary<int,IDictionary<AxisTypeAndUnitInfo,ManualScaleInfo>> PersistentScaleInfo
    {
        get;
        set;
    }


    public IDictionary<Guid,ScaleType> PlotIdVsLocalScaleType { get; set; }            
    public IDictionary<Guid,IDictionary<AxisTypeAndUnitInfo, 
           PlotScales.PersistentScaleData>> PlotIdVsPersistentScaleData { get; set;}
}



 var foo = new FeatureStateInfo
                {
                     //Fill values
                };
 var res = JsonConvert.SerializeObject(foo);
 var deserializedProperty = JsonConvert.DeserializeObject<FeatureStateInfo>(res);//Throws error

Getting the below error

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.IDictionary2[System.Int32,System.Collections.Generic.IDictionary2[Axis,ManualScale]]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array

Posting the sample JSON without any edits. This is a valid json.

{"PersistentScaleInfo":[{"Key":1,"Value":[{"Key":{"AxisType":0,"UnitInfo":{"UnitId":"601988e7-06a1-4dfd-9bba-535989b3afba","SubunitId":"00000000-0000-0000-0000-000000000000"}},"Value":{"Units":"Hz ","MaxScale":400,"MinScale":0.062,"IsAuto":false,"Axes":"X","AxisType":0,"UnitInfo":{"UnitId":"601988e7-06a1-4dfd-9bba-535989b3afba","SubunitId":"00000000-0000-0000-0000-000000000000"},"IsMinLessThanMax":true}},{"Key":{"AxisType":1,"UnitInfo":{"UnitId":"250f1aea-b8e3-4a4d-98e0-f60abded10a4","SubunitId":"94d2baf0-3bc0-41f7-b207-d8ce54e65e35"}},"Value":{"Units":"in/s rms","MaxScale":0.015,"MinScale":0,"IsAuto":false,"Axes":"Y","AxisType":1,"UnitInfo":{"UnitId":"250f1aea-b8e3-4a4d-98e0-f60abded10a4","SubunitId":"94d2baf0-3bc0-41f7-b207-d8ce54e65e35"},"IsMinLessThanMax":true}}]}],"PlotIdVsLocalScaleType":[{"Key":"5ef394a9-98ad-4f52-b916-b698ae4ef351","Value":2}],"PlotIdVsPersistentScaleData":[{"Key":"5ef394a9-98ad-4f52-b916-b698ae4ef351","Value":[{"Key":{"AxisType":0,"UnitInfo":{"UnitId":"601988e7-06a1-4dfd-9bba-535989b3afba","SubunitId":"00000000-0000-0000-0000-000000000000"}},"Value":{"ManualScaleRange":{"MaxScale":400,"MinScale":0.062},"IsVisibleOnView":true,"IsAutoChecked":false,"AreManualScalesSet":true}},{"Key":{"AxisType":1,"UnitInfo":{"UnitId":"250f1aea-b8e3-4a4d-98e0-f60abded10a4","SubunitId":"94d2baf0-3bc0-41f7-b207-d8ce54e65e35"}},"Value":{"ManualScaleRange":{"MaxScale":0.015,"MinScale":0},"IsVisibleOnView":true,"IsAutoChecked":false,"AreManualScalesSet":true}}]}]}

SOLUTION: i modified the class according to everyones suggestion as below. This works fine for me

public class PlotSetFeatureStateInfo
        {

            public List<KeyValuePair<int, List<KeyValuePair<AxisTypeAndUnitInfo, ManualScaleInfo>>>> PersistentScaleInfo
            {
                get;
                set;
            }


            public List<KeyValuePair<Guid, ScaleType>>PlotIdVsLocalScaleType { get; set; }

            public List<KeyValuePair<Guid,List<KeyValuePair<AxisTypeAndUnitInfo, 
          PlotScales.PersistentScaleData>>>> PlotIdVsPersistentScaleData { get; set; }
        }

Thank you :)

  • Could you post the sample `JSON` that you are trying to deserialize? – vikscool May 29 '19 at 06:26
  • Show the json string and how you're attempting to deserialise it. – bolkay May 29 '19 at 06:30
  • look here https://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us – Leonid Malyshev May 29 '19 at 06:31
  • @Sagar Deshpande have you checked your JSON. It seems to be invalid. – SUNIL DHAPPADHULE May 29 '19 at 06:39
  • @LeonidMalyshev that post talks about XmlSerializer. I am looking for Json deserialization for a dictionary wrapped inside a object – Sagar Deshpande May 29 '19 at 07:04
  • You need to declare classes that match the structure of your JSON. Your JSON is an object, with a property named `PersistentScaleInfo`, which is a collection of objects, etc. You can't just say "I'd like this in a dictionary". – Lasse V. Karlsen May 29 '19 at 07:25
  • The reason why you `Json` is getting failed in de-serialization is because of square brackets `[` whereas for your dictionary it should have curly braces `{`. So, convert your dictionaries into classes as suggested by @LasseVågsætherKarlsen. – vikscool May 29 '19 at 10:01

0 Answers0