Gsixin

导航

Newtonsoft.json 自定义序列化操作,序列化的时候加一些额外操作。

功能:把对象里flg或flag名字的参数的数值,转换为二进制字符串,字符串整合在一起。

Main

            var obj = new object[] { new {
                PPPPP = new { fffffffffff = "eeeee",
                    flg = 5,
                    fff = new { d = 0, flg = 999 }
                },
                flag = 3,
                flg = 222,
                FirstName = "James",
                LastName = "Newton-King",
                Roles = new  object[]{ "Admin", "ddddddddddd" }
            }};

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented, new BitFlagConverter());
            string json2 = JsonConvert.SerializeObject(obj, Formatting.Indented);

            Console.WriteLine(json);
            Console.WriteLine("+++++++++++++++++++++++++++++++");
            Console.WriteLine(json2);
            Console.ReadLine();

Class

 public class BitFlagConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var jtoken = JToken.FromObject(value);
            JObject jToken = null;
            JArray jArray = null;
            if (value.GetType().IsArray)
            {
                jArray = jtoken as JArray;
            }
            else
            {
                jToken = jtoken as JObject;
            }
            if (jToken != null)
            {
                Recurse(jToken, "FLAG_Comb", "flag", "flg");
            }
            if (jArray != null)
            {
                foreach (var item in jArray)
                {
                    Recurse(item as JObject, "FLAG_Comb", "flag", "flg");
                }
            }
            jtoken.WriteTo(writer);

        }

        private void Recurse(JObject jobj, string newName, params string[] namePattern)
        {
            string flgVal = "";
            foreach (var item in jobj.Children())
            {
                var dd = item as JProperty;
                if (dd.Value.Type == JTokenType.Object)
                {
                    Recurse(dd.Value as JObject, newName, namePattern);
                }
                else if (dd.Value.Type == JTokenType.Array)
                {
                    var fff = dd.Value as JArray;
                    foreach (var itm in fff)
                    {
                        if (itm.Type == JTokenType.Object)
                        {
                            Recurse(itm as JObject, newName, namePattern);
                        }
                    }
                }
                else if (dd.Value.Type == JTokenType.Integer || dd.Value.Type == JTokenType.String)
                {
                    if (namePattern.Any(tt => dd.Name.ToLower() == tt))
                    {
                        int.TryParse(dd.Value.ToString(), out int td);
                        if (string.IsNullOrEmpty(flgVal))
                        {
                            flgVal = $"{dd.Name}: {Convert.ToString(td, 2)};";
                        }
                        else
                        {
                            flgVal += $"  {dd.Name}: {Convert.ToString(td, 2)};";
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(flgVal))
            {
                jobj.Add(newName, flgVal);
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return true;
        }
    }

posted on 2022-05-27 17:21  GoldenGao  阅读(274)  评论(0编辑  收藏  举报