c# - Easiest way to use JSON.Net for both BSON and JSON? -
i have pieces of data byte arrays byte[], , need render them base64 in json, raw byte arrays in bson.
how can in json.net?
so, far have so:
class data { public byte[] bytes{get;set;} } converting bson fine, when converting json, of course not base64 encoded , treated string
hmm, using following code json.net 6.0.1, appears work want no special treatment: byte arrays converted base-64 strings , vice versa. serializing objects in different way, or using old version? if not, can provide code demonstrates problem?
string s = "foo bar baz quux"; data data = new data { bytes = encoding.utf8.getbytes(s) }; string json = jsonconvert.serializeobject(data); console.writeline(json); data = jsonconvert.deserializeobject<data>(json); console.writeline(encoding.utf8.getstring(data.bytes)); output:
{"bytes":"rm9viejhcibcyxoguxv1ea=="} foo bar baz quux
Comments
Post a Comment