Repository navigation
Expand file tree
/
Copy pathJsonPointer.cs
More file actions
50 lines (43 loc) · 1.37 KB
/
Copy pathJsonPointer.cs
File metadata and controls
50 lines (43 loc) · 1.37 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
//---------------------------------------------------------------------
// <copyright file="JsonPointer.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
using System;
using System.Linq;
namespace Microsoft.OpenApi
{
public class JsonPointer
{
private readonly string[] _Tokens;
public string[] Tokens { get { return _Tokens; } }
public JsonPointer(string pointer)
{
_Tokens = pointer.Split('/').Skip(1).Select(Decode).ToArray();
}
private JsonPointer(string[] tokens)
{
_Tokens = tokens;
}
private string Decode(string token)
{
return Uri.UnescapeDataString(token).Replace("~1", "/").Replace("~0", "~");
}
public bool IsNewPointer()
{
return _Tokens.Last() == "-";
}
public JsonPointer ParentPointer
{
get
{
if (_Tokens.Length == 0) return null;
return new JsonPointer(_Tokens.Take(_Tokens.Length - 1).ToArray());
}
}
public override string ToString()
{
return "/" + String.Join("/", _Tokens);
}
}
}