I want to serialize a MyClass, which is a class that contains a list MyClass.
In the XML, I want to write only myClass.Name, then when I deserialize it, I then find which MyClass should be in which other MyClass. I have the following code that properly serializes the list of MyClass into a list of string. However, it doesn't deserialize the list of string.
//List of actual object. It's what I use when I work with the object.
[XmlIgnore]
public List<TaskConfiguration> ChildTasks { get; set; }
//Used by the serializer to get the string list, and used
//by the serializer to deserialize the string list to.
[XmlArray("ChildTasks")]
public List<string> ChildTasksSurrogate
{
get
{
List<string> childTaskList = new List<string>();
if (ChildTasks != null)
childTaskList.AddRange(ChildTasks.Select(ct => ct.Name).ToList());
if (_childTasksSurrogate != null)
childTaskList.AddRange(_childTasksSurrogate);
//Clears it not to use it when it serializes.
_childTasksSurrogate = null;
return childTaskList;
}
set
{
_childTasksSurrogate = value;
}
}
[XmlIgnore]
private List<string> _childTasksSurrogate;
As I said, the serialization works. The problem lies with the deserialization. After the deserialization, MyClass._childTasksSurrogate is null.
The problem was related to HOW does the XmlSerializer deserializes the Xml :
I thought that the XmlSerializer would assign the whole property (read: myList = DeserializedList), while it looks like it adds all the elements (read: myList.AddRange(DeserializedList).
Related
When serializing an object, I have a List<T> property where T is an abstract type which I'd like to serialize naturally. However, when deserializing the object, I want/need to manually deserialize this abstract list. I'm doing the latter part with a custom JsonConverter which looks at a property on each item
in the list and then deserializes it to the correct type and puts it in the list.
But I also need to deserialize the rest of the object, without doing it property-by-property. Pseudo-code follows.
MyObject
class MyObject {
public Guid Id;
public string Name;
public List<MyAbstractType> Things;
}
MyObjectConverter
class MyObjectConverter : JsonConverter {
public override object ReadJson(reader, ..., serializer) {
// Line below will fail because the serializer will attempt to deserlalize the list property.
var instance = serializer.Deserialize(reader);
var rawJson = JObject.Load(reader); // Ignore reading from an already-read reader.
// Now loop-over and deserialize each thing in the list/array.
foreach (var item in rawJson.Value<JArray>(nameof(MyObject.Things))) {
var type = item.Value<string>(nameof(MyAbstractType.Type));
MyAbstractType thing;
// Create the proper type.
if (type == ThingTypes.A) {
thing = item.ToObject(typeof(ConcreteTypeA));
}
else if (type == ThingTypes.B) {
thing = item.ToObject(typeof(ConcreteTypeB));
}
// Add the thing.
instance.Things.Add(thing);
}
return instance;
}
}
To recap, I want to manually handle the deserialization of Things, but allow them to naturally serialize.
This question already has answers here:
How do I get json.net to serialize members of a class deriving from List<T>?
(3 answers)
Closed 6 years ago.
I can't find a way to serialize with JSON.NET a list of derived lists, and I'm starting to wonder if it's even possible. Let me explain the situation with some code.
I've created a new class which is derived from a list of a specific object (tried first with a generic one) and added a string property NomGroupe:
public class GroupeActes : List<Acte>
{
public string NomGroupe { get; set; }
public GroupeActes(string nom, List<Acte> liste)
{
NomGroupe = nom;
foreach (var acte in liste)
{
this.Add(acte);
}
}
}
Then, in my code, I've declared a list of this class (List<GroupeActes> listOfGroupeActes) and I fill it with data. For the serialization, I use this code:
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.All;
using (StreamWriter sw = new StreamWriter(#"listOfGroupeActes.json"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, listOfGroupeActes);
}
I've tried with and without the TypeNameHandling.All parameter and with several combination of Json.net properties and even with DataContract/DataMember.
So far, I only managed to get in my json file either the data of each nested List<Acte> without the NomGroupe property, or the other way around. But not both, which is what I'd like to have.
Two questions then:
Is it even possible?
If yes, how can I do it?
Thanks for your help!
You don't want to inherit from List<T>.
Create a list property instead:
public class GroupeActes
{
public List<Acte> Actes { get; set; }
public string NomGroupe { get; set; }
public GroupeActes(string nom, List<Acte> liste)
{
NomGroupe = nom;
Actes.AddRange(acte);
}
}
Lists (and other collection types) get special treatment while serializing. You don't want the collection type's public properties (such as Capacity and Count) in your output, so the property you added through inheritance won't be serialized either.
A collection is serialized like this:
if o is IEnumerable
foreach object s in o
serialize o
So the serializer won't even look at your enumerable's properties.
Try to use Newtonsoft Json.NET
string itemToSend = JsonConvert.SerializeObject(dataModel);
I have a class which I use the XmlSerializer with to serialize data to and from XML files.
I have several DateTime properties. In the post, Prevent timezone conversion on deserialization of DateTime value the answer correctly removes timezone offsets from DateTime properties.
However, I have a property which is a list of DateTime objects that I can't remove the timezones from.
[XmlElement]
public List<DateTime> Times {get; set;}
I have tired something like this, but the value is always null and none of the data is correctly serialized to the list property.
[XmlIgnore]
public List<DateTime> Times {get; set;}
[XmlElement(ElementName = "Times")]
public List<string> TimesString
{
get
{
return Times.ForEach(fe => RemoveTimeZone(fe));
}
set
{
foreach(var v in value)
{
Times.Add(ConvertToDate(v));
}
}
}
The value property is always empty and both list properties are always empty.
My goal is to not create a new class, but to somehow bind directly to my list properties.
Your TimesString property is a proxy collection property, i.e. a property that gets or sets an underlying collection, transforming its members in the process. The simplest way to make such a proxy collection work correctly with XmlSerializer is to make it be an array rather than a list, in your case a string []:
[XmlIgnore]
public List<DateTime> Times { get; set; }
[XmlElement(ElementName = "Times")]
public string [] TimesString
{
get
{
return Times == null ? null : Times.Select(t => RemoveTimeZone(t)).ToArray();
}
set
{
if (value == null)
return;
(Times = Times ?? new List<DateTime>(value.Length)).AddRange(value.Select(s => ConvertToDate(s)));
}
}
string [] works while List<string> does not because XmlSerializer deserializes a property referring to a class implementing IList<T> in the following way:
It calls the getter to get the list. If null, it allocates a list and sets it via the setter. It holds onto the list in some local variable while populating it.
It deserializes each list element, and adds it to the list it is holding.
And that's it. It never calls the containing class's list property setter afterwards.
However, since an array is basically a read-only collection and so cannot be added to once allocated, it cannot be set back until completely populated. This is what XmlSerializer does, which allows proxy array properties to deserialized successfully.
For more, see Cannot deserialize XML into a list using XML Deserializer or XML Deserialization of collection property with code defaults.
I am trying to serialize some data which contains an observable collection of objects and write it into a text tile. My output is [] and I do now know where I have made a mistake.
my object code
public class ObjectList : ObservableCollection<string>, INotify...
{
public ObservableCollection<string> ObjectListInstance = new ObservableCollection<string>();
public string Name;
... get set methods & property changed method
}
my IO code
using (Stream newStream = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("file.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting))
{
DataContractJsonSerializer newDataContractJsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<ObjectList>));
newDataContractJsonSerializer.WriteObject(newStream, ObjectList);
}
my code stub
ObjectList newObjectList = new ObjectList();
newObjectList.Name = "AAA NAME";
newObjectList.ObjectListInstance.Add("ITEM 1");
newObjectList.ObjectListInstance.Add("ITEM 2");
bool status = await IOClass.IO.WriteCategory(newObjectList);
You can't have a class that is both a collection and has additional properties to be serialized. However, you can have a class that contains a list and has additional properties, which is what I think you are trying to do here. To make it work you will need to make some adjustments to your code:
Your ObjectList should not inherit from ObservableCollection<T> (or any other list type)
You must mark your class with [DataContract], and mark the properties/fields you want to be serialized with [DataMember].
When you create your DataContractJsonSerializer instance, pass to the constructor the top-level type you want to serialize. In your case, this should be ObjectList, not ObservableCollection<ObjectList>.
When you call WriteObject on the serializer, the second parameter should be the object instance you are serializing, not a Type.
Here is the corrected class:
[DataContract]
public class ObjectList
{
[DataMember]
public ObservableCollection<string> ObjectListInstance = new ObservableCollection<string>();
[DataMember]
public string Name;
}
Here is the corrected serialization code:
DataContractJsonSerializer newDataContractJsonSerializer =
new DataContractJsonSerializer(typeof(ObjectList));
newDataContractJsonSerializer.WriteObject(newStream, newObjectList);
With these changes you should get the following JSON output:
{"Name":"AAA NAME","ObjectListInstance":["ITEM 1","ITEM 2"]}
I've got a class which has been serialized into JSON, and which I'm trying to deserialize into an object.
e.g.
public class ContentItemViewModel
{
public string CssClass { get; set; }
public MyCustomClass PropertyB { get; set; }
}
the simple property (CssClass) will deserialize with:
var contentItemViewModels = ser.Deserialize<ContentItemViewModel>(contentItems);
But PropertyB gets an error...
We added a JavaScriptConverter:
ser.RegisterConverters(new List<JavaScriptConverter>{ publishedStatusResolver});
But when we added 'MyCustomClass' as a 'SupportedType', the Deserialize method was never called. However when we have ContentItemViewModel as the SupportedType, then Deserialize is called.
We've got a current solution which looks something like this:
class ContentItemViewModelConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
var cssClass = GetString(dictionary, "cssClass"); //I'm ommitting the GetString method in this example...
var propertyB= GetString(dictionary, "propertyB");
return new ContentItemViewModel{ CssClass = cssClass ,
PropertyB = new MyCustomClass(propertyB)}
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new Exception("Only does the Deserialize");
}
public override IEnumerable<Type> SupportedTypes
{
get
{
return new List<Type>
{
typeof(ContentItemViewModel)
};
}
}
}
But we'd prefer a simpler solution of only deserializing MyCustomClass, as there are a number of other fields which are on the ViewModel, and it seems a waste to have to edit this converter every time we change/add a property....
Is there a way to Deserialize JUST PropertyB of type MyCustomClass?
Thanks for your help!
Have you considered using DatacontractJsonSerializer
[DataContract]
public class MyCustomClass
{
[DataMember]
public string foobar { get; set; }
}
[DataContract]
public class ContentItemViewModel
{
[DataMember]
public string CssClass { get; set; }
[DataMember]
public MyCustomClass PropertyB { get; set; }
}
class Program
{
static void Main(string[] args)
{
ContentItemViewModel model = new ContentItemViewModel();
model.CssClass = "StackOver";
model.PropertyB = new MyCustomClass();
model.PropertyB.foobar = "Flow";
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContentItemViewModel));
ser.WriteObject(ms, model);
byte[] json = ms.ToArray();
ms.Close();
string s= Encoding.UTF8.GetString(json, 0, json.Length);
Console.ReadLine();
}
}
Add all possible classes to DatacontractJsonSerializer.KnownTypes if MyCustomClass has derivations.
For whatever it may be worth after all this time, but I stumbled over the same problem and the solution is that the Deserializer hasn't got a clue about the classes you are deserializing unless you give him the necessary information.
On the top level, it knows the type from the type parameter of Deserialize<>(). That's why your converter for ContentItemViewModel works. For nested objects, it needs __type properties and a JavaScriptTypeResolver.
var ser = new JavaScriptSerializer(new SimpleTypeResolver());
ser.RegisterConverters(myconverters);
MyClass myObject = new MyClass();
string json = ser.Serialize(myObject);
// set a breakpoint here to see what has happened
ser.Deserialize<MyClass>(json);
A TypeResolver adds a __type property to each serialized object. You can write a custom type resolver that uses short names. In this sample, I use the SimpleTypeResolver from .net that "simply" stores the fully qualified type name as __type. When deserializing, the JavaScriptDeserializer finds __type and asks the TypeResolver for the correct type. Then it knows a type and can call a registered JavaScriptConverter.Deserialize method.
Without a TypeResolver, objects are deserialized to a Dictionary because JavaScriptSerializer doesn't have any type information.
If you can't provide a __type property in your json string, I think you'll need to deserialize to Dictionary first and then add a "guessing-step" that interprets the fields to find the right type. Then, you can use the ConvertToType method of JavaScriptSerializer to copy the dictionary into the object's fields and properties.
If you need to use the JavaScriptSerializer that is provides by ASP.NET and can't create your own, consider this section from the .ctor help of JavaScriptSerializer:
The instance of JavaScriptSerializer that is used by the asynchronous communication layer for invoking Web services from client script uses a special type resolver. This type resolver restricts the types that can be deserialized to those defined in the Web service’s method signature, or the ones that have the GenerateScriptTypeAttribute applied. You cannot modify this built-in type resolver programmatically.
Perhaps the GenerateScriptType Attribute can help you. But I don't know what kind of __type Properties are be needed here.