|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | + |
| 9 | +namespace BootstrapBlazor.Components; |
| 10 | + |
| 11 | +public class ColumnVisibleItemConverter : JsonConverter<ColumnVisibleItem> |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// <inheritdoc/> |
| 15 | + /// </summary> |
| 16 | + /// <param name="reader"></param> |
| 17 | + /// <param name="typeToConvert"></param> |
| 18 | + /// <param name="options"></param> |
| 19 | + /// <returns></returns> |
| 20 | + public override ColumnVisibleItem? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 21 | + { |
| 22 | + string? name = null; |
| 23 | + bool visible = false; |
| 24 | + if (reader.TokenType == JsonTokenType.StartObject) |
| 25 | + { |
| 26 | + while (reader.Read()) |
| 27 | + { |
| 28 | + if (reader.TokenType == JsonTokenType.EndObject) |
| 29 | + { |
| 30 | + break; |
| 31 | + } |
| 32 | + if (reader.TokenType == JsonTokenType.PropertyName) |
| 33 | + { |
| 34 | + var propertyName = reader.GetString(); |
| 35 | + if (propertyName == "name") |
| 36 | + { |
| 37 | + reader.Read(); |
| 38 | + name = reader.GetString(); |
| 39 | + } |
| 40 | + else if (propertyName == "visible") |
| 41 | + { |
| 42 | + reader.Read(); |
| 43 | + visible = reader.GetBoolean(); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return new ColumnVisibleItem(name, visible); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// <inheritdoc/> |
| 53 | + /// </summary> |
| 54 | + /// <param name="writer"></param> |
| 55 | + /// <param name="value"></param> |
| 56 | + /// <param name="options"></param> |
| 57 | + public override void Write(Utf8JsonWriter writer, ColumnVisibleItem value, JsonSerializerOptions options) |
| 58 | + { |
| 59 | + writer.WriteStartObject(); |
| 60 | + writer.WriteString("name", value.Name); |
| 61 | + writer.WriteBoolean("visible", value.Visible); |
| 62 | + writer.WriteEndObject(); |
| 63 | + } |
| 64 | +} |
0 commit comments