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.

9
  • 2
    Why not changing the type of Default to int instead of string ? Commented May 19, 2021 at 12:10
  • 2
    Can you provide a stand-alone sample of code showing that happening? I can't reproduce this problem Commented May 19, 2021 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. Commented May 19, 2021 at 12:17
  • 1
    Can't reproduce with a simple Json.NET unit test, see dotnetfiddle.net/qymCGq. Can you please edit your question to share a minimal reproducible example? Can't reproduce with System.Text.Json either, see dotnetfiddle.net/VaQU3M. (System.Text.Json does require setting PropertyNameCaseInsensitive = true since the JSON is camel cased and the model is pascal cased.) Commented May 19, 2021 at 15:14
  • 1
    All I can think is that you must have some global JsonConverter<string> in JsonSerializerSettings.Converters that is modifying the string value. See How to set json serializer settings in asp.net core 3? 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. Commented May 19, 2021 at 15:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to StackOverflow. How does this relate to the OP's question?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.