JSON继承JsonConverter序列化与反序列化重写类属性

骑猪看日落 2021-06-26 16:06 464阅读 0赞
  1. 首先自定义一个JSON类,继承JsonConverter,把类的属性重写到JSON中.

    using System;

    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using System.Text;
    4. using GongHuiNewtonsoft.Json;
    5. using GongHuiNewtonsoft.Json.Linq;
    6. namespace JSONDemo
    7. {
    8. public class CustomConverter : JsonConverter
    9. {
    10. private readonly Type[] _types;
    11. public CustomConverter(params Type[] types)
    12. {
    13. this._types = types;
    14. }
    15. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    16. {
    17. JToken t = JToken.FromObject(value);
    18. if (t.Type != JTokenType.Object)
    19. {
    20. t.WriteTo(writer);
    21. }
    22. else
    23. {
    24. JObject jo = (JObject)t;
    25. IList<string> propertyNameList = jo.Properties().Select(p => p.Name).ToList();
    26. jo.AddFirst(new JProperty("Keys", new JArray(propertyNameList)));
    27. jo.WriteTo(writer);
    28. }
    29. }
    30. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    31. {
    32. throw new NotImplementedException("Unnecessary because CanRead is false.The type will skip the converter.");
    33. }
    34. public override bool CanRead
    35. {
    36. get
    37. {
    38. return false;
    39. }
    40. }
    41. public override bool CanConvert(Type objectType)
    42. {
    43. return _types.Any(t => t == objectType);
    44. }
    45. }
    46. }
  2. 定义一个City对象

    using System;

    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using System.Text;
    4. namespace JSONDemo
    5. {
    6. public class City
    7. {
    8. public string Country { get; set; }
    9. public IList<string> Name { get; set; }
    10. public City()
    11. {
    12. this.Name = new List<string>
    13. {
    14. "Auckland",
    15. "Wellington",
    16. "Hamilton"
    17. };
    18. }
    19. }
    20. }

3.序列化与反序列化

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using GongHuiNewtonsoft.Json;
  7. using GongHuiNewtonsoft.Json.Serialization;
  8. using GongHuiNewtonsoft.Json.Converters;
  9. namespace JSONDemo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. City city = new City
  16. {
  17. Country = "New Zealand"
  18. };
  19. Console.WriteLine("------------自定义序列化添加对象属性------------");
  20. string json = JsonConvert.SerializeObject(city, Formatting.Indented, new CustomConverter(typeof(City)));
  21. Console.WriteLine(json);
  22. Console.WriteLine("------------自定义反序列化添加对象属性------------");
  23. City newCity = JsonConvert.DeserializeObject<City>(json, new CustomConverter(typeof(City)));
  24. Console.WriteLine(newCity.Country);
  25. Console.WriteLine(newCity.Name.Count);
  26. Console.WriteLine(string.Join("/", newCity.Name.ToArray()));
  27. }
  28. }
  29. }

4.运行的结果,注意:序列化City对象时,添加了Keys对象的属性,反序列化时属性中的值多添加了一次,使得Name属性总数量是6.,而不是3.
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,464人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# Json序列序列

    前言:在实际开发过程中经常都要和Json打交道,序列化与反序列化就成了开发中必不可缺的技能。本篇博客就教大家如何进行Json序列化与反序列化。 首先要添加引用NuGet包,N