I have some byte data that I want to attach some metadata to and serialize to some standard interoperable format. I figured I might be able to do this with json, in which case I'd like to have something like
{
'metadata' : { /* ... */ },
'data' : ???
}
What is a good way to do this?
I considered doing the data in base64 encoding, but then I'd come to something of the following:
class SerializationFormat {
MyMetaData metadata {get; set;}
string data {get; set;}
}
byte[] serialize(byte[] mydata, MyMetaData metadata){
var obj = new SerializationFormat(){
data = system.Convert.ToBase64String(mydata),
metadata = metadata
};
//using json.net
string jsonstring = JsonConvert.SerializeObject(obj);
return System.Text.Encoding.UTF8.GetBytes(jsonstring);
}
I consider this rather silly; I'm taking byte data, encoding it in base64 which is guaranteed to be within 7 bit ASCII, encode it into a C# string (UTF-16), and decode it again into its original representation.
Is there a better way to do this?