json-converter.dib 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!meta
  2. {"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
  3. #!csharp
  4. #!value --from-file ../data/json/test01.json --name JsonData
  5. #!csharp
  6. class ADT {
  7. public string Value { get; set; }
  8. public string Action { get; set; }
  9. }
  10. #!csharp
  11. using System.Text.Json;
  12. using System.Text.Json.Serialization;
  13. class ADTJsonConverter : JsonConverter<ADT> {
  14. public override ADT Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) {
  15. if (reader.TokenType != JsonTokenType.StartObject) {
  16. throw new JsonException();
  17. }
  18. ADT result = new ADT();
  19. while (reader.Read()) {
  20. if (reader.TokenType == JsonTokenType.EndObject) {
  21. return result;
  22. }
  23. if (reader.TokenType != JsonTokenType.PropertyName) {
  24. throw new JsonException();
  25. }
  26. string PropertyName = reader.GetString();
  27. switch (PropertyName) {
  28. case "action":
  29. if (reader.Read()) {
  30. result.Action = reader.GetString();
  31. }
  32. break;
  33. case "message":
  34. if (reader.Read()) {
  35. result.Value = reader.GetString();
  36. }
  37. break;
  38. }
  39. }
  40. return null;
  41. }
  42. public override void Write(Utf8JsonWriter writer, ADT value, JsonSerializerOptions options) {
  43. writer.WriteStartObject();
  44. writer.WritePropertyName("action");
  45. writer.WriteStringValue(value.Action);
  46. writer.WritePropertyName("message");
  47. writer.WriteStringValue(value.Value);
  48. writer.WriteEndObject();
  49. }
  50. }
  51. #!csharp
  52. using System.Text.Json;
  53. using System.Text.Json.Serialization;
  54. using System.Text.RegularExpressions;
  55. class RuleJsonConverter : JsonConverter<Dictionary<int, ADT>> {
  56. public override Dictionary<int, ADT> Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) {
  57. if (reader.TokenType != JsonTokenType.StartObject) {
  58. throw new JsonException();
  59. }
  60. var converter = (JsonConverter<ADT>)options.GetConverter(typeof(ADT));
  61. Dictionary<int, ADT> dictionary = new Dictionary<int, ADT>();
  62. Regex regex = new Regex(@"S(?<stream>\d{1,3})F(?<function>\d{1,3})");
  63. while (reader.Read()) {
  64. if (reader.TokenType == JsonTokenType.EndObject) {
  65. return dictionary;
  66. }
  67. if (reader.TokenType != JsonTokenType.PropertyName) {
  68. throw new JsonException();
  69. }
  70. string PropertyName = reader.GetString();
  71. Match match = regex.Match(PropertyName);
  72. int stream = int.Parse(match.Groups["stream"].ToString());
  73. int function = int.Parse(match.Groups["function"].ToString());
  74. if (0 > stream || stream > 256 || 0 > function || function > 256) {
  75. throw new JsonException();
  76. }
  77. int key = (stream << 8) | (function);
  78. if (reader.Read()) {
  79. ADT value = converter.Read(ref reader, typeof(ADT), options);
  80. if (dictionary.TryAdd(key, value) == false) {
  81. dictionary[key] = value;
  82. }
  83. }
  84. }
  85. return null;
  86. }
  87. public override void Write(Utf8JsonWriter writer, Dictionary<int, ADT> dictionary, JsonSerializerOptions options) {
  88. writer.WriteStartObject();
  89. var converter = (JsonConverter<ADT>)options.GetConverter(typeof(ADT));
  90. foreach ((int key, ADT value) in dictionary)
  91. {
  92. string propertyName = $"S{(key >> 8) & 0xff}F{key & 0xff}";
  93. writer.WritePropertyName(propertyName);
  94. converter.Write(writer, value, options);
  95. }
  96. writer.WriteEndObject();
  97. }
  98. }
  99. #!csharp
  100. #!share JsonData --from value --as jsonData
  101. using System.Text.Json;
  102. using System.Text.Json.Nodes;
  103. var jsonSerializeOptions = new JsonSerializerOptions
  104. {
  105. WriteIndented = true,
  106. Converters =
  107. {
  108. new RuleJsonConverter(),
  109. new ADTJsonConverter(),
  110. }
  111. };
  112. var nodes = JsonNode.Parse(jsonData);
  113. var enableNode = nodes["enable"];
  114. var ruleNode = nodes["rule"];
  115. bool enableValue = enableNode.GetValue<bool>();
  116. Dictionary<int, ADT> ruleValue = JsonSerializer.Deserialize<Dictionary<int, ADT>>(ruleNode.ToJsonString(), jsonSerializeOptions);
  117. enableValue.Display();
  118. ruleValue.Display();