This question already has answers here:
Circular reference detected exception while serializing object to JSON
(9 answers)
Closed 9 years ago.
I'm having trouble making an client-server app.
I'm working with Entity Framework(EF) and I need to serialize an object to send it via sockets that contains List attributes.
I'm using XMLSerialization for the Serialization part.
My problem is: When I try to Serialize a new Survey object and the OPTIONs List is Empty I can Serialize the object SURVEY. But, when I add an OPTION object to the SURVEY.OPTIONS list I cannot Serialize the object.
One of the classes that EF auto-generates from the Entity-Relationship Diagram is:
public partial class SURVEY
{
public SURVEY()
{
this.OPCIONs = new List<OPTION>();
}
public int id_survey { get; set; }
public System.DateTime initial_date { get; set; }
public System.DateTime end_date { get; set; }
public virtual List<OPTION> OPTIONs { get; set; }
}
I'm using this Code for get a Survey from the Database:
DateTime actualDate = new DateTime().Today;
private static ComedorCaniaDBContext context = new ComedorCaniaDBContext();
Survey survey = context.SURVEYs.Create()
survey = (SURVEY)context.SURVEYs
.Include("Options")
.Where(e => e.initial_date < actualDate && e.end_date > actualDate)
.FirstOrDefault();
I'm using this Code for Serialization:
public static Byte[] ObjectToByteArray<T>(T obj)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xmlS = new XmlSerializer(typeof(T));
xmlS.Serialize(ms, obj);
return ms.ToArray();
}
}
catch
{
return null;
}
}
I'll appreciate your help.
Thanks.
Entity Framework is near impossible to make work with the built-in XML serializer and BinaryFormatter serializer, if you don't want to lose features like lazy loading. They just aren't made to deal with that.
You're going to have to use a different serializer that can handle Entity Framework objects, such as JSON.Net, or else write your own serializer.
See the following articles for the long line of others who have had the same question:
Circular reference detected exception while serializing object to JSON
How did I solve the Json serializing circular reference error?
Basically, you either get to keep lazy loading and use some other serializer or lose it and continue to use the built-in serializer.
Related
I have a host of classes including the normal designs of object hierarchies and interfaces, base classes, etc. from a project where I cannot modify any code. I have another payload class in my project which uses composition to encapsulate information from the other classes and contain properties in the payload class whose types are the classes from the other project.
Now I have a need to be able to create an instance of the payload class containing instances of those other classes and serialize it to Base64 string for transmission.
Issue is since I cannot touch the code for the other classes, I cannot add serialization attributes (for .NET binary formatter/protobuf-net). I was also trying to look at using protobuf-net without attributes, but since the object hierarchy is too deep, it seemed to be too complicated to create.
Can somebody tell me a better choice to go ahead with the serialization/deserialization part without modifying the existing code.
Sample code to illustrate the requirement is shown below:
void Main()
{
var b = new B();
b.SetStatus("This is B");
var c = new C { Name = "C", Value = 100};
var payload = new Payload(b, c);
var serializedData = SerializeToString<Payload>(payload);
serializedData.Dump();
}
private static TData DeserializeFromString<TData>(string settings)
{
byte[] b = Convert.FromBase64String(settings);
using (var stream = new MemoryStream(b))
{
var formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
return (TData)formatter.Deserialize(stream);
}
}
private static string SerializeToString<TData>(TData settings)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, settings);
stream.Flush();
stream.Position = 0;
return Convert.ToBase64String(stream.ToArray());
}
}
public class A
{
public string Id { get; set; }
public string StatusMsg {get;protected set;}
public virtual void SetStatus(string msg)
{
StatusMsg = msg;
}
}
public class B : A
{
public B()
{
Id = new Guid().ToString();
}
public override void SetStatus(string msg)
{
base.SetStatus(msg);
}
}
public class C
{
public string Name
{
get;
set;
}
public Int32 Value
{
get;
set;
}
}
public class Payload
{
public B PropertyB { get; set; }
public C PropertyC { get; set; }
public Payload(B b, C c)
{
this.PropertyB = b;
this.PropertyC = c;
}
}
Without adding configuration attributes, you have a few options:
use a serialize that won't care: XmlSerializer or Json.NET might work if you're lucky
accept the runtime config work of something like protobuf-net or one of the others; it probably isn't as much work as you expect (heck, drop me an email with real code and I might be able to do it)
wrote the serialization entirely manually
write a DTO layer - a basic model with attributes etc that works well with your chosen serializer - and either write code to map between the two models, or use a tool like auto-mapper
Personally, when serialization configuration gets tricky, my default option is "write a DTO model".
I don't understand why protobuf would not work but you can do the serialization manually if you don't mind.
In that case, create a BinaryWriter and then write everything you need to be able to deserialize it back again using a BinaryReader. Doing it this way you will get a very compact representation tailored for you specific needs which is also very fast. But it's a bit more work also. General purpose serialization can be verbose and slow but is almost always preferred in any case.
But I still don't get why using any existing attribute-less serialization method wouldn't work. You should probably start by really understand why it doesn't work with for instance protobuf or JSON.NET. You mentioned "too deep hierarchy". How deep is that?
I am using .net Core 1.1 and writing a console application. I need to Serialize an Object with some basic properties of strings and int as well as some IEnumerable Objects and serialize it as XML so I can save it to a "data.xml" file.
I receive the Exception:
System.Runtime.Serialization.SerializationException: 'Type 'System.Collections.Generic.List`1[[CorpsDataExtractor.ViewModels.LegacyView, CorpsDataExtractor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfLegacyView:http://schemas.datacontract.org/2004/07/CorpsDataExtractor.ViewModels' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'
I currently am able to serialize my Object as JSON, but am having trouble utilizing the DataContractSerializer. I Added the Known Type List that the exception states I am missing but it still fails.
// This List isretrieved using Entity Framework Core
List<LegacyView> legacyData = await _corpsRepo.ListLegacyData();
// The Below Line will write to a JSON File. No Error and works
File.WriteAllText(#"E:\\CorporationsDataLegacy.json", JsonConvert.SerializeObject(legacyData));
// This Creates a Known Type List for the DataContractSerializer
var knownTypeList = new List<Type>
{
typeof(LegacyView),
typeof(People),
typeof(DocumentType)
};
FileStream writer = new FileStream(#"E:\\data.xml", FileMode.Create);
var ser = new DataContractSerializer(typeof(LegacyView), knownTypeList );
// When Debugging I Receive The stated exception at this line
ser.WriteObject(writer, legacyData);
Below Are my Class ViewModels:
Legacy View
[DataContract]
public class LegacyView
{
[DataMember]
public string Ubi { get; set; }
[DataMember]
public IEnumerable<Person> People{ get; set; }
[DataMember]
public IEnumerable<DocumentType> DocumentTypes { get; set; }
}
Person
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string MiddleName { get; set; }
}
Documents
[DataContract]
public class DocumentType
{
[DataMember]
public string Document { get; set; }
[DataMember]
public DateTime? CompletedDate { get; set; }
}
Any Direction on what I am missing?
I might be a bit off as I've done more of XmlSerialize rather than DataContracts one, but I believe both assume eventual deserialization of the content. And with IEnumerable<T> as the type, deserializer wouldn't know what class to actually instantiate to back the interface during deserialization. Change fields to concrete classes instead of enumeration, and that should work.
By using a Both Gusman and LB2 comments I was able to solve the issue. As LB2 states "with IEnumerable as the type, deserializer wouldn't know what class to actually instantiate to back the interface during deserialization."
This is why the error was occurring, however the problem did not exist at the Class LegacyView [DataMember] level, it existed at the known Types that were being passed into the DataContractSerializer.
Gusman was correct that I needed to tell the DataContractSerializer that these were going to be an enumerable type.
// I needed to tell the DataContractSerializer what the types were
var knownTypeList = new List<Type>
{
typeof(List<LegacyView>),
typeof(List<People>),
typeof(List<DocumentType>)
};
using (var writer = new FileStream(#"E:\\myTestExample.xml", FileMode.Create))
{
var ser = new DataContractSerializer(typeof(LegacyView), knownTypeList);
ser.WriteObject(writer, legacyData);
}
I hope to find a solution from you. What I need is to serialize ValidatorList class object into an xml document. How to do this?
I tried like this:
XmlSerializer _serializer = new XmlSerializer(list);
But I don't know how to make output of xml for list that has several classes.
C#
_list= new ListVal();
Type _type = typeof(ras);
_list.Add(new RequiredField
{
Property = _type.GetProperty("CustRef")
}.Add(new AsciiVal()));
_list.Add(new RequiredField
{
Property = _type.GetProperty("ctr")
}.Add(new StringLengthVal
{
min= 3,
max= 3
}));
[Serializable]
public class Field
{
public Field Next
{
get;
set;
}
public Field TypeName
{
get;
set;
}
public Field PropertyName
{
get;
set;
}
}
public class RequiredField : Field
{
//TODO
}
public class AsciiVal: Field
{
//TODO
}
public class StringLengthVal: Field
{
//TODO
}
public class ListVal: List<Field>
{
//TODO
}
I have something close, but not exactly the Xml you want. In actual fact I think you'll see that the Xml produced below makes a bit more sense than what you have.
To get you started, you control the serialization and deserialization using attributes in the System.Xml.Serialization namespace. A few useful ones to read up on are
XmlRootAttribute
XmlElementAttribute
XmlAttributeAttribute
XmlIncludeAttribute
So I mocked up some code which closely matches your own. Notice the addition of some attributes to instruct the serializer how I want the Xml to be laid out.
[XmlInclude(typeof(AsciiValidator))]
[XmlInclude(typeof(RequiredValidator))]
[XmlInclude(typeof(StringLengthValidator))]
public class FieldValidator
{
[XmlElement("Next")]
public FieldValidator Next
{
get;
set;
}
[XmlElement("PropertyName")]
public string PropertyName
{
get;
set;
}
}
public class AsciiValidator: FieldValidator
{
}
public class RequiredValidator: FieldValidator
{
}
public class StringLengthValidator: FieldValidator
{
[XmlElement]
public int MinLength{get;set;}
[XmlElement]
public int MaxLength{get;set;}
}
[XmlRoot("ValidatorList")]
public class ValidatorList : List<FieldValidator>
{
}
Point of interest; Every class inheriting FieldValidator must be added to the list of known types using XmlIncludeAttribute so the serializer knows what to do with them)
Then I created an example object map:
var test = new ValidatorList();
test.Add(
new RequiredValidator()
{
PropertyName="CustRef",
Next = new AsciiValidator()
});
test.Add(
new RequiredValidator()
{
PropertyName="CurrencyIndicator",
Next = new StringLengthValidator(){
MinLength=3,
MaxLength = 10
}
});
Finally I told the serializer to serialize it (and output the result to the console)
var ser = new XmlSerializer(typeof(ValidatorList));
ser.Serialize(Console.Out,test);
This was the result:
<?xml version="1.0" encoding="utf-8"?>
<ValidatorList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FieldValidator xsi:type="RequiredValidator">
<Next xsi:type="AsciiValidator" />
<PropertyName>CustRef</PropertyName>
</FieldValidator>
<FieldValidator xsi:type="RequiredValidator">
<Next xsi:type="StringLengthValidator">
<MinLength>3</MinLength>
<MaxLength>10</MaxLength>
</Next>
<PropertyName>CurrencyIndicator</PropertyName>
</FieldValidator>
</ValidatorList>
Not a million miles away from what you wanted. There is the need to output certain things in a certain way (eg xsi:type tells the serializer how to deserialize back to the object map). I hope this gives you a good start.
Here is a live, working example: http://rextester.com/OXPOB95358
Deserialization can be done by calling the Deserialize method on the XmlSerializer.
For example, if your xml is in a string:
var ser = new XmlSerializer(typeof(ValidatorList));
var test = "<..../>" // Your Xml
var xmlReader = XmlReader.Create(new StringReader(test));
var validatorList = (ValidatorList)ser.Deserialize(xmlReader);
There are many overrides of Deserialize which take differing inputs depending if the data is in a Stream an existing reader, or saved to a file.
You have to decorate the class that contains the _validators field with the KonwnType attribute
[Serializable]
[KwownType(typeof(RequiredFieldValidator)]
[KwownType(typeof(AsciValidator)]
public class MySerialisableClass
I have several SO answers that detail how to serialize objects using XML. I'll provide links below.
However, since you're looking for a rather simple serialization of your object, you may want to read up on the DataContractSerializer. It's much less complicated than the old .NET 1.x XML Serialization.
Here's the list of SO answers:
Omitting all xsi and xsd namespaces when serializing an object in .NET?
XmlSerializer: remove unnecessary xsi and xsd namespaces
Suppress xsi:nil but still show Empty Element when Serializing in .Net
Using XmlAttributeOverrides on Nested Properties
Even though many of these have to do with XML serialization and namespaces, they contain complete examples of serializing an object to XML using .NET 1.x XML Serialization.
I have a class which inherits from a collection, specifically List<> and I've listed the class below. The problem I'm encountering when I serialize the object to XML using DataContractSerializer is the additional fields I've added within the object are not getting serialized by the serializer.
Here is the class:
[CollectionDataContract(Name = "ServiceEvents", Namespace = "")]
public class ServiceEventList : List<ServiceEvent>
{
[DataMember]
public long StaleDate { get; set; }
[DataMember]
public long ExpirationDate { get; set; }
}
When I serialize the object and write to disk, here is the output (notice both StaleDate and ExpirationDate are missing).
<ServiceEvents xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ServiceEvent><Date>2012-06-26T22:23:24.120817Z</Date><Description>A Service Event</Description><Id>634763462041210040</Id><Notes></Notes><Odometer>42</Odometer></ServiceEvent></ServiceEvents>
Here is the code that serializes the object:
using (FileStream fs = new FileStream(path, FileMode.Create))
{
//TODO: StaleDate is not serializing to disk
//TODO: ExpirationDate is not serializing to disk
DataContractSerializer ser = new DataContractSerializer(typeof(ServiceEventList));
ser.WriteObject(fs, list);
}
My next thought is to remove the inheritance structure and just embed a List object into the class. I'd prefer to just extend List but won't waste more time on it if the community confirms my approach won't work. Thanks in advance for the advice.
According to this post:
http://social.msdn.microsoft.com/Forums/eu/wcf/thread/57eb195a-43a9-47fe-8b1a-a9d23feb2df2
the problem is indeed that you inherit from a collection type - in this case the DataContractSerializer serializes only its items, but not any extra properties.
I am using Newtonsoft.Json with version 4.0.8 and trying to use it with Web API.
So I wanted to deserialize JSON with
JsonConvert.DeserializeObject<AClass>(jsonString);
This works until I added a Dictionary as property to this class and wanted to deserialize it.
The json string is in the form of
{
"Date":null,
"AString":"message",
"Attributes":[
{"Key":"key1","Value":"value1"},
{"Key":"key2","Value":"value2"}
],
"Id":0,
"Description":"...
}
When deserializing exception of type JsonSerializationException occures with message: "Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'."
What am I doing wrong here?
UPDATE1:
When serializing with JSON.NET i get the following for the dictionary:
Attributes":{"key1":"value1","key2":"value2"}
Seems that WebApi deserializes the object in an other way than Json.Net would.
Server side I use following line for implicit deserializing:
return new HttpResponseMessage<AClass>(object);
UPDATE2:
As a workaround I came now to following line server side.
return new HttpResponseMessage<string>(JsonConvert.SerializeObject(license).Base64Encode());
I convert it with Json.Net server side and transfer it as base64 encoded string. So Json.Net can deserialize its own format.
But its still not that what I want, so are thery any further suggestions?
It should work if you declare Attributes as List<KeyValuePair<string, string>>
From this post, calling
JsonConvert.SerializeObject(yourObject, new KeyValuePairConverter());
gets your JSON in the format that the Web API is creating for you.
Ergo, one might assume that calling
JsonConvert.DeserializeObject<AClass>(jsonString, new KeyValuePairConverter());
will do the reverse and correctly handle the Web API's style.
I have no idea whether this overload even exists, though; give it a try and see what happens...
Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(strJsonResult);
If it's .NET 4, you can use DataContract attributes and the DataContractJsonSerializer Class to enforce the message format:
[DataContract]
public class Message
{
[DataMember]
public DateTime? Date { get; set; }
[DataMember]
public string AString { get; set; }
[DataMember]
public Dictionary<string, string> Attributes { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string Description { get; set; }
}
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Message));
Message message = null;
using (MemoryStream jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
// Deserialize
message = (Message)jsonSerializer.ReadObject(jsonStream);
// Go to the beginning and discard the current stream contents.
jsonStream.Seek(0, SeekOrigin.Begin);
jsonStream.SetLength(0);
// Serialize
jsonSerializer.WriteObject(jsonStream, message);
jsonString = Encoding.UTF8.GetString(jsonStream.ToArray());
}
Serializing this back out produces the following JSON:
{"AString":"message","Attributes":[{"Key":"key1","Value":"value1"},{"Key":"key2","Value":"value2"}],"Date":null,"Description":"...","Id":0}