-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JsonReaderExtensions.cs
56 lines (51 loc) · 1.85 KB
/
JsonReaderExtensions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using Newtonsoft.Json;
namespace Microsoft.CodeAnalysis.Razor.Serialization
{
internal static class JsonReaderExtensions
{
public static bool ReadTokenAndAdvance(this JsonReader reader, JsonToken expectedTokenType, out object value)
{
value = reader.Value;
return reader.TokenType == expectedTokenType && reader.Read();
}
public static void ReadProperties(this JsonReader reader, Action<string> onProperty)
{
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
var propertyName = reader.Value.ToString();
onProperty(propertyName);
break;
case JsonToken.EndObject:
return;
}
}
}
public static string ReadNextStringProperty(this JsonReader reader, string propertyName)
{
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
Debug.Assert(reader.Value.ToString() == propertyName);
if (reader.Read())
{
var value = (string)reader.Value;
return value;
}
else
{
return null;
}
}
}
throw new JsonSerializationException($"Could not find string property '{propertyName}'.");
}
}
}