0

I use the exist API and can't change it.

So I have some variable - CellProviderID.

It looks like it is an int, because when I set the int value the server returns the expected response.

"CellProviderID":5,"CellProviderID2":7,"CellProviderID3":2

The problem appears when I leave this value empty and after serialization I get:

"CellProviderID":0,"CellProviderID2":0,"CellProviderID3":0

because 0 is default value for int type. And the server returns an error:

{"ErrorCode":10,"ErrorMessage":"Cell provider was specified, but cell number is blank, Pager provider was specified, but pager number is blank"}

So it looks rather like it's some enum, and 0 hasn't a value.

It works well when I change serialized json from

"CellProviderID":0,"CellProviderID2":0,"CellProviderID3":0

to

"CellProviderID":"","CellProviderID2":"","CellProviderID3":""

How should I initialize the properties to be able to setup int values and get "" when I leave this property empty?

[JsonProperty]
public int CellProviderID { get; set; }
[JsonProperty]
public int CellProviderID2 { get; set; }
[JsonProperty]
public int CellProviderID3 { get; set; }
3
  • 1
    how about trying to make your properties of type int? that way they can be null (and see how the serialization reacts to that) Commented Nov 17, 2015 at 11:51
  • they are integer. And default value for integer is 0 insted of null. Commented Nov 17, 2015 at 11:53
  • 3
    @iPogosov He means int?, not int with a question mark. int? is shorthand form for Nullable<int> which means the type can be null or an int. That is, change your property to : public int? CellProviderID { get; set; } Commented Nov 17, 2015 at 11:58

2 Answers 2

3

you can change return type of your properties from int to int?, so you will get null value as default.

It will be serialized as

"CellProviderID":null,"CellProviderID2":null,"CellProviderID3":null

so you can have a deal with that.

Next, you must change your serialization to ignore null values. If you're using Json.Net, you serialization must look like:

JsonConvert.SerializeObject(movie,Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })

So, your server will not receive non-initialized values

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

Comments

2

You could optionally write a Custom JsonConverter and handle the custom serialization/deserialization yourself.

internal class Inherited
{
    public int MyProperty { get; set; }
    public int MyProperty2 { get; set; }
}

internal class CustomConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Inherited);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var data = value as Inherited;
        if (data != null)
        {
            writer.WriteStartObject();
            foreach (var item in typeof(Inherited).GetProperties())
            {
                writer.WritePropertyName(item.Name);
                switch (item.PropertyType.Name)
                {
                    case "Int32":
                        {
                            writer.WriteValue(default(int) == (int)item.GetValue(data, null) ? "" : item.GetValue(data, null).ToString());
                            break;
                        }
                }
            }

            writer.WriteEnd();
        }
    }
}

JsonSerializerSettings obj = new JsonSerializerSettings();
obj.Converters.Add(new CustomConverter());
var result = JsonConvert.SerializeObject(new Inherited(0) { MyProperty = 0, MyProperty2 = 0 }, obj);

Results in - {"MyProperty":"","MyProperty2":""}

2 Comments

As I understand if I have in the Inherited class int properties, when I can set default int value(0), I will get always ""?
Give it a try. I think so.

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.