.net5 中JsonConverter正确实现方式

public class SmartObjectJsonConverter : JsonConverter<SmartObject>
{
            public override SmartObject Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            {
                SmartObject obj = new SmartObject();
                dynamic d = obj;
                if (reader.TokenType==JsonTokenType.StartObject)
                {
                    while (reader.Read())
                    {
                        switch (reader.TokenType)
                        {
                            case JsonTokenType.PropertyName:
                                var propertyName = GetPropertyName(ref reader,options);
                                reader.Read();//读取属性
                                var propertyValue = GetPropertyValue(ref reader, options);
                                d[propertyName] = propertyValue;
                                break;
                            default:
                                break;
                        }
                    }
                }                
                return obj;
            }

            private List<object> GetList(ref Utf8JsonReader reader, JsonSerializerOptions options)
            {
                List<object> list = new List<object>();
                while (reader.Read())
                {
                    if (reader.TokenType != JsonTokenType.EndArray)
                    {
                        list.Add(GetPropertyValue(ref reader, options));
                    }
                    else
                    {
                        break;
                    }
                }
                return list;
            }

            private SmartObject GetObject(ref Utf8JsonReader reader, JsonSerializerOptions options)
            {
                SmartObject obj = new SmartObject();
                dynamic d = obj;
                while (reader.Read())
                {
                    if (reader.TokenType != JsonTokenType.EndObject)
                    {
                        var name = GetPropertyName(ref reader, options);
                        reader.Read();
                        var value = GetPropertyValue(ref reader, options);
                        d[name] = value;
                    }
                    else
                    {
                        break;
                    }
                }
                return obj;
            }

            private string GetPropertyName(ref Utf8JsonReader reader, JsonSerializerOptions options)
            {
                return reader.GetString();
            }

            private object GetPropertyValue(ref Utf8JsonReader reader, JsonSerializerOptions options)
            {
                switch (reader.TokenType)
                {
                    case JsonTokenType.Null:
                        return null;
                    case JsonTokenType.StartArray:
                        return GetList(ref reader,options);
                    case JsonTokenType.StartObject:
                        return GetObject(ref reader,options);
                    case JsonTokenType.String:
                        DateTime time = default(DateTime);
                        Guid g = Guid.Empty;
                        if (reader.TryGetDateTime(out time))
                        {
                            return time;
                        }
                        else if (reader.TryGetGuid(out g))
                        {
                            return g;
                        }
                        else
                        {
                            return reader.GetString();
                        }
                    case JsonTokenType.Number:
                        sbyte sb; byte b; int i; short s; long l; decimal d; double db; float f;
                        if (reader.TryGetSByte(out sb))
                        {
                            return sb;
                        }
                        else if (reader.TryGetByte(out b))
                        {
                            return b;
                        }
                        else if (reader.TryGetInt16(out s))
                        {
                            return s;
                        }
                        else if (reader.TryGetInt32(out i))
                        {
                            return i;
                        }
                        else if (reader.TryGetInt64(out l))
                        {
                            return l;
                        }
                        else if (reader.TryGetSingle(out f))
                        {
                            return f;
                        }
                        else if (reader.TryGetDecimal(out d))
                        {
                            return d;
                        }
                        else if (reader.TryGetDouble(out db))
                        {
                            return db;
                        }
                        else
                        {
                            return 0;
                        }
                    case JsonTokenType.True:
                        return true;
                    case JsonTokenType.False:
                        return false;
                }
                return null;
            }

            public override void Write(Utf8JsonWriter writer, SmartObject value, JsonSerializerOptions options)
            {
                if (value == null)
                {
                    writer.WriteNullValue();
                }
                else
                {
                    writer.WriteStartObject();
                    var properties = value.AsList();
                    foreach (var property in properties)
                    {
                        var val = property.Value;
                        writer.WritePropertyName(property.Key);
                        if (val == null)
                        {
                            writer.WriteNullValue();
                        }
                        else
                        {
                            WriteObject(writer,val,options);
                        }
                    }
                    writer.WriteEndObject();
                }
            }
            private void WriteObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
            {
                if (value == null)
                {
                    writer.WriteNullValue();
                }
                else
                {
                    if (value is SmartObject)
                    {
                        Write(writer, value as SmartObject, options);
                    }
                    else
                    {
                        JsonSerializer.Serialize(writer, value, options);
                    }
                }
            }
}

以上代码递归循环了各种属性。

posted @ 2021-09-29 15:07  Joe·Zhou  阅读(610)  评论(0编辑  收藏  举报