允许使用.NET框架的可能解决方案
JavaScriptSerializer
是注册一个转换器,Base-64将字节数组编码为子字段,如下所示:
class ByteArrayBase64Converter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
return Convert.FromBase64String((string)dictionary["b64"]);
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
return new Dictionary<string, object> { { "b64", Convert.ToBase64String((byte[])obj) } };
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(byte[])}; }
}
}
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new ByteArrayBase64Converter() });