-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathJsonPointer.cs
More file actions
70 lines (62 loc) · 1.83 KB
/
Copy pathJsonPointer.cs
File metadata and controls
70 lines (62 loc) · 1.83 KB
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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI
{
using System;
using System.Linq;
/// <summary>
/// JSON pointer.
/// </summary>
public class JsonPointer
{
/// <summary>
/// Initializes the <see cref="JsonPointer"/> class.
/// </summary>
/// <param name="pointer">Pointer as string.</param>
public JsonPointer(string pointer)
{
this.Tokens = string.IsNullOrEmpty(pointer) || pointer == "/"
? new string[0]
: pointer.Split('/').Skip(1).Select(this.Decode).ToArray();
}
/// <summary>
/// Initializes the <see cref="JsonPointer"/> class.
/// </summary>
/// <param name="tokens">Pointer as tokenized string.</param>
private JsonPointer(string[] tokens)
{
this.Tokens = tokens;
}
/// <summary>
/// Tokens.
/// </summary>
public string[] Tokens { get; }
/// <summary>
/// Gets the parent pointer.
/// </summary>
public JsonPointer ParentPointer
{
get
{
if (this.Tokens.Length == 0)
{
return null;
}
return new JsonPointer(this.Tokens.Take(this.Tokens.Length - 1).ToArray());
}
}
/// <summary>
/// Decode the string.
/// </summary>
private string Decode(string token)
{
return Uri.UnescapeDataString(token).Replace("~1", "/").Replace("~0", "~");
}
/// <summary>
/// Gets the string representation of this JSON pointer.
/// </summary>
public override string ToString()
{
return "/" + string.Join("/", this.Tokens);
}
}
}