I need to serialize some custom objects in order to store information. However, I am struggling to deserialize those objects from the serialized JSON string back into their original object forms.
The serialized string seems fine:
[
{
"MyStringArray": [
"stringInput1",
"stringInput2"
],
"MyCharArray": [
"a",
"b",
"c",
"."
],
"MyString": "dummy",
"MyClass3Object": [
{
"MyString": "ListInput1"
},
{
"MyString": "ListInput2"
}
]
}
]
However, when I reconstruct the original MyClass1 object, the list has one entry as it should but it is filled with nulls instead of the corresnponding data. Any ideas as to what may be happening? Thanks in advance for the brainstorming :)
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using System.Text.RegularExpressions;
namespace JsonTesting
{
class Program
{
static void Main(string[] args)
{
MyClass1 c1 = new MyClass1();
c1.AddInfo();
string toJsonString = JsonConvert.SerializeObject(c1, Formatting.Indented,
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Include });
File.WriteAllText(#"C:\temp\dumpJsonText.txt",toJsonString);
MyClass1 fromJson = JsonConvert.DeserializeObject<MyClass1>(toJsonString);
Console.ReadLine();
}
}
public class MyClass1 : List<MyClass2> {
public MyClass1() { }
public void AddInfo() {
this.Add(new MyClass2(new string[] { "stringInput1", "stringInput2" },
new char[] { 'a', 'b', 'c', '.' },
"dummy",
new List<MyClass3>() { new MyClass3("ListInput1", new Regex(#"[A-Z]")), new MyClass3("ListInput2", new Regex(#"[0-9]")) }
));
}
}
public class MyClass2
{
private string[] _myStringArray = null;
private char[] _myCharArray = null;
private string _myString = null;
private List<MyClass3> _myClass3Object = null;
public MyClass2() { }
public MyClass2(string[] myStringArray, char[] myCharArray, string myString, List<MyClass3> myClass3Object)
{
_myStringArray = myStringArray;
_myCharArray = myCharArray;
_myString = myString;
_myClass3Object = myClass3Object;
}
public string[] MyStringArray { get { return _myStringArray; } }
public char[] MyCharArray { get { return _myCharArray; } }
public string MyString { get { return _myString; } }
public List<MyClass3> MyClass3Object { get { return _myClass3Object; } }
}
public class MyClass3 {
private Regex _myRegex;
private string _myString = null;
public MyClass3() { }
public MyClass3(string myString, Regex myRegex) {
_myString = myString;
_myRegex = myRegex;
}
public string MyString{ get {return _myString;} }
}
}
Your classes MyClass2 and MyClass3 are read-only. In order for Json.NET to deserialize a read-only type, you must either provide a custom JsonConverter that manually deserializes and constructs an instance of the type, or provide a parameterized constructor whose argument names match the property names modulo case. You have already created the necessary constructors and so are halfway done.
However your types have parameterless constructors as well. So, which constructor does Json.NET call? For a non-enumerable type that is serialized to a JSON object, the following rules apply:
If [JsonConstructor] is set on a constructor, use that constructor.
Next, in full trust only, when MemberSerialization.Fields is applied, or [Serializable] is applied and DefaultContractResolver.IgnoreSerializableAttribute == false, the special method FormatterServices.GetUninitializedObject() is used to allocate the object. None of the type's constructors are called.
(This is an unusual case that does not arise often.)
Next, if there is a public parameterless constructor, use it.
Next, if a private parameterless constructor exists and the setting ConstructorHandling.AllowNonPublicDefaultConstructor
is enabled, the private parameterless constructor is used.
Next, if there is a single public parameterized constructor, use that constructor.
Failing all of the above, Json.NET cannot construct instances of the type. An exception will get thrown during deserialization unless a custom converter is available.
Thus the parameterless constructors take precedence over the parameterized constructors. To force the parameterized constructors to be used, mark them with [JsonConstructor] as mentioned above:
public class MyClass3
{
private Regex _myRegex;
private string _myString = null;
public MyClass3() { }
[JsonConstructor]
// The argument names must match the property names modulo case for Json.NET to deserialize the properties successfully.
public MyClass3(string myString, Regex myRegex)
{
_myString = myString;
_myRegex = myRegex;
}
public string MyString { get { return _myString; } }
public Regex MyRegex { get { return _myRegex; } }
}
Alternatively, you could eliminate the parameterless constructor as it apparently did not exist in the first version of your question. Then make the same change to MyClass2. Now your types will deserialize successfully.
Note that Json.NET has a built-in converter for serializing a Regex.
Sample fiddle.
Related
Is it possible to adjust JsonSerializerSettings that way?
(I guess not) but still have some hope, cause I'm not very experienced with their API.
By default and with all settings I've tried missing int either deserialized to 0 or causing exception.
Example
{
"defaultCaptionLineNum": 6,
"worksheets": {
"0": { "name": "Ws1", "caption_ln": 6 },
"1": { "name": "Ws2", "caption_ln": null },
"2": { "name": "Ws3" },
"3": { "name": "Ws4", "caption_ln": 5 },
}
}
Currently by default caption line (caption_ln) of Ws3 evaluated to 0, I want it to be evaluated to null (as in Ws2).
I'm using
jObject.ToObject<MyClass>(JsonSerializer.Create(settings));
to get object with worksheet(Ws) information from JSON (tried it without serializer as well)
and any variations of Include and Ignore here don't make jObject deserialize missing ints in json differently (with the exception of throwing error right away for missing values)
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
MissingMemberHandling = MissingMemberHandling.Ignore
};
Other serializer settings seem to be irrelevant.
UPD: Listing of MyObject MyClass.
public class MyClass
{
public class WorkSheet
{
[JsonProperty("name")]
string wsName;
[JsonProperty("caption_ln")]
int? captionLineNumber;
public WorkSheet(string name, int captionln)
{
wsName = name;
captionLineNumber = captionln;
}
}
private int _defaultCaptionLineNum;
[JsonProperty("defaultCaptionLineNum")]
public int DefaultCaptionLineNum { get => _defaultCaptionLineNum; }
private Dictionary<int, WorkSheet> _worksheets = new Dictionary<int, WorkSheet>();
[JsonProperty("worksheets")]
public Dictionary<int, WorkSheet> Worksheets { get => _worksheets; set => _worksheets = value; }
public MyClass()
{
Console.WriteLine("New MyClass object created!");
}
}
Your problem is that your type WorkSheet has only one constructor, which is parameterized:
public class WorkSheet
{
public WorkSheet(string name, int captionln)
{
wsName = name;
captionLineNumber = captionln;
}
// Remainder omitted
}
As explained in the documentation page ConstructorHandling Enumeration, Json.NET will fall back to a single parameterized constructor when there is no parameterless constructor, which is the case here. For the constructor arguments, Json.NET will match the JSON parameters to constructor arguments by name (modulo case), deserializing to the argument type if present or passing a default value if not present. And since there is no "captionln" parameter present in your JSON, captionLineNumber gets initialized to default(int), which is 0. This explains the problem you are seeing.
To fix the problem, you can change captionln to be of type int?:
public class WorkSheet
{
public WorkSheet(string name, int? captionln)
{
wsName = name;
captionLineNumber = captionln;
}
// Remainder omitted
}
Working fiddle #1 here.
Alternatively, you could introduce a private serialization-specific constructor and mark it with [JsonConstructor]:
public class WorkSheet
{
[JsonConstructor]
private WorkSheet() { }
public WorkSheet(string name, int captionln)
{
wsName = name;
captionLineNumber = captionln;
}
// Remainder omitted
}
Sample fiddle #2 here.
Related questions on constructor use in Json.NET:
How does JSON deserialization in C# work (whose answer has a cool diagram).
Json.net `JsonConstructor` constructor parameter names.
JSON.net: how to deserialize without using the default constructor?.
I've used https://quicktype.io to create a class for my Json data. However I'm not really understanding why with the helper methods created to convert to json, from json, and supply default parameters they are each created differently as a static method of the class, a static class and a class with a static method.
The reason really is because as soon as I create a second class from another set of json data the code fails because of the static classes and I want to make sure I refactor them correctly. As well as understand the reasons of course.
I figure 'Converter' will never change across all my json objects so I can move this as is to a separate file and Serialize to a static method with FromJson. But I'd just like to understand more about the reasoning of how it was done in the first place and the better approach.
Here is the code:
public partial class StationDO
{
public string Active { get; set; }
//more fields here
}
public partial class StationDO
{
public static List<StationDO> FromJson(string json)
{
return JsonConvert.DeserializeObject<List<StationDO>>(json, Converter.Settings);
}
}
public static class Serialize
{
public static string ToJson(this List<StationDO> self)
{
return JsonConvert.SerializeObject(self, Converter.Settings);
}
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
If I look at the Java code the same site produces, it simply puts everything bar the bean into a single class 'Converter'
You can have all the static members in StationDO class. In that case I recommend mark that class as sealed (public sealed class StationDO) to prevent someone to inheriting from that class and use the static methods from inherited class.
public class InheritedStationDO : StationDO { }
// ... somewhere else ...
InheritedStationDO.FromJson(jsonValue); // still returns List<StationDO> not List<InheritedStationDO> !!!
EDIT:
After close look I think, the whole design of members is not good.
1) There is no need to accept just List<StationDO>.
2) There is no need the defined special methods for (de)serialization of every class, you will have. You can have just one method for serialization and one for deserialization for all your classes.
Example:
public class StationDO {
public string Active { get; set; }
}
public class AnotherDO {
public string Name { get; set; }
}
// and more *DO classes
// class need to be "static" because contains "extension methods"
public static class MySerializationHelper {
private static readonly JsonSerializerSettings serializationSettings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
// universal method for deserialization from json
// the generic type "T" represents the result type of deserialization
public static T FromJson<T>(string json) {
return JsonConvert.DeserializeObject<T>(json, serializationSettings);
}
// universal method for serialization to json
// this "this" keyword means, its "extension method"
public static string ToJson<T>(this T self) {
return JsonConvert.SerializeObject(self, serializationSettings);
}
}
Usage:
StationDO obj01 = GetSomeStation();
// returns json of one object
string json01A = obj01.ToJson(); // these two lines are equivalent
string json01B = MySerializationHelper.ToJson(obj01); // these two lines are equivalent
// returns new object deserialized from json in "json01" variable
StationDO clone01 = MySerializationHelper.FromJson<StationDO>(json01A);
StationDO obj02 = GetAnotherStation();
StationDO[] arr01 = new StationDO[] { obj01, obj02 };
// returns json of array with two objects
string json02A = arr01.ToJson(); // these two lines are equivalent
string json02B = MySerializationHelper.ToJson(arr01); // these two lines are equivalent
// returns new array with deserialized object from json in "json02" variable
StationdDO[] clone02 = MySerializationHelper.FromJson<StationdDO[]>(json02A);
AnotherDO obj03 = GetAnotherDO();
string json03A = obj03.ToJson(); // these two lines are equivalent
string json03B = MySerializationHelper.ToJson(obj03); // these two lines are equivalent
As you see, the generics is the way, how to avoid code duplication for every class.
And you can (de)serialize all kind of arrays and collections or single objects. Not just List<T>.
I have a class, that should support version tolerant serialization
[Serializable]
class A {
[OptionalField]
int a;
[OptionalField]
MyClass b;
[OptionalField]
MyClass c;
}
How can I correct missing fields after deserialization? I guess, I have to use method marked with [OnDeserializing]. But how can I get which of fields was ignored?
Can I configure auto-deserialization to initialize field by default constructor in case of them missing?
Additionally, you can use OnSerializingAttribute and OnSerializedAttribute to set the fields. As the example shows, fields that have been already set will keep their value. Note, however, that this is only the case if the field is set during the OnSerializing event. Fields set during the OnSerialized event will override the serialized value.
EDIT: In this case you can check in your method (decorated with OnSerialized) if the field equals to null and instantiate when necessary. If there is the possibility that this field is never be used and its creation can be deferred, think about hiding the field in question behind a property and instantiate it lazily.
Models.cs:
using System;
using System.Runtime.Serialization;
namespace SerializationExample
{
[Serializable]
public class Model
{
public Model(){
A = new SomeClass();
}
[OptionalField]
public int value;
[OptionalField]
public SomeClass A;
[OptionalField]
public AnotherClass B;
[OnDeserializing]
void OnDeserializing(StreamingContext context)
{
B = new AnotherClass("Set during deserializing");
}
[OnDeserialized]
void OnDeserialized(StreamingContext context)
{
// Do sth. here after the object has been deserialized
}
public override string ToString()
{
return String.Format("A: {0}\nB: {1}", A, B);
}
}
[Serializable]
public class SomeClass
{
public string Value { get; set; }
public SomeClass()
{
Value = "Default";
}
public override string ToString()
{
return Value;
}
}
[Serializable]
public class AnotherClass
{
public string Value { get; private set; }
public AnotherClass(string v)
{
Value = v;
}
public override string ToString()
{
return Value;
}
}
}
Program.cs:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializationExample
{
class Program
{
static void Main(string[] args)
{
string[] FileNames = new string[] {
#"model1.bin",
#"model2.bin"
};
Stream[] files = new Stream[] {
File.Create(FileNames[0]),
File.Create(FileNames[1])
};
BinaryFormatter bf = new BinaryFormatter();
Model m1 = new Model();
m1.B = new AnotherClass("Set in app");
m1.A.Value = "Set in app";
Model m2 = new Model();
Console.WriteLine("M1:\n{0}\n", m1);
Console.WriteLine("M2:\n{0}\n\n", m2);
bf.Serialize(files[0], m1);
bf.Serialize(files[1], m2);
foreach (var f in files)
f.Seek(0, SeekOrigin.Begin);
m1 = null;
m2 = null;
m1 = (Model)bf.Deserialize(files[0]);
m2 = (Model)bf.Deserialize(files[1]);
Console.WriteLine("M1:\n{0}\n", m1);
Console.WriteLine("M2:\n{0}\n\n", m2);
foreach (var f in files)
f.Close();
}
}
}
Output:
M1:
A: Set in app
B: Set in app
M2:
A: Default
B:
M1:
A: Set in app
B: Set in app
M2:
A: Default
B: Set during deserializing
If you just want to initialize those values to defaults, all you need is a default parameterless constructor that initializes them. This will get called during deserialization, and any missing fields will keep whatever values you initialized them to in the constructor.
If you want more control, you can implement the ISerializable interface and the proper constructor on your class (you should usually do both, though often one or the other is unneeded.)
If C# finds a constructor with the signature:
protected A ( SerializationInfo info, StreamingContext context )
{
}
it will call that constructor, and pass a weakly-typed dictionary with all of the serialization information it has. (You can use ISerializable::GetObjectData to write custom fields into the object and retrieve them in the constructor). You can use the info.GetXXX methods to extract those values.
One thing to note: if you implement this constructor, you have to do all of the work, including the fields that would normally get deserialized automatically. For any missing fields, just set them appropriately. Similarly, if you implement GetObjectData, you have to serialize everything in that method. It's pretty simple, but if you change your class you need to edit your custom method/constructor appropriately.
I tried to search for an answer for this problem but could not find much, most probably because I do not know how to look for it properly, so here it goes. All help is very much appreciated.
With the base class that looks like
abstract public class Property
{
private String name;
public Property(String propertyName)
{
name = propertyName;
}
public String Name
{
get { return name; }
}
abstract public override String ToString();
}
And derived classes that look like
public class StringProperty : Property
{
private String value; // different properties for different types
public StringProperty(String propertyName, String value) : base(propertyName)
{
this.value = value;
}
public String Value // different signature for different properties
{
get { return value; }
}
public override String ToString()
{
return base.Name + ": " + value;
}
}
During runtime, the function receives a collection of "Property" objects. What do I need to do to be able to obtain the "Value" of each? Do I need to have a big if statement to query the type of each "Property" object? If not, is there a more elegant solution?
I tried to define an abstract "Value" property to be overridden but since the return types are different, it did not work. I also tried playing with shadowing the "Value" property, but I could not make it work. The idea of using an COM-like Variant does not sound very appropriate, either.
Thanks a lot in advance.
EDIT:
I should have added details as to what I am trying to do. The properties are displayed in a Winforms app. Different "TextBox"es represent different properties and are filtered for proper input (depending on the type). The updated values are read back and stored. The container object will be serialized into JSON and deserialized on an Android and iPhone client and eventually these values will be passed into a layer running native C++ code doing OpenGL stuff. I don't know in advance the kind of all needed properties so as the middleman, I wanted to make my code as robust as possible while being able to feed the OpenGL engine.
You can use a generic class:
public class AnyProperty<T> : Property
{
private T value;
// ... etc
I'd really recommend making the base class an Interface by now:
public interface IProperty
{
public String Name { get; }
}
public class Property<T> : IProperty
{
public Property(String name, T value)
{
Name = name;
Value = value;
}
public String Name { get; private set; }
public T Value { get; private set; }
public override String ToString()
{
return string.Format("{0}: {1}", Name, Value)
}
}
Here is sample usage:
var intProp = new Property<int> ("age", 32);
var strProp = new Property<string> ("name", "Earl");
var enumProp = new Property<ColorEnum> ("eye color", ColorEnum.Magenta);
To make the construction even simpler, you could have a factory method:
public static Property<T> MakeProperty(string name, T value)
{
return new Property<T>(name,value);
}
var intProp = MakeProperty("age", 32);
var strProp = MakeProperty("name", "Earl");
var enumProp = MakeProperty("eye color", ColorEnum.Magenta);
Not necessarily recommended, and a bit OT:
You could make it even funkier with an extension method:
public static Property<T> AsProp<T>(this T value, string name)
{
return new Property<T>(name,value);
}
var intProp = 32.AsProp("age");
var strProp = "Earl".AsProp("name");
var enumProp = ColorEnum.Magenta.AsProp("eye color");
You would have to simply use the object type. What are you trying to accomplish? The problem here isn't the structure of your classes, it's the function that receives the collection of Property objects. It's impossible to even cast something to an unknown type, since you don't know what type of variable it needs to be stored in.
So basically, your Property.Value property needs to be of type object. In your method that uses the Property objects, you need to do something with them, and what you're doing will decide how it should be structured. Are you printing values out? Have a *Value class inheriting from an abstract PropertyValue class and override ToString() to return an appropriate string represention.
I made a few changes to your sample code and got this result...
abstract public class Property
{
private readonly String _name;
public Property(String propertyName)
{
_name = propertyName;
}
public String Name
{
get { return _name; }
}
abstract public override String ToString();
}
public class StringProperty : Property
{
private readonly dynamic _value; // different properties for different types
public StringProperty(String propertyName, dynamic value)
: base(propertyName)
{
this._value = value;
}
public dynamic Value // different signature for different properties
{
get { return _value; }
}
public override String ToString()
{
return base.Name + ": " + _value;
}
}
static void Main(string[] args)
{
StringProperty sp = new StringProperty("A double", 3.444);
StringProperty sp2 = new StringProperty("My int", 4343);
StringProperty sp3 = new StringProperty("My directory", new DirectoryInfo("Some directory"));
StringProperty sp4 = new StringProperty("My null", null);
Console.WriteLine(sp);
Console.WriteLine(sp2);
Console.WriteLine(sp3);
Console.WriteLine(sp4);
}
}
Values are properly printed to the console in the expected way.
It would require a bit of a rethink, but have you considered using the dynamic type (introduced in .net4)
Doesn't really solve your problem, but sidespteps it.
Your properties can bascically just be a
Dictionary<String, dynamic>
, the gotcha is they don't get evaluated until runtime, so you get no compiler support for typing.
so given you want
int SomeValue = MyProperties[SomePropertyName] + 10;
So if
MyProperties[SomePropertyName] = 10; // all is good
if its 76.52 or Fred, the addition will throw an exception at the point it executes.
Code is much simpler and cleaner, no extra casting and the amount of scaffolding required is minimal, BUT, you'll need to unit test code that uses the dictionary extensively and religiously.
Let us say we have a key with values which are polymorphic in their sense. Consider the next sample project:
public class ToBeSerialized
{
[BsonId]
public ObjectId MongoId;
public IDictionary<string, BaseType> Dictionary;
}
public abstract class BaseType
{
}
public class Type1 : BaseType
{
public string Value1;
}
public class Type2:BaseType
{
public string Value1;
public string Value2;
}
internal class Program
{
public static void Main()
{
var objectToSave = new ToBeSerialized
{
MongoId = ObjectId.GenerateNewId(),
Dictionary = new Dictionary<string, BaseType>
{
{"OdEd1", new Type1 {Value1="value1"}},
{
"OdEd2",
new Type1 {Value1="value1"}
}
}
};
string connectionString = "mongodb://localhost/Serialization";
var mgsb = new MongoUrlBuilder(connectionString);
var MongoServer = MongoDB.Driver.MongoServer.Create(mgsb.ToMongoUrl());
var MongoDatabase = MongoServer.GetDatabase(mgsb.DatabaseName);
MongoCollection<ToBeSerialized> mongoCollection = MongoDatabase.GetCollection<ToBeSerialized>("Dictionary");
mongoCollection.Save(objectToSave);
ToBeSerialized received = mongoCollection.FindOne();
}
}
Sometimes when I try to deserialize it, I get deserialization errors like "Unknown discriminator value 'The name of concrete type'". What am I doing wrong? If every value stores a _t why cannot it map it correctly?
Driver should know about all discriminators to deserialize any class without errors. There are two ways to do it:
1.Register it globally during app start:
BsonClassMap.RegisterClassMap<Type1>();
BsonClassMap.RegisterClassMap<Type2>();
2.Or though the BsonKnownTypes attibute:
[BsonKnownTypes(typeof(Type1), typeof(Type2)]
public class BaseType
{
}
If you will use #1 or #2 your deserialization will work correctly.
You have to register which types inherit from BaseClass before attempting to deserialize them. This will happen automatically if you serialize first, which is probably why the error occurs only sometimes.
You can register derived types using an attribute:
[BsonDiscriminator(Required = true)]
[BsonKnownTypes(typeof(DerivedType1), typeof(DerivedType2))]
public class BaseClass { ... }
public class DerivedType1 : BaseClass { ... }