GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
JSONSerializers.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 System;
18using System.Globalization;
19using System.Net;
20using System.Numerics;
21using Newtonsoft.Json;
22
23namespace GenerallyPositive
24{
25 public class IPAddressJsonConverter : JsonConverter<IPAddress>
26 {
27 public override void WriteJson(JsonWriter writer, IPAddress value, JsonSerializer serializer)
28 {
29 writer.WriteValue(value?.ToString());
30 }
31
32 public override IPAddress ReadJson(JsonReader reader, Type objectType, IPAddress existingValue, bool hasExistingValue, JsonSerializer serializer)
33 {
34 var s = reader.Value as string;
35 return string.IsNullOrEmpty(s) ? null : IPAddress.Parse(s);
36 }
37 }
38
39 public class ComplexJsonConverter : JsonConverter<Complex>
40 {
41 public override void WriteJson(JsonWriter writer, Complex value, JsonSerializer serializer)
42 {
43 string separator = value.Imaginary >= 0 ? "+" : "-";
44 writer.WriteValue($"{value.Real.ToString(CultureInfo.InvariantCulture)}{separator}{Math.Abs(value.Imaginary).ToString(CultureInfo.InvariantCulture)}i");
45 }
46
47 public override Complex ReadJson(JsonReader reader, Type objectType, Complex existingValue, bool hasExistingValue, JsonSerializer serializer)
48 {
49 var s = reader.Value as string;
50 if (string.IsNullOrEmpty(s)) return Complex.Zero;
51
52 return ConverterHelper.ParseComplex(s);
53 }
54 }
55
56 public class UriJsonConverter : JsonConverter<Uri>
57 {
58 public override void WriteJson(JsonWriter writer, Uri value, JsonSerializer serializer)
59 {
60 writer.WriteValue(value?.OriginalString);
61 }
62
63 public override Uri ReadJson(JsonReader reader, Type objectType, Uri existingValue, bool hasExistingValue, JsonSerializer serializer)
64 {
65 var s = reader.Value as string;
66 return string.IsNullOrEmpty(s) ? null : new Uri(s, UriKind.RelativeOrAbsolute);
67 }
68 }
69
70 public class VersionJsonConverter : JsonConverter<Version>
71 {
72 public override void WriteJson(JsonWriter writer, Version value, JsonSerializer serializer)
73 {
74 writer.WriteValue(value?.ToString());
75 }
76
77 public override Version ReadJson(JsonReader reader, Type objectType, Version existingValue, bool hasExistingValue, JsonSerializer serializer)
78 {
79 var s = reader.Value as string;
80 return string.IsNullOrEmpty(s) ? null : new Version(s);
81 }
82 }
83
84 // BigInteger usually works fine in recent Json.NET versions (serialized as string for large values),
85 // but if you hit issues with very large numbers, add this:
86 public class BigIntegerJsonConverter : JsonConverter<BigInteger>
87 {
88 public override void WriteJson(JsonWriter writer, BigInteger value, JsonSerializer serializer)
89 {
90 writer.WriteValue(value.ToString());
91 }
92
93 public override BigInteger ReadJson(JsonReader reader, Type objectType, BigInteger existingValue, bool hasExistingValue, JsonSerializer serializer)
94 {
95 var s = reader.Value as string;
96 return string.IsNullOrEmpty(s) ? BigInteger.Zero : BigInteger.Parse(s);
97 }
98 }
99}