Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonObject.cs
Go to the documentation of this file.
6
8
9[DebuggerDisplay("JsonObject[{Count}]")]
10[DebuggerTypeProxy(typeof(DebugView))]
11public sealed class JsonObject : JsonNode, IDictionary<string, JsonNode?>, ICollection<KeyValuePair<string, JsonNode?>>, IEnumerable<KeyValuePair<string, JsonNode?>>, IEnumerable
12{
14 private class DebugView
15 {
16 [DebuggerDisplay("{Display,nq}")]
17 private struct DebugViewProperty
18 {
21
23 public string PropertyName;
24
26 public string Display
27 {
28 get
29 {
30 if (Value == null)
31 {
32 return PropertyName + " = null";
33 }
34 if (Value is JsonValue)
35 {
36 return PropertyName + " = " + Value.ToJsonString();
37 }
39 {
40 return $"{PropertyName} = JsonObject[{jsonObject.Count}]";
41 }
43 return $"{PropertyName} = JsonArray[{jsonArray.Count}]";
44 }
45 }
46 }
47
50
51 public string Json => _node.ToJsonString();
52
53 public string Path => _node.GetPath();
54
56 private DebugViewProperty[] Items
57 {
58 get
59 {
61 int num = 0;
63 {
64 array[num].PropertyName = item.Key;
65 array[num].Value = item.Value;
66 num++;
67 }
68 return array;
69 }
70 }
71
73 {
74 _node = node;
75 }
76 }
77
79
81
82 public int Count
83 {
84 get
85 {
87 return _dictionary.Count;
88 }
89 }
90
99
108
110
112 : base(options)
113 {
114 }
115
117 {
118 foreach (KeyValuePair<string, JsonNode> property in properties)
119 {
120 Add(property.Key, property.Value);
121 }
122 }
123
124 public static JsonObject? Create(JsonElement element, JsonNodeOptions? options = null)
125 {
126 if (element.ValueKind == JsonValueKind.Null)
127 {
128 return null;
129 }
130 if (element.ValueKind == JsonValueKind.Object)
131 {
132 return new JsonObject(element, options);
133 }
135 }
136
138 : base(options)
139 {
140 _jsonElement = element;
141 }
142
144 {
145 return ((IDictionary<string, JsonNode>)this).TryGetValue(propertyName, out jsonNode);
146 }
147
149 {
150 if (writer == null)
151 {
152 throw new ArgumentNullException("writer");
153 }
154 if (_jsonElement.HasValue)
155 {
157 return;
158 }
159 if (options == null)
160 {
162 }
163 writer.WriteStartObject();
165 {
166 while (enumerator.MoveNext())
167 {
169 writer.WritePropertyName(current.Key);
170 JsonNodeConverter.Instance.Write(writer, current.Value, options);
171 }
172 }
173 writer.WriteEndObject();
174 }
175
177 {
179 {
180 return jsonNode;
181 }
182 return null;
183 }
184
185 internal override void GetPath(List<string> path, JsonNode child)
186 {
187 if (child != null)
188 {
190 string key = _dictionary.FindValue(child).Value.Key;
191 if (key.IndexOfAny(ReadStack.SpecialCharacters) != -1)
192 {
193 path.Add("['" + key + "']");
194 }
195 else
196 {
197 path.Add("." + key);
198 }
199 }
200 if (base.Parent != null)
201 {
202 base.Parent.GetPath(path, this);
203 }
204 }
205
206 internal void SetItem(string propertyName, JsonNode value)
207 {
210 {
211 value?.AssignParent(this);
212 });
214 }
215
217 {
219 if (item != null)
220 {
221 item.Parent = null;
222 }
223 }
224
225 public void Add(string propertyName, JsonNode? value)
226 {
229 value?.AssignParent(this);
230 }
231
233 {
234 Add(property.Key, property.Value);
235 }
236
237 public void Clear()
238 {
239 if (_jsonElement.HasValue)
240 {
241 _jsonElement = null;
242 }
243 else
244 {
245 if (_dictionary == null)
246 {
247 return;
248 }
249 foreach (JsonNode item in _dictionary.GetValueCollection())
250 {
252 }
254 }
255 }
256
257 public bool ContainsKey(string propertyName)
258 {
261 }
262
263 public bool Remove(string propertyName)
264 {
265 if (propertyName == null)
266 {
267 throw new ArgumentNullException("propertyName");
268 }
271 bool flag = _dictionary.TryRemoveProperty(propertyName, out existing);
272 if (flag)
273 {
275 }
276 return flag;
277 }
278
284
290
296
301
307
313
314 private void InitializeIfRequired()
315 {
316 if (_dictionary != null)
317 {
318 return;
319 }
320 bool caseInsensitive = base.Options.HasValue && base.Options.Value.PropertyNameCaseInsensitive;
322 if (_jsonElement.HasValue)
323 {
324 foreach (JsonProperty item in _jsonElement.Value.EnumerateObject())
325 {
327 if (jsonNode != null)
328 {
329 jsonNode.Parent = this;
330 }
332 }
333 _jsonElement = null;
334 }
336 }
337}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
static string NodeElementWrongType
Definition SR.cs:286
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static readonly JsonSerializerOptions s_defaultOptions
string ToJsonString(JsonSerializerOptions? options=null)
Definition JsonNode.cs:527
void Add(string propertyName, JsonNode? value)
void Add(KeyValuePair< string, JsonNode?> property)
JsonObject(JsonNodeOptions? options=null)
void DetachParent(JsonNode item)
override void GetPath(List< string > path, JsonNode child)
JsonObject(IEnumerable< KeyValuePair< string, JsonNode?> > properties, JsonNodeOptions? options=null)
bool Remove(string propertyName)
bool ContainsKey(string propertyName)
JsonObject(JsonElement element, JsonNodeOptions? options=null)
bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode)
override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options=null)
IEnumerator< KeyValuePair< string, JsonNode?> > GetEnumerator()
JsonNode GetItem(string propertyName)
JsonPropertyDictionary< JsonNode > _dictionary
Definition JsonObject.cs:80
void SetItem(string propertyName, JsonNode value)
static ? JsonObject Create(JsonElement element, JsonNodeOptions? options=null)
static JsonNode Create(JsonElement element, JsonNodeOptions? options)
new IEnumerator< T > GetEnumerator()
void WriteTo(Utf8JsonWriter writer)
ObjectEnumerator EnumerateObject()
static readonly char[] SpecialCharacters
Definition ReadStack.cs:13