Ignore JSON types

jrahma 111 Reputation points
2021-01-03T18:22:42.4+00:00

Hi,

I am using MySQL at backend and I have problems with two fields when using System.Text.Json

The fields are mobile which has a value for example = 026547388

and is_admin which has 0 or 1 (representing true or false in MySQL)

When I use the Microsoft Json I get the following error:

The JSON value could not be converted to System.Boolean. Path: $[0].is_admin | LineNumber: 0 | BytePositionInLine: 383.

my is_admin is declared like this:

public bool is_admin { get; set; }

and mobile is declared this way:

public int mobile { get; set; }

regarding mobile, field, if I set It as int then I will be losing the 0 at the left and my number will be 026547388

Kindly help

Thanks,
Jassim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,458 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 113.7K Reputation points
    2021-01-03T19:24:11.633+00:00

    One of simple approaches is shown in the next example:

    class MyData  
    {  
    	[JsonIgnore]  
    	public bool is_admin { get; set; }  
      
    	[EditorBrowsable( EditorBrowsableState.Never )]  
    	[JsonPropertyName( "is_admin" )]  
    	public int is_admin_as_int  
    	{  
    		get  
    		{  
    			return is_admin ? 1 : 0;  
    		}  
    		set  
    		{  
    			is_admin = value != 0;  
    		}  
    	}  
      
    	public string mobile { get; set; }  
    }  
      
    // sample usage:  
    var data = new MyData { is_admin = true, mobile = "026547388" };  
    string json = JsonSerializer.Serialize( data ); // {"is_admin":1,"mobile":"026547388"}  
    MyData data2 = JsonSerializer.Deserialize<MyData>( json );  
    

    See also the topics related to converters, for example: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to.

    2 people found this answer helpful.

  2. Viorel 113.7K Reputation points
    2021-01-06T10:00:27.857+00:00

    If you already tried the second suggestion (using custom converters), then check this example too:

    UserDetails data1 = new UserDetails { user_id = 100, first_name = "John", is_active = false, is_admin = true, mobile = "026547388" };
    var options = new JsonSerializerOptions { Converters = { new MyConverter( ) } };
    string json = JsonSerializer.Serialize( data1, options );
    // result: {"user_id":100,"first_name":"John","last_name":null,"is_admin":1,"is_active":0,"mobile":"026547388"}
    UserDetails data2 = JsonSerializer.Deserialize<UserDetails>( json, options );
    

    where:

    public class UserDetails
    {
        public int user_id { get; set; }
        public string first_name { get; set; }
        // . . .
        public bool is_admin { get; set; }
        public bool is_active { get; set; }
    
        public string mobile { get; set; }
    }
    
    
    public class MyConverter : JsonConverter<bool>
    {
        public override bool Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options ) => Convert.ToBoolean( reader.GetInt16( ) );
    
        public override void Write(
            Utf8JsonWriter writer,
            bool value,
            JsonSerializerOptions options ) => writer.WriteNumberValue( Convert.ToInt16( value ) );
    }
    

    See the documentation for other details about converters.

    1 person found this answer helpful.
    0 comments No comments