JSON Deserialization with property default value not working - json

I am using Newtonsoft to deserialize data from a file. When I deserialize two different instances from two different sets of data, both instances' property ends up having the same value. I have created a small project to repro the issue. Here are my 2 JSON files
File1.json:
{
"Name": "File1",
"SomeProperty":
{
"Value": 1
}
}
File2.json:
{
"Name": "File2",
"SomeProperty":
{
"Value": 2
}
}
SomeProperty.cs
namespace Json
{
public class SomePropertyDto
{
public static SomePropertyDto Default = new SomePropertyDto
{
Value = 0
};
public int Value { get; set; }
}
}
FileDataDto.cs
namespace Json
{
public class FileDataDto
{
public string Name { get; set; }
public SomePropertyDto SomeProperty
{
get => someProperty;
set => someProperty = value;
}
private SomePropertyDto someProperty = SomePropertyDto.Default;
}
}
Program.cs
using System.IO;
using Newtonsoft.Json;
namespace Json
{
class Program
{
static void Main(string[] args)
{
string json1 = File.ReadAllText("File1.json");
string json2 = File.ReadAllText("File2.json");
FileDataDto fileData1 = JsonConvert.DeserializeObject<FileDataDto>(json1);
FileDataDto fileData2 = JsonConvert.DeserializeObject<FileDataDto>(json2);
}
}
}
After deserializing both instances of FileDataDto, both their SomeProperty values are the same. However if I do not initialise the FileDataDto someProperty field to SomePropertyDto.Default,
private SomePropertyDto someProperty;// = SomePropertyDto.Default;
it works correctly. If I include the initialisation to the default value
private SomePropertyDto someProperty = SomePropertyDto.Default;
after deserializing fileData1, the SomeProperty value equals 1 as expected. However, after deserializing fileData2, both fileData1 and FileData2 instances' SomeProperty value equals 2 which is not what is expected.

According to https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/JsonSerializerSettings.cs#L46, the default object creation setting is "Auto", which means https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/ObjectCreationHandling.cs#L34
Reuse existing objects, create new objects when needed.
So when your Default object is there, someProperty stay this, the same, shared object for all FileDataDto instances.
Provide customized JsonSerializerSettings (with ObjectCreationHandling set to Replace) if you need that Default value.

Related

Reading JSON values from web browser?

I have a random JSON generated online and I am able to print all the values. But how do I read each array separately? For example, the below JSON contains different attributes, how do I read the string name that is an array containing 4 values.
JSON reader:
public class JsonHelper
{
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
[System.Serializable]
public class RootObject
{
public string name;
public string height;
public string mass ;
}
The below script is used to access the JSON online through RESTApi GET service. I am able to receive the whole text but how I read one single value of name or height or mass?
Script:
using UnityEngine.Networking;
using System.Linq;
using System.Linq.Expressions;
using UnityEngine.UI;
using System.IO;
public class GetData : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(GetNames());
}
IEnumerator GetNames()
{
string GetNameURL = "https://swapi.co/api/people/1/?format=json";
using(UnityWebRequest www = UnityWebRequest.Get(GetNameURL))
{
// www.chunkedTransfer = false;
yield return www.Send();
if(www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
if(www.isDone)
{
string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
Debug.Log(jsonResult); //I am getting the result here
}
}
}
}
}
Your API call to 'https://swapi.co/api/people/1/?format=json' returns a single object, not an array.
So after you get your json, you can access name and height etc like:
if (www.isDone)
{
string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
Debug.Log(jsonResult); //I am getting the result here
RootObject person = JsonUtility.FromJson<RootObject>(jsonResult);
// then you can access each property
Debug.Log(person.name);
Debug.Log(person.height);
}

type handling by Jackson/Spring conversion of JSON to java POJO

I'm using
Spring 3.1.0.RELEASE
Jackson 1.9.5
I'm using org.springframework.web.client.RestTemplate's getForObject() method:
getForObject(String url, Class<?> responseType, Map<String, ?> urlVariables) throws RestClientException
Here's my JSON:
{
"someObject": {
"someKey": 42,
},
"key2": "valueA"
}
Here's the POJO used to hold it:
SomeClass.java:
#JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"someObject",
"key2"
})
public class SomeClass {
#JsonProperty("someObject")
private SomeObject someObject;
#JsonProperty("key2")
private String key2;
#JsonProperty("someObject")
public LocationInfo getSomeObject() {
return someObject;
}
#JsonProperty("someObject")
public void setLocationInfo(SomeObject someObject) {
this.someObject = someObject;
}
}
SomeObject.java:
#JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"someKey"
})
public class SomeObject{
#JsonProperty("someKey")
private String someKey;
#JsonProperty("someKey")
public String getSomeKey() {
if(someKey==null){
someKey = "";
}
return someKey.toUpperCase();
}
#JsonProperty("someKey")
public void setSomeKey(String someKey) {
this.someKey = someKey;
}
}
It works. Given the JSON structure, I get a String value of "42" in the property someKey of class SomeObject
I don't understand why. Is there some magical conversion going on behind the scenes of which I'm unaware?
Can the conversion be counted on? Also, i'm not currently getting any whitespace at the beginning or end of the String someKey. Is that something I can count on as well, since the integer value cannot have any whitespace?
Check out the code at https://github.com/joelittlejohn/jsonschema2pojo if you want to really understand how it works.
Yes the conversion can be counted on, yes you can count on their not being whitespace in the String in the pojo.
In a nutshell the fields from the JSON file are read in, then these get mapped to the member variables/setter methods of the Pojos that is passed in as your responseType.

Ignore parsing errors during JSON.NET data parsing

I have an object with predefined data structure:
public class A
{
public string Id {get;set;}
public bool? Enabled {get;set;}
public int? Age {get;set;}
}
and JSON is supposed to be
{ "Id": "123", "Enabled": true, "Age": 23 }
I want to handle JSON error in positive way, and whenever server returns unexpected values for defined data-types I want it to be ignore and default value is set (null).
Right now when JSON is partially invalid I'm getting JSON reader exception:
{ "Id": "123", "Enabled": "NotABoolValue", "Age": 23 }
And I don't get any object at all.
What I want is to get an object:
new A() { Id = "123", Enabled = null, Age = 23 }
and parsing warning if possible.
Is it possible to accomplish with JSON.NET?
To be able to handle deserialization errors, use the following code:
var a = JsonConvert.DeserializeObject<A>("-- JSON STRING --", new JsonSerializerSettings
{
Error = HandleDeserializationError
});
where HandleDeserializationError is the following method:
public void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
{
var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = true;
}
The HandleDeserializationError will be called as many times as there are errors in the json string. The properties that are causing the error will not be initialized.
Same thing as Ilija's solution, but a oneliner for the lazy/on a rush (credit goes to him)
var settings = new JsonSerializerSettings { Error = (se, ev) => { ev.ErrorContext.Handled = true; } };
JsonConvert.DeserializeObject<YourType>(yourJsonStringVariable, settings);
Props to Jam for making it even shorter =)
There is another way. for example, if you are using a nuget package which uses newton json and does deseralization and seralization for you. You may have this problem if the package is not handling errors. then you cant use the solution above. you need to handle in object level. here becomes OnErrorAttribute useful. So below code will catch any error for any property, you can even modify within the OnError function and assign default values
public class PersonError
{
private List<string> _roles;
public string Name { get; set; }
public int Age { get; set; }
public List<string> Roles
{
get
{
if (_roles == null)
{
throw new Exception("Roles not loaded!");
}
return _roles;
}
set { _roles = value; }
}
public string Title { get; set; }
[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
errorContext.Handled = true;
}
}
see https://www.newtonsoft.com/json/help/html/SerializationErrorHandling.htm

How can I do JSON serializer ignore navigation properties?

I am exactly in the same case that this question:
How do I make JSON.NET ignore object relationships?
I see the proposed solution and I know I must use a Contract Revolver, and I also see the code of the Contract Resolver, but I do not know how to use it.
Should I use it in the WebApiConfig.vb?
Should I modify my Entity Model anyway?
It is a useful question👍 and I hope this help:
A)
If you have created your models manually (without Entity Framework), mark the relation properties as virtual first.
If your models were created by EF, It has already done it for you and each Relation Property is marked as virtual, as seen below:
Sample class:
public class PC
{
public int FileFolderId {get;set;}
public virtual ICollection<string> Libs { get; set; }
public virtual ICollection<string> Books { get; set; }
public virtual ICollection<string> Files { get; set; }
}
B)
Those relation properties can now be ignored by the JSON serializer by using the following ContractResolver for JSON.NET:
CustomResolver:
class CustomResolver : DefaultContractResolver
{
private readonly List<string> _namesOfVirtualPropsToKeep=new List<string>(new String[]{});
public CustomResolver(){}
public CustomResolver(IEnumerable<string> namesOfVirtualPropsToKeep)
{
this._namesOfVirtualPropsToKeep = namesOfVirtualPropsToKeep.Select(x=>x.ToLower()).ToList();
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty prop = base.CreateProperty(member, memberSerialization);
var propInfo = member as PropertyInfo;
if (propInfo != null)
{
if (propInfo.GetMethod.IsVirtual && !propInfo.GetMethod.IsFinal
&& !_namesOfVirtualPropsToKeep.Contains(propInfo.Name.ToLower()))
{
prop.ShouldSerialize = obj => false;
}
}
return prop;
}
}
C)
Finally, to serialize your model easily use the above ContractResolver. Set it up like this:
// -------------------------------------------------------------------
// Serializer settings
JsonSerializerSettings settings = new JsonSerializerSettings
{
// ContractResolver = new CustomResolver();
// OR:
ContractResolver = new CustomResolver(new []
{
nameof(PC.Libs), // keep Libs property among virtual properties
nameof(PC.Files) // keep Files property among virtual properties
}),
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
};
// -------------------------------------------------------------------
// Do the serialization and output to the console
var json = JsonConvert.SerializeObject(new PC(), settings);
Console.WriteLine(json);
// -------------------------------------------------------------------
// We can see that "Books" filed is ignored in the output:
// {
// "FileFolderId": 0,
// "Libs": null,
// "Files": null
// }
Now, all the navigation (relation) properties [virtual properties] will be ignored automatically except you keep some of them by determine them in your code.😎
Live DEMO
Thanks from #BrianRogers for his answer here.
If you are using Newtonsoft.Json
Mark field with
Newtonsoft.Json.JsonIgnore
Instead of
System.Text.Json.Serialization.JsonIgnore

JSON Deserialization of Cast object returns Uncast JSON

I'm currently doing some work on an API where I'm Casting an object before serializing it and returning the JSON. I expected the JSON result to be that of the Cast object and not the Uncast object, however I get all the properties in my JSON from the Uncast object
Example code
using System;
using Newtonsoft.Json;
namespace JSONSerializationOfCastObject
{
class Program
{
static void Main(string[] args)
{
B InstanceB = new B(){PropA = "A",PropB = "B"};
A InstanceA = InstanceB;
var JSONInstanceA = JsonConvert.SerializeObject(InstanceA);
Console.WriteLine(JSONInstanceA);
Console.ReadLine();
}
}
public class A
{
public string PropA { get; set; }
}
public class B:A
{
public string PropB { get; set; }
}
}
Result
{"PropB":"B","PropA":"A"}
Expected result
{"PropA":"A"}
Another example where the type just isn't what you expect
B InstanceB = new B(){PropA = "A",PropB = "B"};
A InstanceA = InstanceB;
var x = InstanceA.GetType() == typeof(A); //==> False but we casted it to A
I just cannot figure this out, NewtonSoft must do some reflection under the hood.
GitHub Example here: https://github.com/tharris29/JSONSerializationOfCastObject/tree/master
update
So I understand this is to do with reflection but just seems an odd result. Is there a way to tell the serializer what object type to use for serialization?
Just because you have assigned a B to an A does not mean that it is no longer a B. You can see this for yourself if you print out the type of your InstanceA variable after the assignment:
B instanceB = new B() { PropA = "A", PropB = "B" };
A instanceA = instanceB;
Console.WriteLine(instanceA.GetType().Name);
As you will see, the result is B.
Json.Net uses reflection to look at the actual type of an object and get all of its properties. As far as I know, it does not have a built-in way to limit the properties to just those of a base type. If you want to do that, you will need a custom JsonConverter. Here is one that might work for you (notice it also uses reflection):
public class BaseTypeConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
T instance = (T)value;
JObject obj = new JObject();
foreach (PropertyInfo prop in typeof(T).GetProperties())
{
if (prop.CanRead)
{
obj.Add(prop.Name, JToken.FromObject(prop.GetValue(instance)));
}
}
obj.WriteTo(writer);
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You can use this converter like this:
B instanceB = new B() { PropA = "A", PropB = "B" };
// serialize B, but only include the properties from type A
string json = JsonConvert.SerializeObject(instanceB, new BaseTypeConverter<A>());
Console.WriteLine(json);
Output:
{"PropA":"A"}