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;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
7
9{
14 {
19 {
20 }
21
28 public void Encode(object Object, int? Indent, StringBuilder Json)
29 {
30 XmlElement Xml = (XmlElement)Object;
31 Encode(Xml, Indent, Json);
32 }
33
40 public static void Encode(XmlElement Xml, int? Indent, StringBuilder Json)
41 {
42 bool First = true;
43
44 Json.Append('{');
45
46 if (Indent.HasValue)
47 Indent++;
48
49 AppendProperty("__name", Xml.LocalName, Indent, ref First, Json);
50 AppendProperty("__ns", Xml.NamespaceURI, Indent, ref First, Json);
51
52 foreach (XmlAttribute Attr in Xml.Attributes)
53 AppendProperty(Attr.Name, Attr.Value, Indent, ref First, Json);
54
55 if (Xml.HasChildNodes)
56 {
57 Dictionary<string, LinkedList<XmlElement>> ChildElements = null;
58 StringBuilder InnerText = null;
59
60 foreach (XmlNode N in Xml.ChildNodes)
61 {
62 if (N is XmlElement E)
63 {
64 if (ChildElements is null)
65 ChildElements = new Dictionary<string, LinkedList<XmlElement>>();
66
67 if (!ChildElements.TryGetValue(E.LocalName, out LinkedList<XmlElement> Elements))
68 {
69 Elements = new LinkedList<XmlElement>();
70 ChildElements[E.LocalName] = Elements;
71 }
72
73 Elements.AddLast(E);
74 }
75 else if (N is XmlText ||
76 N is XmlCDataSection ||
77 N is XmlWhitespace ||
78 N is XmlSignificantWhitespace)
79 {
80 if (InnerText is null)
81 InnerText = new StringBuilder();
82
83 InnerText.Append(N.InnerText);
84 }
85 }
86
87 if (!(ChildElements is null))
88 {
89 foreach (KeyValuePair<string, LinkedList<XmlElement>> P in ChildElements)
90 {
91 if (P.Value.First.Next is null)
92 AppendProperty(P.Key, P.Value.First.Value, Indent, ref First, Json);
93 else
94 AppendProperty(P.Key, P.Value, Indent, ref First, Json);
95 }
96
97 if (!(InnerText is null))
98 {
99 AppendProperty(ChildElements.ContainsKey("value") ? "__value" : "value",
100 InnerText.ToString(), Indent, ref First, Json);
101 }
102 }
103 else if (!(InnerText is null))
104 AppendProperty("value", InnerText.ToString(), Indent, ref First, Json);
105 }
106
107 if (Indent.HasValue)
108 {
109 Indent--;
110
111 if (!First)
112 {
113 Json.AppendLine();
114 Json.Append(new string('\t', Indent.Value));
115 }
116 }
117
118 Json.Append('}');
119 }
120
121 private static void AppendProperty(string Name, object Value, int? Indent,
122 ref bool First, StringBuilder Json)
123 {
124 if (First)
125 First = false;
126 else
127 Json.Append(',');
128
129 if (Indent.HasValue)
130 {
131 Json.AppendLine();
132 Json.Append(new string('\t', Indent.Value));
133 }
134
135 Json.Append('"');
136 Json.Append(JSON.Encode(Name));
137 Json.Append("\":");
138
139 if (Indent.HasValue)
140 Json.Append(' ');
141
142 JSON.Encode(Value, Indent, Json);
143 }
144
150 public Grade Supports(Type ObjectType)
151 {
152 return ObjectType == typeof(XmlElement) ? Grade.Ok : Grade.NotAtAll;
153 }
154
155 }
156}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
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 .
Interface for encoding objects of certain types to JSON.
Definition: IJsonEncoder.cs:11
Grade
Grade enumeration
Definition: Grade.cs:7