How to serialize byte[] as simple JSON Array and not as base64 in JSON.net?

后端 未结 3 675
轮回少年
轮回少年 2020-11-30 05:35

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.

In the data

3条回答
  •  伪装坚强ぢ
    2020-11-30 06:27

    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.

    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

提交回复
热议问题