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

Memory mgmt #133

Merged
merged 5 commits into from
Jun 20, 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
3 changes: 2 additions & 1 deletion .github/semver_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"settings": {
"ruleOverrides": { },
"omitDisclaimer": true,
"includeHeader": true
"includeHeader": true,
"assumeChanges": true
},
"nuget": {
"repositoryUrl": "https://api.nuget.org/"
Expand Down
4 changes: 2 additions & 2 deletions Json.More/Json.More.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Json.More.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Version>1.4.2</Version>
<Version>1.4.3</Version>
<Description>Provides extended functionality for the System.Text.Json namespace.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
Expand All @@ -14,7 +14,7 @@
<PackageReleaseNotes>https://gregsdennis.github.io/json-everything/release-notes/json-more.html</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.4.2.0</FileVersion>
<FileVersion>1.4.3.0</FileVersion>
<DocumentationFile>Json.More.xml</DocumentationFile>
<PackageIcon>json-logo-256.png</PackageIcon>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
41 changes: 21 additions & 20 deletions Json.More/JsonElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;

Expand Down Expand Up @@ -145,8 +146,8 @@ public static string ToJsonString(this JsonElement element)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this long value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -157,8 +158,8 @@ public static JsonElement AsJsonElement(this long value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this int value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -169,8 +170,8 @@ public static JsonElement AsJsonElement(this int value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this short value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -181,8 +182,8 @@ public static JsonElement AsJsonElement(this short value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this bool value)
{
var doc = JsonDocument.Parse($"{value.ToString().ToLower()}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value ? "true" : "false");
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -193,8 +194,8 @@ public static JsonElement AsJsonElement(this bool value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this decimal value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -205,8 +206,8 @@ public static JsonElement AsJsonElement(this decimal value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this double value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -217,8 +218,8 @@ public static JsonElement AsJsonElement(this double value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this float value)
{
var doc = JsonDocument.Parse($"{value}");
return doc.RootElement;
using var doc = JsonDocument.Parse(value.ToString(CultureInfo.InvariantCulture));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -229,8 +230,8 @@ public static JsonElement AsJsonElement(this float value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this string? value)
{
var doc = JsonDocument.Parse(JsonSerializer.Serialize(value));
return doc.RootElement;
using var doc = JsonDocument.Parse(JsonSerializer.Serialize(value));
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -241,8 +242,8 @@ public static JsonElement AsJsonElement(this string? value)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this IEnumerable<JsonElement> values)
{
var doc = JsonDocument.Parse($"[{string.Join(",", values.Select(v => v.ToJsonString()))}]");
return doc.RootElement;
using var doc = JsonDocument.Parse($"[{string.Join(",", values.Select(v => v.ToJsonString()))}]");
return doc.RootElement.Clone();
}

/// <summary>
Expand All @@ -253,8 +254,8 @@ public static JsonElement AsJsonElement(this IEnumerable<JsonElement> values)
/// <remarks>This is a workaround for lack of native support in the System.Text.Json namespace.</remarks>
public static JsonElement AsJsonElement(this IDictionary<string, JsonElement> values)
{
var doc = JsonDocument.Parse($"{{{string.Join(",", values.Select(v => $"{JsonSerializer.Serialize(v.Key)}:{v.Value.ToJsonString()}"))}}}");
return doc.RootElement;
using var doc = JsonDocument.Parse($"{{{string.Join(",", values.Select(v => $"{JsonSerializer.Serialize(v.Key)}:{v.Value.ToJsonString()}"))}}}");
return doc.RootElement.Clone();
}
}
}
4 changes: 2 additions & 2 deletions JsonLogic/JsonLogic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<PackageTags>json logic json-logic jsonlogic</PackageTags>
<PackageReleaseNotes>https://gregsdennis.github.io/json-everything/release-notes/json-logic.html</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<FileVersion>1.3.1.0</FileVersion>
<DocumentationFile>JsonLogic.xml</DocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
5 changes: 4 additions & 1 deletion JsonLogic/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public class LogicComponentConverter : JsonConverter<Rule>
public override Rule Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
return new LiteralRule(JsonDocument.ParseValue(ref reader).RootElement);
{
using var doc = JsonDocument.ParseValue(ref reader);
return new LiteralRule(doc.RootElement.Clone());
}

var data = JsonSerializer.Deserialize<Dictionary<string, ArgumentCollection>>(ref reader, options);

Expand Down
10 changes: 9 additions & 1 deletion JsonLogic/RuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ namespace Json.Logic
/// </summary>
public static class RuleExtensions
{
private static readonly JsonElement _nullElement;

static RuleExtensions()
{
using var doc = JsonDocument.Parse("null");
_nullElement = doc.RootElement.Clone();
}

/// <summary>
/// Calls <see cref="Rule.Apply(JsonElement)"/> with no data.
/// </summary>
/// <param name="rule">The rule.</param>
/// <returns>The result.</returns>
public static JsonElement Apply(this Rule rule)
{
return rule.Apply(JsonDocument.Parse("null").RootElement);
return rule.Apply(_nullElement);
}
}
}
3 changes: 2 additions & 1 deletion JsonLogic/Rules/ReduceRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public override JsonElement Apply(JsonElement data)
Current = element,
Accumulator = accumulator
};
var item = JsonDocument.Parse(JsonSerializer.Serialize(intermediary, _options)).RootElement;
using var doc = JsonDocument.Parse(JsonSerializer.Serialize(intermediary, _options));
var item = doc.RootElement.Clone();

accumulator = _rule.Apply(item);
}
Expand Down
3 changes: 2 additions & 1 deletion JsonPatch/EditableJsonElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public EditableJsonElement(JsonElement raw)

public JsonElement ToElement()
{
return JsonDocument.Parse(ToString()).RootElement;
using var doc = JsonDocument.Parse(ToString());
return doc.RootElement.Clone();
}

public override string ToString()
Expand Down
4 changes: 2 additions & 2 deletions JsonPatch/JsonPatch.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.Patch</RootNamespace>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<PackageId>JsonPatch.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Company>Greg Dennis</Company>
Expand All @@ -18,7 +18,7 @@
<PackageIcon>json-logo-256.png</PackageIcon>
<PackageReleaseNotes>https://gregsdennis.github.io/json-everything/release-notes/json-patch.html</PackageReleaseNotes>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<FileVersion>1.0.5.0</FileVersion>
<DocumentationFile>JsonPatch.xml</DocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
4 changes: 2 additions & 2 deletions JsonPath/JsonPath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageIcon>json-logo-256.png</PackageIcon>
<PackageTags>json-path jsonpath query json</PackageTags>
<PackageReleaseNotes>https://gregsdennis.github.io/json-everything/release-notes/json-path.html</PackageReleaseNotes>
<Version>0.1.6</Version>
<FileVersion>0.1.6.0</FileVersion>
<Version>0.1.7</Version>
<FileVersion>0.1.7.0</FileVersion>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>JsonPath.Net.xml</DocumentationFile>
Expand Down
3 changes: 2 additions & 1 deletion JsonPath/PropertyNameIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ internal static bool TryParse(ReadOnlySpan<char> span, ref int i, [NotNullWhen(t
{
if (start == '\'')
key = key.Replace("\\'", "'").Replace("\"", "\\\"");
key = JsonDocument.Parse($"\"{key}\"").RootElement.GetString();
using var doc = JsonDocument.Parse($"\"{key}\"");
key = doc.RootElement.GetString();
}
catch
{
Expand Down
3 changes: 2 additions & 1 deletion JsonPath/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public static bool TryParseJsonElement(this ReadOnlySpan<char> span, ref int i,
var block = span[i..end];
if (block[0] == '\'' && block[^1] == '\'')
block = $"\"{block[1..^1].ToString()}\"".AsSpan();
element = JsonDocument.Parse(block.ToString()).RootElement.Clone();
using var doc = JsonDocument.Parse(block.ToString());
element = doc.RootElement.Clone();
i = end;
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion JsonSchema/ConstKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ internal class ConstKeywordJsonConverter : JsonConverter<ConstKeyword>
{
public override ConstKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var element = JsonDocument.ParseValue(ref reader).RootElement;
using var document = JsonDocument.ParseValue(ref reader);
var element = document.RootElement;

return new ConstKeyword(element);
}
Expand Down
3 changes: 2 additions & 1 deletion JsonSchema/DefaultKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ internal class DefaultKeywordJsonConverter : JsonConverter<DefaultKeyword>
{
public override DefaultKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var element = JsonDocument.ParseValue(ref reader).RootElement;
using var document = JsonDocument.ParseValue(ref reader);
var element = document.RootElement;

return new DefaultKeyword(element);
}
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema/EnumKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal class EnumKeywordJsonConverter : JsonConverter<EnumKeyword>
{
public override EnumKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var document = JsonDocument.ParseValue(ref reader);
using var document = JsonDocument.ParseValue(ref reader);

if (document.RootElement.ValueKind != JsonValueKind.Array)
throw new JsonException("Expected array");
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema/ExamplesKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class ExamplesKeywordJsonConverter : JsonConverter<ExamplesKeyword>
{
public override ExamplesKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var document = JsonDocument.ParseValue(ref reader);
using var document = JsonDocument.ParseValue(ref reader);

if (document.RootElement.ValueKind != JsonValueKind.Array)
throw new JsonException("Expected array");
Expand Down
3 changes: 2 additions & 1 deletion JsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ public override JsonSchema Read(ref Utf8JsonReader reader, Type typeToConvert, J
var keywordType = SchemaKeywordRegistry.GetImplementationType(keyword);
if (keywordType == null)
{
var element = JsonDocument.ParseValue(ref reader).RootElement;
using var document = JsonDocument.ParseValue(ref reader);
var element = document.RootElement;
otherData[keyword] = element.Clone();
break;
}
Expand Down
4 changes: 2 additions & 2 deletions JsonSchema/JsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
<RepositoryUrl>https://github.com/gregsdennis/json-everything</RepositoryUrl>
<PackageTags>json-schema validation schema json</PackageTags>
<Version>1.10.3</Version>
<FileVersion>1.10.3.0</FileVersion>
<Version>1.10.4</Version>
<FileVersion>1.10.4.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AssemblyName>JsonSchema.Net</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema/RequiredKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal class RequiredKeywordJsonConverter : JsonConverter<RequiredKeyword>
{
public override RequiredKeyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var document = JsonDocument.ParseValue(ref reader);
using var document = JsonDocument.ParseValue(ref reader);

if (document.RootElement.ValueKind != JsonValueKind.Array)
throw new JsonException("Expected array");
Expand Down
3 changes: 2 additions & 1 deletion JsonSchema/SchemaKeyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void Validate(ValidationContext context)
}

context.Log(() => "Validating against meta-schema.");
var schemaAsJson = JsonDocument.Parse(JsonSerializer.Serialize(context.LocalSchema)).RootElement;
using var document = JsonDocument.Parse(JsonSerializer.Serialize(context.LocalSchema));
var schemaAsJson = document.RootElement;
var newOptions = ValidationOptions.From(context.Options);
newOptions.ValidateMetaSchema = false;
var results = metaSchema.Validate(schemaAsJson, newOptions);
Expand Down
3 changes: 2 additions & 1 deletion JsonSchema/SchemaKeywordRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ static SchemaKeywordRegistry()
.Select(t => new {Type = t, Keyword = t.GetCustomAttribute<SchemaKeywordAttribute>().Name})
.ToDictionary(k => k.Keyword, k => k.Type));

var nullElement = JsonDocument.Parse("null").RootElement;
using var document = JsonDocument.Parse("null");
var nullElement = document.RootElement;
_nullKeywords = new ConcurrentDictionary<Type, IJsonSchemaKeyword>
{
[typeof(ConstKeyword)] = new ConstKeyword(nullElement),
Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-logic.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.3.1](https://github.com/gregsdennis/json-everything/pull/133)

[#132](https://github.com/gregsdennis/json-everything/pull/132) - Fixed some memory management issues around `JsonDocument` and `JsonElement`. Thanks to [@ddunkin](https://github.com/ddunkin) for finding and fixing these.

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

- Exposed `JsonElementExtensions` so that it can be used in custom rules.
Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-more.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.4.3](https://github.com/gregsdennis/json-everything/pull/133)

[#132](https://github.com/gregsdennis/json-everything/pull/132) - Fixed some memory management issues around `JsonDocument` and `JsonElement`. Thanks to [@ddunkin](https://github.com/ddunkin) for finding and fixing these.

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

Fixes potential race condition in `EnumStringConverter`. Credit to [@jaysvoboda](https://github.com/jaysvoboda) for finding and fixing this.
Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-patch.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.0.6](https://github.com/gregsdennis/json-everything/pull/133)

[#132](https://github.com/gregsdennis/json-everything/pull/132) - Fixed some memory management issues around `JsonDocument` and `JsonElement`. Thanks to [@ddunkin](https://github.com/ddunkin) for finding and fixing these.

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

Added support for nullable reference types.
Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-path.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [0.1.7](https://github.com/gregsdennis/json-everything/pull/133)

[#132](https://github.com/gregsdennis/json-everything/pull/132) - Fixed some memory management issues around `JsonDocument` and `JsonElement`. Thanks to [@ddunkin](https://github.com/ddunkin) for finding and fixing these.

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

Updated expression parsing to handle whitespace better.
Expand Down
2 changes: 1 addition & 1 deletion docs_source/release-notes/json-pointer.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [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.
[#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.

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

Expand Down
4 changes: 4 additions & 0 deletions docs_source/release-notes/json-schema.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.10.4](https://github.com/gregsdennis/json-everything/pull/133)

[#132](https://github.com/gregsdennis/json-everything/pull/132) - Fixed some memory management issues around `JsonDocument` and `JsonElement`. Thanks to [@ddunkin](https://github.com/ddunkin) for finding and fixing these.

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

Added overload for `ExitKeyword` logging extension to pull the validition result from the context rather than having to pass it in.
Expand Down