Detecting the absence of values when desirializing json files

Asked 2 months ago, Updated 2 months ago, 2 views

You are creating a program that uses values read from json where the array is defined.

    The
  • value should be the value described in the json file.
  • Use the value of the element immediately preceding the array if the property is not listed in the json file.

If you deserialize the example json file in the following class, it will be 0 if it is not listed, so if you write 0 as a value, you will not be able to determine it.

using System.text.Json;
internal class value
{
    public int front {get;set;}
    public int back {get;set;}
}

Example json file

[
  {
    "Front": 2000,
    "Back": 2000,
  },
  {
    "Back": 3000,
  },
  {
    "Front"—0,
  }
]

c# json

2022-09-30 14:11

2 Answers

It depends on what you use for JSON parsers, but in general, null tolerance type can be used to determine whether it is null or not.
Specifically, try changing it as follows:

internal class value
{
  public int?front {get;set;}
  public int? Back {get;set;}
}


2022-09-30 14:11

I don't know what the de-serializer I'm using, but if I use System.Text.Json or Newtonsoft.Json, I'm not sure.

Use previously listed values if properties are not listed in the json file

I don't know what the "previously listed value" in is, but if you use the value of the element immediately preceding the array, you can do the following on condition that you do not change the definition of the Value class: .NET 6.0 console app.

# nullable disable
using System.Text.Json;
using Newtonsoft.Json;
usingNewtonsoft.Json.Linq;

US>//#string [email protected]"[
  {
    Front: 2000,
    "Back": 2000,
  },
  {
    "Back": 3000,
  },
  {
    "Front" : 0,
  },
  {
    "Front": 555,
    "Back":666,
  },
  {
  }
]";

// System.Text.Json
JsonElement jsonElement=System.Text.Json.JsonSerializer.Deserialize<JsonElement>(json,
    new JsonSerializerOptions
    {
        AllowTrailingCommas=true
    });
List<Value>list1 = new List<Value>();;
if(jsonElement.ValueKind==JsonValueKind.Array)
{
    int prevFront=-1;
    int prevBack=-1;
    bool isFrontSet, isBackSet;
    foreach(JsonElementjeremInArray in jsonElement.EnumerateArray())
    {
        if(jeremInArray.ValueKind==JsonValueKind.Object)
        {
            value = new Value();
            isFrontSet=false;
            isBackSet=false;
            foreach(JsonProperty jprop in jelemInArray.EnumerateObject())
            {
                if(jprop.Name=="Front")
                {
                    value.Front=prevFront=jprop.Value.GetInt32();
                    isFrontSet = true;
                }
                if(jprop.Name=="Back")
                {
                    value.Back=prevBack=jprop.Value.GetInt32();
                    isBackSet = true;
                }                
            }

            if(isFrontSet==false)
            {
                value.Front=prevFront;
                isFrontSet = true;
            }
            if(isBackSet==false)
            {
                value.Back =prevBack;
                isBackSet = true;
            }

            list1.Add(value);
        }
    }
}

// Newtonsoft.Json
JToken jtoken=JsonConvert.DeserializeObject<JToken>(json);
List<Value>list2 = new List<Value>();;
if(jtoken is JArray)
{
    int prevFront=-1;
    int prevBack=-1;
    bool isFrontSet, isBackSet;
    foreach (JToken jtokenInArray in (JAray) jtoken)
    {
        if(jtokenInArray is JObject)
        {
            value = new Value();
            isFrontSet=false;
            isBackSet=false;
            foreach (KeyValuePair<string, JToken>kvpin(JObject)jtokenInArray)
            {
                if(kvp.Key=="Front")
                {
                    value.Front=prevFront=(int)kvp.Value;
                    isFrontSet = true;
                }
                if(kvp.Key=="Back")
                {
                    value.Back=prevBack=(int)kvp.Value;
                    isBackSet = true;
                }
            }

            if(isFrontSet==false)
            {
                value.Front=prevFront;
                isFrontSet = true;
            }
            if(isBackSet==false)
            {
                value.Back =prevBack;
                isBackSet = true;
            }

            list2.Add(value);
        }
    }
}

public class value
{
    public int front {get;set;}
    public int back {get;set;}
}

The results are as follows (Visual Studio debug image):

Enter a description of the image here


2022-09-30 14:11

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.