Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TOON.cs
1using System;
5
6namespace Waher.Content.Toon
7{
11 public static class TOON
12 {
13 private static readonly Dictionary<Type, IToonEncoder> encoders;
14
15 static TOON()
16 {
17 encoders = new Dictionary<Type, IToonEncoder>();
18 Types.OnInvalidated += Types_OnInvalidated;
19 }
20
21 private static void Types_OnInvalidated(object Sender, EventArgs e)
22 {
23 lock (encoders)
24 {
25 encoders.Clear();
26 }
27 }
28
29 #region Encoding
30
37 public static string Encode(object Object, bool Indent)
38 {
39 ToonOutput sb = new ToonOutput();
40 Encode(Object, Indent, sb);
41 return sb.ToString();
42 }
43
50 public static void Encode(object Object, bool Indent, ToonOutput Toon)
51 {
52 Encode(Object, Indent ? (int?)-1 : null, Toon);
53 }
54
62 public static void Encode(object Object, int? Indent, ToonOutput Toon)
63 {
64 if (Object is null)
65 Toon.Append("null");
66 else
67 {
68 IToonEncoder Encoder = GetEncoder(Object);
69 Encoder.Encode(Object, Indent, Toon);
70 }
71 }
72
79 public static IToonEncoder GetEncoder(object Value)
80 {
81 Type T = Value?.GetType() ?? typeof(object);
82 IToonEncoder Encoder;
83
84 lock (encoders)
85 {
86 if (!encoders.TryGetValue(T, out Encoder))
87 {
88 Encoder = Types.FindBest<IToonEncoder, Type>(T)
89 ?? throw new ArgumentException("Unable to encode objects of type " + T.FullName, nameof(Object));
90
91 encoders[T] = Encoder;
92 }
93 }
94
95 return Encoder;
96 }
97
98 #endregion
99 }
100}
Helps with common TOON-related tasks.
Definition: TOON.cs:12
static void Encode(object Object, bool Indent, ToonOutput Toon)
Encodes an object as TOON.
Definition: TOON.cs:50
static IToonEncoder GetEncoder(object Value)
Gets a TOON encoder for a given object, by looking up the best available encoder, based on the type o...
Definition: TOON.cs:79
static string Encode(object Object, bool Indent)
Encodes an object as TOON.
Definition: TOON.cs:37
static void Encode(object Object, int? Indent, ToonOutput Toon)
Extensible encoding of object Object , by using best available IToonEncoder for the corresponding typ...
Definition: TOON.cs:62
TOON Output builder
Definition: ToonOutput.cs:13
override string ToString()
Definition: ToonOutput.cs:164
void Append(char Value)
Appends a character to the output.
Definition: ToonOutput.cs:173
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
Interface for encoding objects of certain types to TOON.
Definition: IToonEncoder.cs:33
void Encode(object Object, int? Indent, ToonOutput Toon)
Encodes the Object to TOON.