Deserialize json to object in c# does not map values
Asked Answered
L

2

7

I'm trying to deserialize a json string by using the Jsonconverter. I have all the values inside the json, I also want to mention that, that specific json is being generated from the swagger, so when i send a payload to the API it maps it into the object model.

However, when i try to deserialize it into the same object, everything is null. What I am doing wrong?

This is how i deserialize it

EmailUrlDto testEmailUrlDto = JsonConvert.DeserializeObject<EmailUrlDto>(jsonString);

JSON format

  {
    "templateUrl": "https://some.blob.url.here.com",
    "paramsHtml": [
  {
    "name": "{{miniAdmin}}",
    "value": "Heyth is Admin!"
  },
  {
   "name": "{{cliBuTree}}",
   "value": "I`m a treeee!"
 }
 ],
 "from": "string",
 "subject": "string",
 "message": "string",
 "isHtmlBody": true,
 "recipients": [
   {
     "recipient": "[email protected]"
   }
 ],
  "cCRecipients": [
    {
      "recipient": "string"
    }
  ],
  "bCCRecipients": [
    {
      "recipient": "string"
    }
  ]
}

EmailUrlDto

public class EmailUrlDto : EmailDto
{
    [Required]
    [JsonPropertyName("templateUrl")]
    public string TemplateUrl { get; set; }

    [Required]
    [JsonPropertyName("paramsHtml")]
    public ParamsHtmlTemplateDto[] ParamsHtml { get; set; }
}

EmailDto

public class EmailDto : Dto
{

    [JsonIgnore]
    public int Id { get; set; }


    [JsonPropertyName("from")]
    [MaxLength(250)]
    public string From { get; set; }


    [JsonPropertyName("subject")]
    [Required]
    [MaxLength(300)]
    public string Subject { get; set; }


    [JsonPropertyName("message")]
    [Required]
    public string Message { get; set; }


    [JsonPropertyName("isHtmlBody")]
    public bool IsHtmlBody { get; set; }


    [JsonIgnore]
    public EStatus EmlStatus { get; set; }


    [JsonPropertyName("smtp")]
    public SMTPConfigurationDto Smtp { get; set; }


    [JsonIgnore]
    public AttachmentDto[] Attachments { get; set; }


    [JsonPropertyName("recipients")]
    public ToRecipientDto[] Recipients { get; set; }


    [JsonPropertyName("cCRecipients")]
    public CcRecipientDto[] CCRecipients { get; set; }


    [JsonPropertyName("bCCRecipients")]
    public BccRecipientDto[] BCCRecipients { get; set; }


    [JsonIgnore]
    public LogDto[] Logs { get; set; }

}
Luella answered 12/3, 2020 at 9:31 Comment(4)
The attributes you are using to name the properties are from System.Text.Json.Serialization namespace. (JsonPropertyName for instance). This is for the use by the framework during serialization/deserialization. Add also, Newtonsoft's JsonProperty(string) attribute and it should work as expected.Bucentaur
@OguzOzgul Do you mean to add JsonProperty(string) on top of what I currently have now in the model?Luella
Yes, I guess it will solve the deserialization problem.Bucentaur
You can post it as an answer so that I will accept it if you want :). ThanksLuella
B
13

The attributes you are using to name the properties are from System.Text.Json.Serialization namespace (JsonPropertyName for instance) and are used by the System.Web.Script.Serialization.JavaScriptSerializer class during serialization/deserialization.

You should add also, Newtonsoft's JsonProperty(string) attribute and it should work as expected

[JsonProperty("from")]
[JsonPropertyName("from")]
[MaxLength(250)]
public string From { get; set; }
Bucentaur answered 12/3, 2020 at 10:10 Comment(0)
T
2

I know this is old but I just wanted to clarify something in case someone ran into the same issue as I did. In my case, I was using the System.Text.Json and Newtonsoft serializers in the wrong places.

As Oguz Ozgul mentioned JsonConvert.DeserializeObject<T> is a Newtonsoft function thus it can serialize/deserialize properties decorated with Newtonsoft Attribute (i.e JsonProperty) correctly. See this for usage

If we want to serialize/deserialize properties decorated with JsonPropertyName which is an Attribute from System.Text.Json then make sure you are using JsonSerializer.Deserialize<T>. See this for usage.

Make sure you use the proper Attribute and the correct Serializer/Deserializer.

Tic answered 10/11, 2022 at 17:2 Comment(1)
Yep, using the same package to Serialize and Deserialize is important. Because System.Text.Json and Newtonsoft are not the same things.Advisory

© 2022 - 2024 — McMap. All rights reserved.