6

I have binary serialized objects in database. They are serialized with protobuf. Now I need to generate some viewer to see the content of Database. So, i read stream from database and deserialized it back to the objects. It works and the result is list of objects:

var dbData = readData(someType);//it is IList collection

Now, I would like to save this list of objects to file to see the content of database. I thought it would be the best to save it to xml. So, i have tried:

var serializer = new XmlSerializer(dbData.GetType());

But i get an error: Cannot deserialize type 'My.Entities.IdBase' because it contains property 'Key' which has no public setter.

What now? I can't change the class definitions to have setters. Should i save objects to json or plain text instead? Or should i extract all properties and values and save it to some xml? Any code example?

Simon
  • 1,955
  • 5
  • 35
  • 49
  • 2
    You can use Json.Net http://www.newtonsoft.com/json which is able to serialize private members aswell. Howto: http://stackoverflow.com/questions/24106986/json-net-force-serialization-of-all-private-fields-and-all-fields-in-sub-classe – TripleEEE Dec 06 '16 at 12:57
  • 1
    The XmlSerializer type complains about these things even if you only intend to serialize objects with it, and never deserialize. – Lasse V. Karlsen Dec 06 '16 at 12:58

1 Answers1

0

JSON.NET would be the answer here. You can find it in nuget. Use it like this:

JsonConvert.DeserializeObject<T>(input);
  • I use "string json = JsonConvert.SerializeObject(objectToWrite, seetings);" I get json as string. How can I convert it to JObject? I have tried:JObject obj = JObject.Parse(json); but it doesn't work. – Simon Dec 06 '16 at 14:22