javascript 如何将 byte[] 序列化为简单的 JSON 数组而不是 JSON.net 中的 base64?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15226921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to serialize byte[] as simple JSON Array and not as base64 in JSON.net?
提问by Marcus
I use JSON.net to serialize some objects between C# and JavaScript. The JSON data is transfered via WebSocket between the .NET and browser application.
我使用 JSON.net 在 C# 和 JavaScript 之间序列化一些对象。JSON 数据通过 WebSocket 在 .NET 和浏览器应用程序之间传输。
In the data structure there are some byte[]
fields, I want these fields as an Array
in JavaScript also.
在数据结构中有一些byte[]
字段,我希望这些字段也作为Array
JavaScript 中的一个。
How can I serialize a C# byte[]
to a simple JSON Array like [ 0 , 1 , 254, 255 ]
instead of a base64 string?
如何将 C# 序列化为byte[]
简单的 JSON 数组,[ 0 , 1 , 254, 255 ]
而不是 base64 字符串?
采纳答案by Joe Enos
Simplest way I can think of is to convert the byte array into an integer array, like:
我能想到的最简单的方法是将字节数组转换为整数数组,例如:
var intArray = byteArray.Select(b => (int)b).ToArray();
This wouldn't require any special handling of the JSON library, or any custom serialization or anything like that.
这不需要对 JSON 库进行任何特殊处理,也不需要任何自定义序列化或类似的东西。
EDIT: This would mean having to customize your data object to handle the different type. Maybe:
编辑:这意味着必须自定义您的数据对象来处理不同的类型。或许:
public class CustomFoo : Foo
{
// SomeBytesHere is a byte[] in the base class
public new int[] SomeBytesHere { get;set; }
}
So maybe it's not the simplest - depending on how much stuff you have to serialize
所以也许这不是最简单的 - 取决于你必须序列化多少东西
回答by Paul Turner
JSON.NET is selecting the BinaryConverter
to read and write an array of bytes. You can see in the sourcethat it uses the WriteValue
operation on the JsonWriter
class with the array of bytes which causes them to be written to as Base-64.
JSON.NET 正在选择BinaryConverter
读取和写入字节数组。您可以在源代码中看到它使用WriteValue
对JsonWriter
类的操作以及字节数组,这导致它们被写入为 Base-64。
To modify this, you can write your own converter which reads and writes an array in the format you expect:
要修改它,您可以编写自己的转换器,以您期望的格式读取和写入数组:
public class ByteArrayConverter : JsonConverter
{
public override void WriteJson(
JsonWriter writer,
object value,
JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
byte[] data = (byte[])value;
// Compose an array.
writer.WriteStartArray();
for (var i = 0; i < data.Length; i++)
{
writer.WriteValue(data[i]);
}
writer.WriteEndArray();
}
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
var byteList = new List<byte>();
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.Integer:
byteList.Add(Convert.ToByte(reader.Value));
break;
case JsonToken.EndArray:
return byteList.ToArray();
case JsonToken.Comment:
// skip
break;
default:
throw new Exception(
string.Format(
"Unexpected token when reading bytes: {0}",
reader.TokenType));
}
}
throw new Exception("Unexpected end when reading bytes.");
}
else
{
throw new Exception(
string.Format(
"Unexpected token parsing binary. "
+ "Expected StartArray, got {0}.",
reader.TokenType));
}
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(byte[]);
}
}
You would use this by applying the JsonConverterAttribute
to the member:
您可以通过将 应用于JsonConverterAttribute
成员来使用它:
[JsonConverter(typeof(ByteArrayConverter))]
public byte[] Data { get; set; }