From cadf9a609fb3d330a6f469c155c502462d9f0d77 Mon Sep 17 00:00:00 2001 From: Aaron Sulwer Date: Wed, 13 Nov 2024 14:27:51 -0700 Subject: [PATCH] removed from example in favor of RulesEngine serialized version --- demo/DemoApp/JsonSerializerConverter.cs | 1 + demo/DemoApp/ObjectAsPrimitiveConverter.cs | 128 --------------------- 2 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 demo/DemoApp/ObjectAsPrimitiveConverter.cs diff --git a/demo/DemoApp/JsonSerializerConverter.cs b/demo/DemoApp/JsonSerializerConverter.cs index 50bd055b..ad61fa58 100644 --- a/demo/DemoApp/JsonSerializerConverter.cs +++ b/demo/DemoApp/JsonSerializerConverter.cs @@ -5,6 +5,7 @@ using RulesEngine.HelperFunctions; using RulesEngine.Models; +using RulesEngine.Serialization; using System; using System.Collections.Generic; using System.Dynamic; diff --git a/demo/DemoApp/ObjectAsPrimitiveConverter.cs b/demo/DemoApp/ObjectAsPrimitiveConverter.cs deleted file mode 100644 index 3b6eae0c..00000000 --- a/demo/DemoApp/ObjectAsPrimitiveConverter.cs +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. -// https://github.com/asulwer/RulesEngine/issues/75 -// https://stackoverflow.com/questions/65972825/c-sharp-deserializing-nested-json-to-nested-dictionarystring-object - -using System; -using System.Collections.Generic; -using System.Dynamic; -using System.Text.Json.Serialization; -using System.Text.Json; - -namespace RulesEngine.HelperFunctions -{ - public class ObjectAsPrimitiveConverter : JsonConverter - { - public FloatFormat FloatFormat { get; init; } - UnknownNumberFormat UnknownNumberFormat { get; init; } - ObjectFormat ObjectFormat { get; init; } - - public ObjectAsPrimitiveConverter() : this(FloatFormat.Double, UnknownNumberFormat.Error, ObjectFormat.Expando) { } - public ObjectAsPrimitiveConverter(FloatFormat floatFormat, UnknownNumberFormat unknownNumberFormat, ObjectFormat objectFormat) - { - this.FloatFormat = floatFormat; - this.UnknownNumberFormat = unknownNumberFormat; - this.ObjectFormat = objectFormat; - } - - public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) - { - if (value.GetType() == typeof(object)) - { - writer.WriteStartObject(); - writer.WriteEndObject(); - } - else - { - JsonSerializer.Serialize(writer, value, value.GetType(), options); - } - } - - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - switch (reader.TokenType) - { - case JsonTokenType.Null: - return null; - case JsonTokenType.False: - return false; - case JsonTokenType.True: - return true; - case JsonTokenType.String: - return reader.GetString(); - case JsonTokenType.Number: - { - if (reader.TryGetInt32(out var i)) - return i; - if (reader.TryGetInt64(out var l)) - return l; - // BigInteger could be added here. - if (FloatFormat == FloatFormat.Decimal && reader.TryGetDecimal(out var m)) - return m; - else if (FloatFormat == FloatFormat.Double && reader.TryGetDouble(out var d)) - return d; - using var doc = JsonDocument.ParseValue(ref reader); - if (UnknownNumberFormat == UnknownNumberFormat.JsonElement) - return doc.RootElement.Clone(); - throw new JsonException(string.Format("Cannot parse number {0}", doc.RootElement.ToString())); - } - case JsonTokenType.StartArray: - { - var list = new List(); - while (reader.Read()) - { - switch (reader.TokenType) - { - default: - list.Add(Read(ref reader, typeof(object), options)); - break; - case JsonTokenType.EndArray: - return list; - } - } - throw new JsonException(); - } - case JsonTokenType.StartObject: - var dict = CreateDictionary(); - while (reader.Read()) - { - switch (reader.TokenType) - { - case JsonTokenType.EndObject: - return dict; - case JsonTokenType.PropertyName: - var key = reader.GetString(); - reader.Read(); - dict.Add(key, Read(ref reader, typeof(object), options)); - break; - default: - throw new JsonException(); - } - } - throw new JsonException(); - default: - throw new JsonException(string.Format("Unknown token {0}", reader.TokenType)); - } - } - - protected virtual IDictionary CreateDictionary() => ObjectFormat == ObjectFormat.Expando ? new ExpandoObject() : new Dictionary(); - } - - public enum FloatFormat - { - Double, - Decimal, - } - - public enum UnknownNumberFormat - { - Error, - JsonElement, - } - - public enum ObjectFormat - { - Expando, - Dictionary, - } -}