Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implicit casts from int and string to pointer segment #162

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions JsonPointer.Tests/JsonPointerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ public void GettingLastItemInArray()
// ReSharper disable once PossibleInvalidOperationException
Assert.AreEqual(JsonValueKind.Null, actual.Value.ValueKind);
}

[Test]
public void ImplicitCastTest()
{
var pointer = JsonPointer.Create("string", 1, "foo");

Assert.AreEqual("/string/1/foo", pointer.ToString());
}
}
}
15 changes: 15 additions & 0 deletions JsonPointer/JsonPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ public static bool TryParse(string source, out JsonPointer pointer, JsonPointerK
return true;
}

/// <summary>
/// Creates a new JSON Pointer from a collection of segments.
/// </summary>
/// <param name="segments">A collection of segments.</param>
/// <returns>The JSON Pointer.</returns>
/// <remarks>This method creates un-encoded pointers only.</remarks>
public static JsonPointer Create(params PointerSegment[] segments)
{
return new JsonPointer
{
_segments = segments.ToArray(),
Kind = JsonPointerKind.Plain
};
}

/// <summary>
/// Creates a new JSON Pointer from a collection of segments.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions JsonPointer/JsonPointer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>Json.Pointer</RootNamespace>
<Version>1.3.3</Version>
<Version>1.4.0</Version>
<PackageId>JsonPointer.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Product>JsonPointer.Net</Product>
Expand All @@ -18,7 +18,7 @@
<PackageIcon>json-logo-256.png</PackageIcon>
<PackageReleaseNotes>https://gregsdennis.github.io/json-everything/release-notes/json-pointer.html</PackageReleaseNotes>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.3.3.0</FileVersion>
<FileVersion>1.4.0.0</FileVersion>
<DocumentationFile>JsonPointer.Net.xml</DocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
20 changes: 20 additions & 0 deletions JsonPointer/PointerSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,25 @@ public override int GetHashCode()
{
return !left.Equals(right);
}

/// <summary>
/// Implicitly casts an <see cref="int"/> to a <see cref="PointerSegment"/>.
/// </summary>
/// <param name="value">A pointer segment that represents the value.</param>
/// <remarks>No URI encoding is performed for implicit casts.</remarks>
public static implicit operator PointerSegment(uint value)
{
return Create(value.ToString());
}

/// <summary>
/// Implicitly casts a <see cref="string"/> to a <see cref="PointerSegment"/>.
/// </summary>
/// <param name="value">A pointer segment that represents the value.</param>
/// <remarks>No URI encoding is performed for implicit casts.</remarks>
public static implicit operator PointerSegment(string value)
{
return Create(value);
}
}
}
7 changes: 6 additions & 1 deletion docs_source/release-notes/json-pointer.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# [1.3.3](https://github.com/gregsdennis/json-everything/pull/162)

- Added implicit casts from `int` and `string` to `PointerSegment`
- Added params overload for `JsonPointer.Create()`.

# [1.3.3](https://github.com/gregsdennis/json-everything/pull/130)

[#123](https://github.com/gregsdennis/json-everything/pull/123) - Removed a copy/paste error that shows up while deserializing relative pointers. Thanks to [@bastiaantenklooster](https://github.com/bastiaantenklooster) for finding this and creating a PR to fix it.
Expand All @@ -10,7 +15,7 @@ Added support for nullable reference types.

Signed the DLL for strong name compatibility.

# [1.3.0](https://github.com/gregsdennis/json-everything/pull/???)
# [1.3.0](https://github.com/gregsdennis/json-everything/pull/58)

Added support pointer creation using lambda expressions, e.g. `x => x.foo[5].bar` to create `/foo/5/bar`.

Expand Down