Skip to content

Commit

Permalink
common json pointers should be thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed May 13, 2024
1 parent 54362ee commit 6528972
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions JsonSchema/CommonJsonPointers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using Json.Pointer;

namespace Json.Schema;
Expand All @@ -14,21 +14,16 @@ public static class CommonJsonPointers
/// <summary>
/// Defines an array containing only a single empty JSON Pointer.
/// </summary>
public static readonly JsonPointer[] SingleEmptyPointerArray = { JsonPointer.Empty };
public static readonly JsonPointer[] SingleEmptyPointerArray = [JsonPointer.Empty];

private static readonly Dictionary<int, JsonPointer> _numberSegments = new();
private static readonly ConcurrentDictionary<int, JsonPointer> _numberSegments = new();

/// <summary>
/// A set of predefined single-segment JSON Pointers that contain numeric indices.
/// </summary>
public static JsonPointer GetNumberSegment(int i)
{
if (!_numberSegments.TryGetValue(i, out var pointer))
{
_numberSegments[i] = pointer = JsonPointer.Create(i);
}

return pointer;
return _numberSegments.GetOrAdd(i, x => JsonPointer.Create(x));
}

}

0 comments on commit 6528972

Please sign in to comment.