/
JData.cs
155 lines (141 loc) · 6.1 KB
/
JData.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Cuemon.IO;
using Newtonsoft.Json;
namespace Cuemon.Extensions.Newtonsoft.Json
{
/// <summary>
/// Provides a factory based way to parse and extract values from various sources of JSON data. Compliant with RFC 7159 as it uses <see cref="JsonTextReader"/> behind the scene.
/// </summary>
public class JData
{
/// <summary>
/// Creates a sequence of <see cref="T:IEnumerable{JDataResult}"/> from the specified <paramref name="json"/>.
/// </summary>
/// <param name="json">A <see cref="Stream"/> that represents a JSON data structure.</param>
/// <param name="setup">The <see cref="StreamReaderOptions" /> which may be configured.</param>
/// <returns>An <see cref="T:IEnumerable{JDataResult}"/> sequence from the specified <see cref="Stream"/>.</returns>
public static IEnumerable<JDataResult> ReadAll(Stream json, Action<StreamReaderOptions> setup = null)
{
Validator.ThrowIfNull(json);
var options = Patterns.Configure(setup);
using (var sr = new StreamReader(json, options.Encoding, false, options.BufferSize, options.LeaveOpen))
{
using (var jr = new JsonTextReader(sr))
{
jr.CloseInput = !options.LeaveOpen;
return ReadAll(jr);
}
}
}
/// <summary>
/// Creates a sequence of <see cref="T:IEnumerable{JDataResult}"/> from the specified <paramref name="json"/>.
/// </summary>
/// <param name="json">A <see cref="string"/> that represents a JSON data structure.</param>
/// <returns>An <see cref="T:IEnumerable{JDataResult}"/> sequence from the specified <see cref="string"/>.</returns>
public static IEnumerable<JDataResult> ReadAll(string json)
{
Validator.ThrowIfNullOrWhitespace(json);
using (var sr = new StringReader(json))
{
using (var jr = new JsonTextReader(sr))
{
return ReadAll(jr);
}
}
}
/// <summary>
/// Creates a sequence of <see cref="T:IEnumerable{JDataResult}"/> from the specified <paramref name="reader"/>.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to parse and extract an <see cref="T:IEnumerable{JDataResult}"/> sequence from.</param>
/// <returns>An <see cref="T:IEnumerable{JDataResult}"/> sequence from the specified <see cref="JsonReader"/>.</returns>
public static IEnumerable<JDataResult> ReadAll(JsonReader reader)
{
Validator.ThrowIfNull(reader);
Validator.ThrowIf.InvalidJsonDocument(ref reader);
return new JData(reader).Result.Value;
}
internal JData(JsonReader reader)
{
Result = new Lazy<List<JDataResult>>(() =>
{
var result = new List<JDataResult>();
while (reader.Read())
{
var jr = new JDataResult();
switch (reader.TokenType)
{
case JsonToken.PropertyName:
jr.PropertyName = reader.Value.ToString();
reader.Read();
break;
}
if (reader.TokenType == JsonToken.StartArray)
{
return FillArrayHierarchy(reader, jr);
}
if (reader.TokenType == JsonToken.StartObject)
{
return FillObjectHierarchy(reader, jr);
}
jr.Value = reader.Value;
jr.Path = reader.Path.RemoveBrackets();
jr.Type = reader.ValueType;
result.Add(jr);
}
return result;
});
}
private Lazy<List<JDataResult>> Result { get; }
private List<JDataResult> FillArrayHierarchy(JsonReader reader, JDataResult parent)
{
return FillHierarchy(reader, parent, r => r.TokenType == JsonToken.EndArray);
}
private List<JDataResult> FillObjectHierarchy(JsonReader reader, JDataResult parent)
{
return FillHierarchy(reader, parent, r => r.TokenType == JsonToken.EndObject);
}
private List<JDataResult> FillHierarchy(JsonReader reader, JDataResult parent, Func<JsonReader, bool> skipWhenTrue)
{
var result = new List<JDataResult>();
while (reader.Read())
{
if (skipWhenTrue(reader)) { break; }
var jr = new JDataResult { Parent = parent };
switch (reader.TokenType)
{
case JsonToken.PropertyName:
jr.PropertyName = reader.Value.ToString();
reader.Read();
break;
}
if (reader.TokenType == JsonToken.StartArray)
{
jr.Children = FillArrayHierarchy(reader, jr);
}
else if (reader.TokenType == JsonToken.StartObject)
{
jr.Children = FillObjectHierarchy(reader, jr);
}
else
{
jr.Value = reader.Value;
}
jr.Path = reader.Path.RemoveBrackets();
jr.Type = reader.ValueType;
result.Add(jr);
}
return result;
}
}
internal static class RegexExtensions
{
internal static readonly Lazy<Regex> LazySquareBracketsRemover = new(() => new Regex(@"\[[^]]*\]", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
internal static string RemoveBrackets(this string path)
{
return LazySquareBracketsRemover.Value.Replace(path, "");
}
}
}