BinaryConverter
Converts a binary value to and from a base 64 string value.
            
                using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Converters
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    public class BinaryConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonSerializer serializer)
        {
            if (value == null)
                writer.WriteNull();
            else {
                byte[] byteArray = GetByteArray(value);
                writer.WriteValue(byteArray);
            }
        }
        private byte[] GetByteArray(object value)
        {
            if (value is SqlBinary)
                return ((SqlBinary)value).Value;
            throw new JsonSerializationException("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public override object ReadJson(JsonReader reader, Type objectType, [System.Runtime.CompilerServices.Nullable(2)] object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null) {
                if (!ReflectionUtils.IsNullable(objectType))
                    throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
                return null;
            }
            byte[] value;
            if (reader.TokenType == JsonToken.StartArray)
                value = ReadByteArray(reader);
            else {
                if (reader.TokenType != JsonToken.String)
                    throw JsonSerializationException.Create(reader, "Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                value = Convert.FromBase64String(reader.Value.ToString());
            }
            if ((object)(ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType) == typeof(SqlBinary))
                return new SqlBinary(value);
            throw JsonSerializationException.Create(reader, "Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
        }
        private byte[] ReadByteArray(JsonReader reader)
        {
            List<byte> list = new List<byte>();
            while (reader.Read()) {
                switch (reader.TokenType) {
                case JsonToken.Integer:
                    list.Add(Convert.ToByte(reader.Value, CultureInfo.InvariantCulture));
                    break;
                case JsonToken.EndArray:
                    return list.ToArray();
                default:
                    throw JsonSerializationException.Create(reader, "Unexpected token when reading bytes: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                case JsonToken.Comment:
                    break;
                }
            }
            throw JsonSerializationException.Create(reader, "Unexpected end when reading bytes.");
        }
        public override bool CanConvert(Type objectType)
        {
            if ((object)objectType == typeof(SqlBinary) || (object)objectType == typeof(SqlBinary?))
                return true;
            return false;
        }
    }
}