Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,12 @@
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!--
[IDE0008] Use explicit type instead of 'var'
[IDE0021] Use block body for constructor
[IDE0022] Use block body for method
[IDE0029] Null check can be simplified
[IDE0032] Use auto property
[IDE0039] Use local function
[IDE0045] 'if' statement can be simplified
[IDE0046] 'if' statement can be simplified
[IDE0055] Fix formatting
[IDE0057] Substring can be simplified
[IDE0059] Unnecessary assignment of a value
[IDE0060] Remove unused parameter
[IDE0090] 'new' expression can be simplified
[IDE0130] Namespace does not match folder structure
[IDE0160] Convert to block scoped namespace
[IDE0290] Use primary constructor
[CA1200] Avoid using cref tags with a prefix
[CA1510] Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance
[CA1716] rename parameter property so that it no longer conflicts with the reserved language keyword
[CA1720] Identifier 'xxx' contains type name
[CA1870] Use a cached 'SearchValues' instance for improved searching performance
[CA2263] Prefer the generic overload 'System.Enum.GetValues<TEnum>()'
-->
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1510;CA1716;CA1720</NoWarn>
</PropertyGroup>

</Project>
14 changes: 11 additions & 3 deletions src/NJsonSchema/DefaultTypeNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System.Buffers;
using System.Text;
using System.Text.RegularExpressions;

Expand All @@ -14,7 +15,13 @@ namespace NJsonSchema
/// <summary>Converts the last part of the full type name to upper case.</summary>
public class DefaultTypeNameGenerator : ITypeNameGenerator
{
private static readonly char[] TypeNameHintCleanupChars = ['[', ']', '<', '>', ',', ' '];
private static readonly char[] _typeNameHintCleanupChars = ['[', ']', '<', '>', ',', ' '];

#if NET8_0_OR_GREATER
private static readonly SearchValues<char> TypeNameHintCleanupChars = SearchValues.Create(_typeNameHintCleanupChars);
#else
private static readonly char[] TypeNameHintCleanupChars = _typeNameHintCleanupChars;
#endif

private readonly Dictionary<string, string> _typeNameMappings = [];
private string[] _reservedTypeNames = ["object"];
Expand All @@ -36,13 +43,14 @@ public virtual string Generate(JsonSchema schema, string? typeNameHint, IEnumera
{
if (string.IsNullOrEmpty(typeNameHint) && !string.IsNullOrEmpty(schema.DocumentPath))
{
typeNameHint = schema.DocumentPath!.Replace("\\", "/").Split('/').Last();
var parts = schema.DocumentPath!.Replace("\\", "/").Split('/');
typeNameHint = parts[^1];
}

typeNameHint ??= "";

// check with one pass before doing iterations
var requiresCleanup = !string.IsNullOrEmpty(typeNameHint) && typeNameHint.IndexOfAny(TypeNameHintCleanupChars) != -1;
var requiresCleanup = typeNameHint.AsSpan().IndexOfAny(TypeNameHintCleanupChars) != -1;

if (requiresCleanup)
{
Expand Down
14 changes: 9 additions & 5 deletions src/NJsonSchema/JsonSchema.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ namespace NJsonSchema
[JsonConverter(typeof(ExtensionDataDeserializationConverter))]
public partial class JsonSchema : IJsonExtensionObject
{
private static readonly JsonObjectType[] _jsonObjectTypeValues = Enum.GetValues(typeof(JsonObjectType))
.OfType<JsonObjectType>()
.Where(v => v != JsonObjectType.None)
.ToArray();
internal static readonly List<JsonObjectType> JsonObjectTypes =
#if NET8_0_OR_GREATER
Enum.GetValues<JsonObjectType>()
#else
Enum.GetValues(typeof(JsonObjectType)).Cast<JsonObjectType>()
#endif
.Where(static v => v != JsonObjectType.None)
.ToList();


// keep a reference so we don't need to create a delegate each time
Expand Down Expand Up @@ -331,7 +335,7 @@ private void ResetTypeRaw()
{
_typeRaw = new Lazy<object?>(() =>
{
var flags = _jsonObjectTypeValues
var flags = JsonObjectTypes
.Where(v => Type.HasFlag(v))
.ToArray();

Expand Down
8 changes: 1 addition & 7 deletions src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,9 @@ private void ValidateType(JToken token, JsonSchema schema, SchemaType schemaType
}
}

private static readonly IEnumerable<JsonObjectType> JsonObjectTypes = Enum
.GetValues(typeof(JsonObjectType))
.Cast<JsonObjectType>()
.Where(t => t != JsonObjectType.None)
.ToList();

private static IEnumerable<JsonObjectType> GetTypes(JsonSchema schema)
{
return JsonObjectTypes.Where(t => schema.Type.HasFlag(t));
return JsonSchema.JsonObjectTypes.Where(t => schema.Type.HasFlag(t));
}

private void ValidateAnyOf(JToken token, JsonSchema schema, string? propertyName, string propertyPath, List<ValidationError> errors)
Expand Down
Loading