Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonArray.cs
Go to the documentation of this file.
6
8
9[DebuggerDisplay("JsonArray[{List.Count}]")]
10[DebuggerTypeProxy(typeof(DebugView))]
11public sealed class JsonArray : JsonNode, IList<JsonNode?>, ICollection<JsonNode?>, IEnumerable<JsonNode?>, IEnumerable
12{
14 private class DebugView
15 {
16 [DebuggerDisplay("{Display,nq}")]
17 private struct DebugViewItem
18 {
21
23 public string Display
24 {
25 get
26 {
27 if (Value == null)
28 {
29 return "null";
30 }
31 if (Value is JsonValue)
32 {
33 return Value.ToJsonString();
34 }
36 {
37 return $"JsonObject[{jsonObject.Count}]";
38 }
40 return $"JsonArray[{jsonArray.List.Count}]";
41 }
42 }
43 }
44
47
48 public string Json => _node.ToJsonString();
49
50 public string Path => _node.GetPath();
51
53 private DebugViewItem[] Items
54 {
55 get
56 {
58 for (int i = 0; i < _node.List.Count; i++)
59 {
60 array[i].Value = _node.List[i];
61 }
62 return array;
63 }
64 }
65
67 {
68 _node = node;
69 }
70 }
71
73
75
77 {
78 get
79 {
81 return _list;
82 }
83 }
84
85 public int Count => List.Count;
86
88
90 : base(options)
91 {
92 }
93
95 : base(options)
96 {
98 }
99
100 public JsonArray(params JsonNode?[] items)
101 {
102 InitializeFromArray(items);
103 }
104
105 private void InitializeFromArray(JsonNode[] items)
106 {
108 for (int i = 0; i < items.Length; i++)
109 {
110 items[i]?.AssignParent(this);
111 }
112 _list = list;
113 }
114
115 public static JsonArray? Create(JsonElement element, JsonNodeOptions? options = null)
116 {
117 if (element.ValueKind == JsonValueKind.Null)
118 {
119 return null;
120 }
121 if (element.ValueKind == JsonValueKind.Array)
122 {
123 return new JsonArray(element, options);
124 }
126 }
127
128 internal JsonArray(JsonElement element, JsonNodeOptions? options = null)
129 : base(options)
130 {
131 _jsonElement = element;
132 }
133
134 [RequiresUnreferencedCode("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.")]
135 public void Add<T>(T? value)
136 {
137 if (value == null)
138 {
139 Add(null);
140 return;
141 }
143 if (jsonNode == null)
144 {
146 }
147 Add(jsonNode);
148 }
149
150 internal JsonNode GetItem(int index)
151 {
152 return List[index];
153 }
154
155 internal void SetItem(int index, JsonNode value)
156 {
157 value?.AssignParent(this);
159 List[index] = value;
160 }
161
162 internal override void GetPath(List<string> path, JsonNode child)
163 {
164 if (child != null)
165 {
166 int value = List.IndexOf(child);
167 path.Add($"[{value}]");
168 }
169 base.Parent?.GetPath(path, this);
170 }
171
173 {
174 if (writer == null)
175 {
176 throw new ArgumentNullException("writer");
177 }
178 if (_jsonElement.HasValue)
179 {
181 return;
182 }
183 CreateNodes();
184 if (options == null)
185 {
187 }
188 writer.WriteStartArray();
189 for (int i = 0; i < _list.Count; i++)
190 {
192 }
193 writer.WriteEndArray();
194 }
195
196 private void CreateNodes()
197 {
198 if (_list != null)
199 {
200 return;
201 }
203 if (!_jsonElement.HasValue)
204 {
205 list = new List<JsonNode>();
206 }
207 else
208 {
210 list = new List<JsonNode>(value.GetArrayLength());
211 foreach (JsonElement item in value.EnumerateArray())
212 {
214 jsonNode?.AssignParent(this);
216 }
217 _jsonElement = null;
218 }
219 _list = list;
220 }
221
222 public void Add(JsonNode? item)
223 {
224 item?.AssignParent(this);
225 List.Add(item);
226 }
227
228 public void Clear()
229 {
230 for (int i = 0; i < List.Count; i++)
231 {
232 DetachParent(List[i]);
233 }
234 List.Clear();
235 }
236
237 public bool Contains(JsonNode? item)
238 {
239 return List.Contains(item);
240 }
241
242 public int IndexOf(JsonNode? item)
243 {
244 return List.IndexOf(item);
245 }
246
247 public void Insert(int index, JsonNode? item)
248 {
249 item?.AssignParent(this);
251 }
252
253 public bool Remove(JsonNode? item)
254 {
255 if (List.Remove(item))
256 {
258 return true;
259 }
260 return false;
261 }
262
263 public void RemoveAt(int index)
264 {
268 }
269
274
276 {
277 return List.GetEnumerator();
278 }
279
284
286 {
287 if (item != null)
288 {
289 item.Parent = null;
290 }
291 }
292}
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
void Insert(int index, T item)
Definition List.cs:650
void CopyTo(T[] array)
Definition List.cs:364
void RemoveAt(int index)
Definition List.cs:824
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
bool Contains(JsonNode? item)
Definition JsonArray.cs:237
bool Remove(JsonNode? item)
Definition JsonArray.cs:253
int IndexOf(JsonNode? item)
Definition JsonArray.cs:242
JsonArray(JsonNodeOptions? options=null)
Definition JsonArray.cs:89
void DetachParent(JsonNode item)
Definition JsonArray.cs:285
override void GetPath(List< string > path, JsonNode child)
Definition JsonArray.cs:162
static ? JsonArray Create(JsonElement element, JsonNodeOptions? options=null)
Definition JsonArray.cs:115
JsonArray(JsonNodeOptions options, params JsonNode?[] items)
Definition JsonArray.cs:94
void SetItem(int index, JsonNode value)
Definition JsonArray.cs:155
JsonArray(JsonElement element, JsonNodeOptions? options=null)
Definition JsonArray.cs:128
JsonArray(params JsonNode?[] items)
Definition JsonArray.cs:100
IEnumerator< JsonNode?> GetEnumerator()
Definition JsonArray.cs:275
void Insert(int index, JsonNode? item)
Definition JsonArray.cs:247
void InitializeFromArray(JsonNode[] items)
Definition JsonArray.cs:105
void Add(JsonNode? item)
Definition JsonArray.cs:222
JsonNode GetItem(int index)
Definition JsonArray.cs:150
override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options=null)
Definition JsonArray.cs:172
void AssignParent(JsonNode parent)
Definition JsonNode.cs:133
string ToJsonString(JsonSerializerOptions? options=null)
Definition JsonNode.cs:527
JsonNode(JsonNodeOptions? options=null)
Definition JsonNode.cs:78
static JsonNode Create(JsonElement element, JsonNodeOptions? options)
new IEnumerator< T > GetEnumerator()
void WriteTo(Utf8JsonWriter writer)
ArrayEnumerator EnumerateArray()