skip to Main Content

Im a newbie with Json stuff. I have a project and am using Newtonsoft. I have this object

`

namespace ConsoleApp1
{
    [JsonObject(MemberSerialization.OptIn)]
    public class Data
    {

        [JsonProperty]
        public bool IsActive { get; set; } = false;

        [JsonProperty]
        public string Source { get; set; }

        [JsonProperty]
        public object SourceData { get; set; }

        [JsonProperty]
        public bool IsClickable { get; set; } = true;

        [JsonConstructor]
        public Data(string Source = "", string SourceData = "", bool IsActive = false, bool IsClickable = true)
        {
            this.IsActive = IsActive;
            this.Source = Source;
            this.SourceData = SourceData;
            this.IsClickable = IsClickable;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            JsonSerializerSettings JsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, NullValueHandling = NullValueHandling.Ignore };
            Data data = new Data() { IsActive = true, Source = "test", SourceData = new List<int> { 1, 2, 3 }, IsClickable = false };
            string json = JsonConvert.SerializeObject(data, Formatting.Indented, JsonSettings);
            Console.WriteLine(json);
            Data dataDes = JsonConvert.DeserializeObject<Data>(json, JsonSettings);
            Console.WriteLine(dataDes);
        }
    }
}

`

So when it gets serialized I get this :

`

{
  "IsActive": true,
  "Source": "test",
  "SourceData": {
    "$type": "System.Collections.Generic.List`1[[System.Int32, System.Private.CoreLib]], System.Private.CoreLib",
    "$values": [
      1,
      2,
      3
    ]
  },
  "IsClickable": false
}

`

But when it gets deserialized I get this error :
Newtonsoft.Json.JsonReaderException: ‘Unexpected character encountered while parsing value: {. Path ‘SourceData’, line 4, position 17.’

Have i forgotten to set something somewhere to make the Metadata work when deserialized?

2

Answers


  1. From your question, the issue is how you are using your object property on SourceData. When Json is serializing it is smart enough to detect that the object datatype is actually an array and handles it according.

    When the object is being deserialized, it is reading your constructor with the datatype "string" and attempting to deserialize it as a string, which is why it errors out when it finds a start of an array [ and not a string ".

    This modified constructor should work (Adapted with MySkullCavelsADarkPlace comment)

        [JsonConstructor]
        public Data(string Source = "", JToken SourceData = null, bool IsActive = false, bool IsClickable = true)
         {
            
    
            this.IsActive = IsActive;
            this.Source = Source;
            
            this.SourceData = SourceData;
            // JToken.Type will hold the type that the deseralizer for Newtonsoft.Json determine that the type is
            var sourceType = SourceData.Type;
    
            this.IsClickable = IsClickable;
        }
    
    Login or Signup to reply.
  2. To deserialize a json you posted you need to create one more class, after this evertything is working properly

    Data data =  JsonConvert.DeserializeObject<Data>(json, 
       new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
    );
    
    public class Data
    {
        public DropdownData DropdownData {get;set;}
    }
    

    UPDATE

    Since you changed your question, it is working too

    var data =  JsonConvert.DeserializeObject<DropdownData>(json, 
    new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All } );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search