2012-08-06 51 views
1

使用Newton.Json進行Json序列化。 應用什麼JsonSerializerSettings,當我有到JSON與屬性將對象序列作爲字節數組,然後顯示以十六進制格式的字節數組..Newton Soft Json JsonSerializerSettings用於Property作爲字節數組的對象

對於例如

class A 
{ 
    public int X {get;set;} 
    public byte[] Y {get;set;} 
} 

當我序列A到JSON我因爲我已經設置並不會得到Y值...對於字節輸出[]應該是十六進制

+0

網站http://james.newtonking.com/projects/json-net.aspx,docs http://james.newtonking.com/projects/json/help/ – GSerjo 2012-08-06 13:52:34

+0

序列化新的A(){X = 1001, Y = new byte [] {0,1,4,8}}給我{「X」:1001,「Y」:「AAEECA ==」},這是你所期待的嗎? – 2012-08-06 13:55:22

+0

是這是輸出,但我需要實際值以十六進制顯示爲「00010408」 – user334223 2012-08-06 14:08:03

回答

2
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("-", "")); 
    } 
} 
+0

我不得不將「WriteRaw」更改爲「WriteValue」以使其正常工作。 – 2017-12-05 12:22:25

0

包括序列化和反序列化

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(); 
    }