// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI
{
using System;
using System.Linq;
///
/// JSON pointer.
///
public class JsonPointer
{
///
/// Initializes the class.
///
/// Pointer as string.
public JsonPointer(string pointer)
{
this.Tokens = string.IsNullOrEmpty(pointer) || pointer == "/"
? new string[0]
: pointer.Split('/').Skip(1).Select(this.Decode).ToArray();
}
///
/// Initializes the class.
///
/// Pointer as tokenized string.
private JsonPointer(string[] tokens)
{
this.Tokens = tokens;
}
///
/// Tokens.
///
public string[] Tokens { get; }
///
/// Gets the parent pointer.
///
public JsonPointer ParentPointer
{
get
{
if (this.Tokens.Length == 0)
{
return null;
}
return new JsonPointer(this.Tokens.Take(this.Tokens.Length - 1).ToArray());
}
}
///
/// Decode the string.
///
private string Decode(string token)
{
return Uri.UnescapeDataString(token).Replace("~1", "/").Replace("~0", "~");
}
///
/// Gets the string representation of this JSON pointer.
///
public override string ToString()
{
return "/" + string.Join("/", this.Tokens);
}
}
}