使用 Newton.Json 进行 Json 序列化。应用什么 JsonSerializerSettings,当我必须 Json 将具有属性的对象序列化为字节数组,然后以十六进制格式显示字节数组时..
例如
class A
{
public int X {get;set;}
public byte[] Y {get;set;}
}
当我将 A 序列化为 json 时,我没有像我设置的那样获得 Y 的值... byte[] 的输出应该是十六进制
使用 Newton.Json 进行 Json 序列化。应用什么 JsonSerializerSettings,当我必须 Json 将具有属性的对象序列化为字节数组,然后以十六进制格式显示字节数组时..
例如
class A
{
public int X {get;set;}
public byte[] Y {get;set;}
}
当我将 A 序列化为 json 时,我没有像我设置的那样获得 Y 的值... byte[] 的输出应该是十六进制
var json = JsonConvert.SerializeObject(new MyTestClass());
public class MyTestClass
{
public string s = "iiiii";
[JsonConverter(typeof(ByteArrayConvertor))]
public byte[] buf = new byte[] {1,2,3,4,5};
}
public class ByteArrayConvertor : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType==typeof(byte[]);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
byte[] arr = (byte[])value;
writer.WriteRaw(BitConverter.ToString(arr).Replace("-", ""));
}
}
只需覆盖 JsonSerializerSettings 中的转换器,优于属性中的属性
private class SerializationTest
{
public byte[] Bytes => new byte[] { 11, 22, 33, 44, 0xAA, 0xBB, 0xCC };
}
private class ByteArrayHexConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(byte[]);
public override bool CanRead => false;
public override bool CanWrite => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotImplementedException();
private readonly string _separator;
public ByteArrayHexConverter(string separator = ",") => _separator = separator;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var hexString = string.Join(_separator, ((byte[])value).Select(p => p.ToString("X2")));
writer.WriteValue(hexString);
}
}
private static void Main(string[] args)
{
var setting = new JsonSerializerSettings { Converters = { new ByteArrayHexConverter() } };
var json = JsonConvert.SerializeObject(new SerializationTest(), setting);
Console.WriteLine(json); // {"Bytes":"0B,16,21,2C,AA,BB,CC"}
}
包括序列化和反序列化
public class A
{
public int X { get; set; }
[JsonProperty(ItemConverterType = typeof(BytesToHexConverter))]
public byte[] Y { get; set; }
}
public class BytesToHexConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(byte[]);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
var hex = serializer.Deserialize<string>(reader);
if (!string.IsNullOrEmpty(hex))
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
return Enumerable.Empty<byte>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var bytes = value as byte[];
var @string = BitConverter.ToString(bytes).Replace("-", string.Empty);
serializer.Serialize(writer, @string);
}
}
public static void Main(string[] args)
{
var a = new A() { X = 1001, Y = new byte[] { 0, 1, 4, 8 } };
var s = new JsonSerializer();
var sb = new StringBuilder();
var sw = new StringWriter(sb);
s.Converters.Add(new BytesToHexConverter());
s.Serialize(sw, a);
var json = sb.ToString();
var sr = new StringReader(json);
var readback = s.Deserialize(sr, typeof(A));
Console.WriteLine(sw);
Console.WriteLine("End...");
Console.ReadLine();
}