3

I have a problem when deserializing JSON with a string property value of "0". It gets deserialized to null.

{
  "name":"Some name",
  "isConfigProperty":true,
  "displayProperty":false,
  "default":"0"
}

This is my class to deserialize to:

public class PropertyModel
{
   public string Name { get; set; }   
   public bool IsConfigProperty { get; set; }
   public bool DisplayProperty { get; set; }
   public string Default { get; set; }
}

When I change Default value from "0" to "1" or any other number/value it deserializes as it should. But when I change it to "0", I get null again.

Does anyone know about this? Is it possibly a bug in NewtonsoftJson or am I missing something?

Thank you.

Connor Low
  • 5,900
  • 3
  • 31
  • 52
spacolino
  • 31
  • 2
  • 2
    Why not changing the type of Default to int instead of string ? – CodeNotFound May 19 '21 at 12:10
  • 2
    Can you provide a stand-alone sample of code showing that happening? I can't reproduce this problem – Martin May 19 '21 at 12:13
  • 2
    Default value is not always a number. It can be also "S" or "B".. or some other string, so changing the type to int is not a solution. – spacolino May 19 '21 at 12:17
  • Are you receiving this data via HttpRequest? Which version of asp.net core are you using? – Andre.Santarosa May 19 '21 at 12:56
  • does it happen if the property is called something other than Default? – Mister Magoo May 19 '21 at 13:01
  • If you are building a new application, try to use `Text.Json` instead of 'newtosoft' – Nb777 May 19 '21 at 14:50
  • 1
    Can't reproduce with a simple Json.NET unit test, see https://dotnetfiddle.net/qymCGq. Can you please [edit] your question to share a [mcve]? Can't reproduce with `System.Text.Json` either, see https://dotnetfiddle.net/VaQU3M. (`System.Text.Json` does require setting `PropertyNameCaseInsensitive = true` since the JSON is camel cased and the model is pascal cased.) – dbc May 19 '21 at 15:14
  • 1
    All I can think is that you must have some global `JsonConverter` in `JsonSerializerSettings.Converters` that is modifying the string value. See [How to set json serializer settings in asp.net core 3?](https://stackoverflow.com/a/58392090/3744182) to see how a global converter might be getting set, then check your code base to see if this is happening. Or you might have some custom contract resolver in `JsonSerializerSettings.ContractResolver` that is applying special logic for properties named `Default`. – dbc May 19 '21 at 15:21
  • Can't reproduce the problem in my asp.net core application, you could check [the screenshot](https://i.stack.imgur.com/ta10f.gif). Can you tell us which version of Asp.net Core version and Newtonsoft.Json version you are using? – Zhi Lv May 20 '21 at 02:59

2 Answers2

0

You can use JsonConverterAttribute annotation: to define type for a property.

public enum UserStatus
{
    NotConfirmed,
    Active,
    Deleted
}

public class User
{
    public string UserName { get; set; }

    [JsonConverter(typeof(StringEnumConverter))]
    public UserStatus Status { get; set; }
}

Doc

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

it was solved. Something strange must have happened in Visual Studio debugger or maybe I just don't know that (I'm new in ASP.NET/Core/Microsoft) As it looks when debugging Blazor webassembly sometimes or in cases I don't know some variables are shown with null value when debugging... when in reallity they are not null. I figured it out when I implemented logging to file and "default":"0" was 0 and not null.

Also thank you for all the answers you provided and sorry for causing this issue before I tried with all the solutions.

spacolino
  • 31
  • 2