Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonPropertyDictionary.cs
Go to the documentation of this file.
3
4namespace System.Text.Json;
5
6internal class JsonPropertyDictionary<T> where T : class
7{
8 private sealed class KeyCollection : ICollection<string>, IEnumerable<string>, IEnumerable
9 {
11
12 public int Count => _parent.Count;
13
14 public bool IsReadOnly => true;
15
20
22 {
24 {
25 yield return item.Key;
26 }
27 }
28
33
38
39 public bool Contains(string propertyName)
40 {
41 return _parent.ContainsProperty(propertyName);
42 }
43
44 public void CopyTo(string[] propertyNameArray, int index)
45 {
46 if (index < 0)
47 {
49 }
51 {
52 if (index >= propertyNameArray.Length)
53 {
55 }
57 }
58 }
59
61 {
63 {
64 yield return item.Key;
65 }
66 }
67
72 }
73
74 private sealed class ValueCollection : ICollection<T>, IEnumerable<T>, IEnumerable
75 {
77
78 public int Count => _parent.Count;
79
80 public bool IsReadOnly => true;
81
86
88 {
90 {
91 yield return item.Value;
92 }
93 }
94
99
104
105 public bool Contains(T jsonNode)
106 {
108 }
109
110 public void CopyTo(T[] nodeArray, int index)
111 {
112 if (index < 0)
113 {
115 }
117 {
118 if (index >= nodeArray.Length)
119 {
121 }
122 nodeArray[index++] = item.Value;
123 }
124 }
125
127 {
129 {
130 yield return item.Value;
131 }
132 }
133
138 }
139
141
143
145
147
149
151
152 public int Count => _propertyList.Count;
153
155
157
158 public bool IsReadOnly { get; }
159
160 public T this[string propertyName]
161 {
162 get
163 {
165 {
166 return value;
167 }
168 return null;
169 }
170 set
171 {
173 }
174 }
175
176 public JsonPropertyDictionary(bool caseInsensitive)
177 {
180 }
181
187
188 public void Add(string propertyName, T value)
189 {
190 if (IsReadOnly)
191 {
193 }
194 if (propertyName == null)
195 {
196 throw new ArgumentNullException("propertyName");
197 }
199 }
200
209
210 public bool TryAdd(string propertyName, T value)
211 {
212 if (IsReadOnly)
213 {
215 }
217 }
218
228
229 public bool ContainsKey(string propertyName)
230 {
231 if (propertyName == null)
232 {
233 throw new ArgumentNullException("propertyName");
234 }
236 }
237
239 {
241 {
242 while (enumerator.MoveNext())
243 {
244 KeyValuePair<string, T> current = enumerator.Current;
245 if (item.Value == current.Value && _stringComparer.Equals(item.Key, current.Key))
246 {
247 return true;
248 }
249 }
250 }
251 return false;
252 }
253
255 {
256 if (index < 0)
257 {
259 }
261 {
262 if (index >= array.Length)
263 {
265 }
266 array[index++] = property;
267 }
268 }
269
277
278 public bool TryGetValue(string propertyName, out T value)
279 {
280 if (propertyName == null)
281 {
282 throw new ArgumentNullException("propertyName");
283 }
284 if (_propertyDictionary != null)
285 {
287 }
289 {
291 {
292 value = property.Value;
293 return true;
294 }
295 }
296 value = null;
297 return false;
298 }
299
300 public T SetValue(string propertyName, T value, Action assignParent = null)
301 {
302 if (IsReadOnly)
303 {
305 }
306 if (propertyName == null)
307 {
308 throw new ArgumentNullException("propertyName");
309 }
311 T val = null;
312 if (_propertyDictionary != null)
313 {
315 {
316 assignParent?.Invoke();
318 return null;
319 }
321 if (val == value)
322 {
323 return null;
324 }
325 }
326 int num = FindValueIndex(propertyName);
327 if (num >= 0)
328 {
329 if (_propertyDictionary != null)
330 {
332 }
333 else
334 {
336 if (keyValuePair.Value == value)
337 {
338 return null;
339 }
340 val = keyValuePair.Value;
341 }
342 assignParent?.Invoke();
344 }
345 else
346 {
347 assignParent?.Invoke();
350 }
351 return val;
352 }
353
361
362 private bool TryAddValue(string propertyName, T value)
363 {
364 if (IsReadOnly)
365 {
367 }
369 if (_propertyDictionary == null)
370 {
372 {
373 return false;
374 }
375 }
377 {
378 return false;
379 }
381 return true;
382 }
383
385 {
386 if (_propertyDictionary == null && _propertyList.Count > 9)
387 {
388 _propertyDictionary = JsonHelpers.CreateDictionaryFromCollection(_propertyList, _stringComparer);
389 }
390 }
391
392 private bool ContainsValue(T value)
393 {
394 foreach (T item in GetValueCollection())
395 {
396 if (item == value)
397 {
398 return true;
399 }
400 }
401 return false;
402 }
403
405 {
407 {
408 while (enumerator.MoveNext())
409 {
410 KeyValuePair<string, T> current = enumerator.Current;
411 if (current.Value == value)
412 {
413 return current;
414 }
415 }
416 }
417 return null;
418 }
419
420 private bool ContainsProperty(string propertyName)
421 {
422 if (_propertyDictionary != null)
423 {
425 }
427 {
429 {
430 return true;
431 }
432 }
433 return false;
434 }
435
436 private int FindValueIndex(string propertyName)
437 {
438 for (int i = 0; i < _propertyList.Count; i++)
439 {
442 {
443 return i;
444 }
445 }
446 return -1;
447 }
448
450 {
452 }
453
455 {
456 if (IsReadOnly)
457 {
459 }
460 if (_propertyDictionary != null)
461 {
463 {
464 return false;
465 }
467 }
468 for (int i = 0; i < _propertyList.Count; i++)
469 {
472 {
474 existing = keyValuePair.Value;
475 return true;
476 }
477 }
478 existing = null;
479 return false;
480 }
481
483 {
484 return _keyCollection ?? (_keyCollection = new KeyCollection(this));
485 }
486
488 {
489 return _valueCollection ?? (_valueCollection = new ValueCollection(this));
490 }
491}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool TryAdd(TKey key, TValue value)
void Add(TKey key, TValue value)
void RemoveAt(int index)
Definition List.cs:824
static StringComparer Ordinal
new bool Equals(object? x, object? y)
void CopyTo(string[] propertyNameArray, int index)
KeyCollection(JsonPropertyDictionary< T > jsonObject)
ValueCollection(JsonPropertyDictionary< T > jsonObject)
IEnumerator< KeyValuePair< string, T > > GetEnumerator()
readonly List< KeyValuePair< string, T > > _propertyList
KeyValuePair< string, T >? FindValue(T value)
void Add(string propertyName, T value)
void AddValue(string propertyName, T value)
bool TryGetPropertyValue(string propertyName, out T value)
bool TryAdd(string propertyName, T value)
JsonPropertyDictionary(bool caseInsensitive, int capacity)
void Add(KeyValuePair< string, T > property)
void CopyTo(KeyValuePair< string, T >[] array, int index)
bool TryGetValue(string propertyName, out T value)
bool Contains(KeyValuePair< string, T > item)
bool TryAddValue(string propertyName, T value)
bool TryRemoveProperty(string propertyName, out T existing)
T SetValue(string propertyName, T value, Action assignParent=null)
static void ThrowNotSupportedException_NodeCollectionIsReadOnly()
static void ThrowArgumentException_NodeArrayTooSmall(string paramName)
static NotSupportedException NotSupportedException_NodeCollectionIsReadOnly()
static void ThrowArgumentOutOfRangeException_NodeArrayIndexNegative(string paramName)
static void ThrowArgumentException_DuplicateKey(string propertyName)
new IEnumerator< T > GetEnumerator()