#!meta {"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}} #!csharp #!value --from-file ../data/json/test01.json --name JsonData #!csharp class ADT { public string Value { get; set; } public string Action { get; set; } } #!csharp using System.Text.Json; using System.Text.Json.Serialization; class ADTJsonConverter : JsonConverter { public override ADT Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } ADT result = new ADT(); while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return result; } if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string PropertyName = reader.GetString(); switch (PropertyName) { case "action": if (reader.Read()) { result.Action = reader.GetString(); } break; case "message": if (reader.Read()) { result.Value = reader.GetString(); } break; } } return null; } public override void Write(Utf8JsonWriter writer, ADT value, JsonSerializerOptions options) { writer.WriteStartObject(); writer.WritePropertyName("action"); writer.WriteStringValue(value.Action); writer.WritePropertyName("message"); writer.WriteStringValue(value.Value); writer.WriteEndObject(); } } #!csharp using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; class RuleJsonConverter : JsonConverter> { public override Dictionary Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } var converter = (JsonConverter)options.GetConverter(typeof(ADT)); Dictionary dictionary = new Dictionary(); Regex regex = new Regex(@"S(?\d{1,3})F(?\d{1,3})"); while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return dictionary; } if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string PropertyName = reader.GetString(); Match match = regex.Match(PropertyName); int stream = int.Parse(match.Groups["stream"].ToString()); int function = int.Parse(match.Groups["function"].ToString()); if (0 > stream || stream > 256 || 0 > function || function > 256) { throw new JsonException(); } int key = (stream << 8) | (function); if (reader.Read()) { ADT value = converter.Read(ref reader, typeof(ADT), options); if (dictionary.TryAdd(key, value) == false) { dictionary[key] = value; } } } return null; } public override void Write(Utf8JsonWriter writer, Dictionary dictionary, JsonSerializerOptions options) { writer.WriteStartObject(); var converter = (JsonConverter)options.GetConverter(typeof(ADT)); foreach ((int key, ADT value) in dictionary) { string propertyName = $"S{(key >> 8) & 0xff}F{key & 0xff}"; writer.WritePropertyName(propertyName); converter.Write(writer, value, options); } writer.WriteEndObject(); } } #!csharp #!share JsonData --from value --as jsonData using System.Text.Json; using System.Text.Json.Nodes; var jsonSerializeOptions = new JsonSerializerOptions { WriteIndented = true, Converters = { new RuleJsonConverter(), new ADTJsonConverter(), } }; var nodes = JsonNode.Parse(jsonData); var enableNode = nodes["enable"]; var ruleNode = nodes["rule"]; bool enableValue = enableNode.GetValue(); Dictionary ruleValue = JsonSerializer.Deserialize>(ruleNode.ToJsonString(), jsonSerializeOptions); enableValue.Display(); ruleValue.Display();