You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the json payload does not have some values to serialize them into a struct, how to set such values to a custom default?
Nullable types aren't the choice because I use .net framework 4.5.
The text was updated successfully, but these errors were encountered:
denchInside
changed the title
About struct parsing
Default values for values that didn't exist
Mar 17, 2022
Well, I have solved the problem by changing 3 lines of code:
use a class instead of a struct
use a predefined field (this would be the default value) and a property referencing it instead of an automatic property ( {get; set;} )
in JSONParser.cs, line 334, change object instance = FormatterServices.GetUninitializedObject(type) (analog to default(type)) to object instance = Activator.CreateInstance(type); (analog to new ClassName().)
Now, the predefined values will stay untouched.
This variant wouldn't work for classes which doesn't have empty constructors.
So, maybe you will need to add object defaultObject into parameters of public static T FromJson<T>(this string json) (and put the defaultObject to all ParseObject() calls) and static object ParseObject(Type type, string json) (and set object instance to the defaultObject too) to manually set the default object if needed.
If the json payload does not have some values to serialize them into a struct, how to set such values to a custom default?
Nullable types aren't the choice because I use .net framework 4.5.
The text was updated successfully, but these errors were encountered: