This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
JsonArray.cs
117 lines (90 loc) · 3.09 KB
/
JsonArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace System.Json
{
public class JsonArray : JsonValue, IList<JsonValue>
{
private readonly List<JsonValue> _list;
public JsonArray(params JsonValue[] items)
{
_list = new List<JsonValue>();
AddRange(items);
}
public JsonArray(IEnumerable<JsonValue> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
_list = new List<JsonValue>(items);
}
public override int Count => _list.Count;
public bool IsReadOnly => false;
public override sealed JsonValue this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
public override JsonType JsonType => JsonType.Array;
public void Add(JsonValue item)
{
_list.Add(item);
}
public void AddRange(IEnumerable<JsonValue> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
_list.AddRange(items);
}
public void AddRange(params JsonValue[] items)
{
if (items != null)
{
_list.AddRange(items);
}
}
public void Clear() => _list.Clear();
public bool Contains(JsonValue item) => _list.Contains(item);
public void CopyTo(JsonValue[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex);
public int IndexOf(JsonValue item) => _list.IndexOf(item);
public void Insert(int index, JsonValue item) => _list.Insert(index, item);
public bool Remove(JsonValue item) => _list.Remove(item);
public void RemoveAt(int index) => _list.RemoveAt(index);
public override void Save(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
stream.WriteByte((byte)'[');
for (int i = 0; i < _list.Count; i++)
{
JsonValue v = _list[i];
if (v != null)
{
v.Save(stream);
}
else
{
stream.WriteByte((byte)'n');
stream.WriteByte((byte)'u');
stream.WriteByte((byte)'l');
stream.WriteByte((byte)'l');
}
if (i < Count - 1)
{
stream.WriteByte((byte)',');
stream.WriteByte((byte)' ');
}
}
stream.WriteByte((byte)']');
}
IEnumerator<JsonValue> IEnumerable<JsonValue>.GetEnumerator() => _list.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
}
}