GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
CustomJsonConverter.cs
1// =============================================================================
2// GPAL - Generally Positive Automation Library
3// Copyright © 2026 Software Decisions, Inc. All rights reserved.
4//
5// This file is part of GPAL.
6// Licensed under the Business Source License 1.1
7//
8// Primary development, architecture, and vision by Michael B. Vederman,
9// CEO of Software Decisions, Inc., Texas.
10//
11// Internal development maintained privately.
12// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
13//
14// See LICENSE for full terms, including Additional Use Grant.
15// =============================================================================
16
17using Newtonsoft.Json;
18using Newtonsoft.Json.Converters;
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Text;
23using System.Threading.Tasks;
24
25namespace GenerallyPositive
26{
27 class CustomJsonConverter: CustomCreationConverter<IDictionary<dynamic, dynamic>>
28 {
29 public override IDictionary<dynamic, dynamic> Create(Type objectType)
30 {
31 return new Dictionary<dynamic, dynamic>();
32 }
33
34 public override bool CanConvert(Type objectType)
35 {
36 // in addition to handling IDictionary<string, object>
37 // we want to handle the deserialization of dict value
38 // which is of type object
39 return objectType == typeof(object) || base.CanConvert(objectType);
40 }
41
42 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
43 {
44 if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
45 return base.ReadJson(reader, objectType, existingValue, serializer);
46
47 //if it's an array serialize it as a list of dictionaries
48 if (reader.TokenType == JsonToken.StartArray)
49 return serializer.Deserialize(reader, typeof(List<object>));
50
51
52 // if the next token is not an object
53 // then fall back on standard deserializer (strings, numbers etc.)
54 return serializer.Deserialize(reader);
55 }
56 }
57}
58