Serializing OpenCV Mat_

The earlier answers are good, but they won’t work for non-continuous matrices which arise when you want to serialize regions of interest (among other things). Also, it is unnecessary to serialize elemSize() because this is derived from the type value. Here’s some code that will work regardless of continuity (with includes/namespace) #pragma once #include <boost/archive/text_oarchive.hpp> … Read more

How to Serialize List?

You could use the XMLSerializer: var aSerializer = new XmlSerializer(typeof(A)); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); aSerializer.Serialize(sw, new A()); // pass an instance of A string xmlResult = sw.GetStringBuilder().ToString(); For this to work properly you will also want xml annotations on your types to make sure it is serialized with the … Read more

JSON Serialize List

If you use the Newtonsoft Json.NET library you can do the following. Define a converter to write the list of key/value pairs the way you want: class MyConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { List<KeyValuePair<string, object>> list = value as List<KeyValuePair<string, object>>; writer.WriteStartArray(); foreach (var item in list) … Read more

Deserializing XML File with multiple element attributes – attributes are not deserializing

To do that you need two levels: [XmlRoot(“TestXML”)] public class TestXml { [XmlElement(“TestElement”)] public TestElement TestElement { get; set; } } public class TestElement { [XmlText] public int Value {get;set;} [XmlAttribute] public string attr1 {get;set;} [XmlAttribute] public string attr2 {get;set;} } Note that the > 26 < may cause problems too (whitespace); you may need … Read more

How do I implement TypeAdapterFactory in Gson?

When you register a regular type adapter (GsonBuilder.registerTypeAdapter), it only generates a type adapter for THAT specific class. For example: public abstract class Animal { abstract void speak(); } public class Dog extends Animal { private final String speech = “woof”; public void speak() { System.out.println(speech); } } // in some gson related method gsonBuilder.registerTypeAdapter(Animal.class, … Read more

Is DataContract attributes required for WCF

If I recall correctly(IIRC), if you don’t use formal data-contract markers, it defaults to acting like a field-serializer. This will work, but is less easy to version, since private changes can break the client/server. IMO you should always formally decorate WCF types with the data-contract/data-member attributes. It will work without them, but for the wrong … Read more