Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlElementJsonEncoder.cs
1using System;
3using System.Text;
4using System.Xml;
8
10{
15 {
20 {
21 }
22
29 public void Encode(object Object, int? Indent, StringBuilder Json)
30 {
31 XmlElement Xml = (XmlElement)Object;
32 Encode(Xml, Indent, Json);
33 }
34
41 public static void Encode(XmlElement Xml, int? Indent, StringBuilder Json)
42 {
43 bool First = true;
44
45 Json.Append('{');
46
47 if (Indent.HasValue)
48 Indent++;
49
50 AppendProperty("__name", Xml.LocalName, Indent, ref First, Json);
51 AppendProperty("__ns", Xml.NamespaceURI, Indent, ref First, Json);
52
53 foreach (XmlAttribute Attr in Xml.Attributes)
54 AppendProperty(Attr.Name, Attr.Value, Indent, ref First, Json);
55
56 if (Xml.HasChildNodes)
57 {
58 Dictionary<string, ChunkedList<XmlElement>> ChildElements = null;
59 StringBuilder InnerText = null;
60
61 foreach (XmlNode N in Xml.ChildNodes)
62 {
63 if (N is XmlElement E)
64 {
65 if (ChildElements is null)
66 ChildElements = new Dictionary<string, ChunkedList<XmlElement>>();
67
68 if (!ChildElements.TryGetValue(E.LocalName, out ChunkedList<XmlElement> Elements))
69 {
70 Elements = new ChunkedList<XmlElement>();
71 ChildElements[E.LocalName] = Elements;
72 }
73
74 Elements.Add(E);
75 }
76 else if (N is XmlText ||
77 N is XmlCDataSection ||
78 N is XmlWhitespace ||
79 N is XmlSignificantWhitespace)
80 {
81 if (InnerText is null)
82 InnerText = new StringBuilder();
83
84 InnerText.Append(N.InnerText);
85 }
86 }
87
88 if (!(ChildElements is null))
89 {
90 foreach (KeyValuePair<string, ChunkedList<XmlElement>> P in ChildElements)
91 {
92 if (P.Value.Count == 1)
93 AppendProperty(P.Key, P.Value.FirstItem, Indent, ref First, Json);
94 else
95 AppendProperty(P.Key, P.Value, Indent, ref First, Json);
96 }
97
98 if (!(InnerText is null))
99 {
100 AppendProperty(ChildElements.ContainsKey("value") ? "__value" : "value",
101 InnerText.ToString(), Indent, ref First, Json);
102 }
103 }
104 else if (!(InnerText is null))
105 AppendProperty("value", InnerText.ToString(), Indent, ref First, Json);
106 }
107
108 if (Indent.HasValue)
109 {
110 Indent--;
111
112 if (!First)
113 {
114 Json.AppendLine();
115 JSON.Indent(Json, Indent.Value);
116 }
117 }
118
119 Json.Append('}');
120 }
121
122 private static void AppendProperty(string Name, object Value, int? Indent,
123 ref bool First, StringBuilder Json)
124 {
125 if (First)
126 First = false;
127 else
128 Json.Append(',');
129
130 if (Indent.HasValue)
131 {
132 Json.AppendLine();
133 JSON.Indent(Json, Indent.Value);
134 }
135
136 Json.Append('"');
137 Json.Append(JSON.Encode(Name));
138 Json.Append("\":");
139
140 if (Indent.HasValue)
141 Json.Append(' ');
142
143 JSON.Encode(Value, Indent, Json);
144 }
145
151 public Grade Supports(Type ObjectType)
152 {
153 return ObjectType == typeof(XmlElement) ? Grade.Ok : Grade.NotAtAll;
154 }
155
156 }
157}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static void Indent(StringBuilder Output, int NrTabs)
Appends an indentation to a string builder using tab characters.
Definition: JSON.cs:762
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Encodes an XML Element as a JSON object.
static void Encode(XmlElement Xml, int? Indent, StringBuilder Json)
Encodes an XmlElement to JSON.
void Encode(object Object, int? Indent, StringBuilder Json)
Encodes the Object to JSON.
XmlElementJsonEncoder()
Encodes an XML Element as a JSON object.
Grade Supports(Type ObjectType)
How well the JSON encoder encodes objects of type ObjectType .
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Interface for encoding objects of certain types to JSON.
Definition: IJsonEncoder.cs:11
Grade
Grade enumeration
Definition: Grade.cs:7