包括序列化和反序列化
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();
}
網站http://james.newtonking.com/projects/json-net.aspx,docs http://james.newtonking.com/projects/json/help/ – GSerjo 2012-08-06 13:52:34
序列化新的A(){X = 1001, Y = new byte [] {0,1,4,8}}給我{「X」:1001,「Y」:「AAEECA ==」},這是你所期待的嗎? – 2012-08-06 13:55:22
是這是輸出,但我需要實際值以十六進制顯示爲「00010408」 – user334223 2012-08-06 14:08:03