8 public class Json : IDisposable {
9 #region PublicInterface 21 if (!SkipWhiteSpace())
return this;
23 if (json[idx] ==
'{') { ParseToNode(); }
else { ParseOneEntryToNode(); }
29 public void Dispose() => tree.Dispose();
38 private string json =
"";
41 private readonly StringBuilder builder =
new StringBuilder();
45 private void ParseToNode() {
46 while (ParseOneEntryToNode()) {
47 if (!SkipWhiteSpace() || (json[idx] ==
'}')) {
54 private bool ParseOneEntryToNode() {
55 if (!SkipWhiteSpace())
return false;
57 string key = json[idx++] ==
'"' ? ParseString() : NextWord(-1);
58 if (json[idx] ==
':') idx++;
65 return idx < json.Length;
68 private void ParseObject() {
69 if (!SkipWhiteSpace())
return;
71 char token = json[idx++];
81 tree.
Leaf = ParseString();
84 tree.
Leaf = NextWord(-1);
91 private void ParseArray() {
94 while (SkipWhiteSpace() && (json[idx] !=
']')) {
96 tree.
Add(index++.ToString());
104 private string ParseString() {
107 while ((idx < json.Length) && (json[idx] !=
'"')) builder.Append(json[idx] ==
'\\' ? Escape() : json[idx++]);
111 return builder.ToString();
114 private char Escape() {
115 switch (json[++idx]) {
116 case 'b':
return '\b';
117 case 'f':
return '\f';
118 case 'n':
return '\n';
119 case 'r':
return '\t';
120 case 't':
return '\t';
121 default:
return json[idx];
125 private string NextWord(
int offset = 0) {
126 int first = idx += offset;
127 while ((idx < json.Length) && (
"{}[]\",:".IndexOf(json[idx]) == -1)) idx++;
128 string word = json.Substring(startIndex: first, length: idx - first);
133 private static bool IsWhiteSpace(
char chr) =>
char.IsWhiteSpace(chr) || (chr ==
',');
135 private bool SkipWhiteSpace() {
136 while ((idx < json.Length) && IsWhiteSpace(json[idx])) idx++;
137 return idx < json.Length;
static Json Instance
Retrieve a Json instance used to load data into
Deserialisation from JSON format
Trees Node
Retrieve the current node in the parsed JSON tree
Json Parse(string jsonText)
Parse text containing valid but unknown format JSON
static Trees Instance
Fetch a cached Trees instance
IDisposable Anchor(string path="")
Mark here so wec an return to it
void Dispose()
Clear deserialized JSON tree
Trees Add(string path)
Walk the path creating new segments as needed