123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #!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<ADT> {
- 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<Dictionary<int, ADT>> {
- public override Dictionary<int, ADT> Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) {
- if (reader.TokenType != JsonTokenType.StartObject) {
- throw new JsonException();
- }
- var converter = (JsonConverter<ADT>)options.GetConverter(typeof(ADT));
- Dictionary<int, ADT> dictionary = new Dictionary<int, ADT>();
- Regex regex = new Regex(@"S(?<stream>\d{1,3})F(?<function>\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<int, ADT> dictionary, JsonSerializerOptions options) {
- writer.WriteStartObject();
-
- var converter = (JsonConverter<ADT>)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<bool>();
- Dictionary<int, ADT> ruleValue = JsonSerializer.Deserialize<Dictionary<int, ADT>>(ruleNode.ToJsonString(), jsonSerializeOptions);
- enableValue.Display();
- ruleValue.Display();
|