-1

I'm facing a problem doing post requests via .Net6 Web API. I'm a bit confused about it. Since .Net6 we prefer to use System.Text.Json in place of Newtonsoft.Json

I have one Bool property in Request Model "isAPRenew"

If I am passing "isAPRenew": "true" as a string, the entire model becomes null. this is happening to a few other data types too.

And I can't change the data type from the front-end call for some reason.

So my ask is there any way to auto-deal such data, as Newtonsoft.Json taking care of it by default

Mazhar Hayat
  • 73
  • 1
  • 9
  • `If I am passing "isAPRenew": "true" as a string,` don't. That's a bug. That's not a bool value, that's a string. JSON has bools and just like numbers, they *aren't* quoted. If you use Swagger that property will appear as a bool, not a string. `for some reason.` that's where the bug is though. If you can't, you'll have to add a type converter to that property. – Panagiotis Kanavos Jul 14 '22 at 10:59
  • Does this answer your question? [Automatic conversion of numbers to bools - migrating from Newtonsoft to System.Text.Json](https://stackoverflow.com/questions/68682450/automatic-conversion-of-numbers-to-bools-migrating-from-newtonsoft-to-system-t) – Panagiotis Kanavos Jul 14 '22 at 11:00

1 Answers1

-1

You should use JsonConverter :

public class BoolConverter : JsonConverter<bool>
    {
        public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            switch (reader.TokenType)
            {
                case JsonTokenType.True: return true;
                case JsonTokenType.False: return false;
                case JsonTokenType.String:
                    {
                        var value = reader.GetString();
                        if (value.Equals("true", StringComparison.OrdinalIgnoreCase)
                      || value.Equals("yes", StringComparison.OrdinalIgnoreCase)
                      || value.Equals("1", StringComparison.Ordinal))
                        {
                            return true;
                        }

                        if (value.Equals("false", StringComparison.OrdinalIgnoreCase)
                            || value.Equals("no", StringComparison.OrdinalIgnoreCase)
                            || value.Equals("0", StringComparison.Ordinal))
                        {
                            return false;
                        }
                        throw new NotSupportedException($"`{value}` can't be converted to `bool`.");
                    }
                case JsonTokenType.Number:
                    {
                        return reader.TryGetInt64(out long l) ? Convert.ToBoolean(l) :
                             reader.TryGetDouble(out double d) && Convert.ToBoolean(d);
                    }


                default: throw new NotSupportedException(" can't be converted to `bool`.");
            }
        }

        public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
        {
            writer.WriteBooleanValue(value);
        }

And you should add your converter in startup:

    builder.Services.AddControllers()
        .AddJsonOptions(options => options.JsonSerializerOptions.Converters
.Add(new BoolConverter()));
vahid tajari
  • 1,163
  • 10
  • 20