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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class MyClass implements IMyClass {
if (_data) {
this.firstName = _data["FirstName"];
this.lastName = _data["lastName"];
(<any>this).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
(this as any).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
this.timeSpan = _data["TimeSpan"];
this.timeSpanOrNull = _data["TimeSpanOrNull"];
this.gender = _data["Gender"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class MyItem implements IMyItem {
init(_data?: any) {
if (_data) {
if (_data["extensions"]) {
(<any>this).extensions = {} as any;
(this as any).extensions = {} as any;
for (let key in _data["extensions"]) {
if (_data["extensions"].hasOwnProperty(key))
((<any>this).extensions as any)![key] = _data["extensions"][key];
((this as any).extensions as any)![key] = _data["extensions"][key];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Person implements IPerson {
if (_data) {
this.firstName = _data["FirstName"];
this.lastName = _data["lastName"];
(<any>this).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
(this as any).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
this.timeSpan = _data["TimeSpan"];
this.timeSpanOrNull = _data["TimeSpanOrNull"];
this.gender = _data["Gender"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Person implements IPerson {
if (_data) {
this.firstName = _data["FirstName"];
this.lastName = _data["lastName"];
(<any>this).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
(this as any).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
this.timeSpan = _data["TimeSpan"];
this.timeSpanOrNull = _data["TimeSpanOrNull"];
this.gender = _data["Gender"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Person implements IPerson {
if (_data) {
this.firstName = _data["FirstName"];
this.lastName = _data["lastName"];
(<any>this).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
(this as any).birthday = _data["Birthday"] ? new Date(_data["Birthday"].toString()) : undefined as any;
this.timeSpan = _data["TimeSpan"];
this.timeSpanOrNull = _data["TimeSpanOrNull"];
this.gender = _data["Gender"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NJsonSchema.CodeGeneration.TypeScript.Models
/// <seealso cref="PropertyModelBase" />
public class PropertyModel : PropertyModelBase
{
private static readonly string _validPropertyNameRegex = "^[a-zA-Z_$][0-9a-zA-Z_$]*$";
private static readonly Lazy<Regex> _validPropertyNameRegex = new(static () => new Regex("^[a-zA-Z_$][0-9a-zA-Z_$]*$", RegexOptions.Compiled, TimeSpan.FromMilliseconds(250)));

private readonly string _parentTypeName;
private readonly TypeScriptGeneratorSettings _settings;
Expand Down Expand Up @@ -44,7 +44,7 @@ public PropertyModel(
}

/// <summary>Gets the name of the property in an interface.</summary>
public string InterfaceName => Regex.IsMatch(_property.Name, _validPropertyNameRegex) ? _property.Name : $"\"{_property.Name}\"";
public string InterfaceName => _validPropertyNameRegex.Value.IsMatch(_property.Name) ? _property.Name : $"\"{_property.Name}\"";

/// <summary>Gets a value indicating whether the property has description.</summary>
public bool HasDescription => !string.IsNullOrEmpty(Description);
Expand Down Expand Up @@ -159,8 +159,9 @@ public string ConvertToClassCode
{
return DataConversionGenerator.RenderConvertToClassCode(new DataConversionParameters
{
Variable = typeStyle == TypeScriptTypeStyle.Class ?
(IsReadOnly ? "(<any>this)." : "this.") + PropertyName : PropertyName + "_",
Variable = typeStyle == TypeScriptTypeStyle.Class
? (IsReadOnly ? "(this as any)." : "this.") + PropertyName
: PropertyName + "_",
Value = "_data[\"" + _property.Name + "\"]",
Schema = _property,
IsPropertyNullable = _property.IsNullable(_settings.SchemaType),
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace NJsonSchema.CodeGeneration
/// <summary>The default enumeration name generator.</summary>
public class DefaultEnumNameGenerator : IEnumNameGenerator
{
private static readonly Regex _invalidNameCharactersPattern = new Regex(@"[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]");
private static readonly Lazy<Regex> _invalidNameCharactersPattern = new(static () => new Regex(@"[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]", RegexOptions.Compiled, TimeSpan.FromMilliseconds(250)));

/// <summary>Generates the enumeration name/key of the given enumeration entry.</summary>
/// <param name="index">The index of the enumeration value (check <see cref="JsonSchema.Enumeration" /> and <see cref="JsonSchema.EnumerationNames" />).</param>
Expand Down Expand Up @@ -62,7 +62,7 @@ public string Generate(int index, string? name, object? value, JsonSchema schema

var cleaned = name.Replace(':', '-').Replace(@"""", "");
var camelCase = ConversionUtilities.ConvertToUpperCamelCase(cleaned, firstCharacterMustBeAlpha: true);
return _invalidNameCharactersPattern.Replace(camelCase, "_");
return _invalidNameCharactersPattern.Value.Replace(camelCase, "_");
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema/ConversionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static string ConvertToUpperCamelCase(string input, bool firstCharacterMu
return input;
}

[MethodImpl((MethodImplOptions) 256)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string Capitalize(string input)
{
if (char.IsUpper(input[0]))
Expand Down
21 changes: 9 additions & 12 deletions src/NJsonSchema/Infrastructure/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@ namespace NJsonSchema
{
internal static class EnumExtensions
{
// for older frameworks
private const MethodImplOptions OptionAggressiveInlining = (MethodImplOptions) 256;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNull(this JsonObjectType type) => (type & JsonObjectType.Null) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNumber(this JsonObjectType type) => (type & JsonObjectType.Number) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsObject(this JsonObjectType type) => (type & JsonObjectType.Object) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsArray(this JsonObjectType type) => (type & JsonObjectType.Array) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInteger(this JsonObjectType type) => (type & JsonObjectType.Integer) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsString(this JsonObjectType type) => (type & JsonObjectType.String) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsBoolean(this JsonObjectType type) => (type & JsonObjectType.Boolean) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFile(this JsonObjectType type) => (type & JsonObjectType.File) != 0;

[MethodImpl(OptionAggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNone(this JsonObjectType type) => type == JsonObjectType.None;
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonSchema.Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public readonly bool Contains(JsonSchema schema)

/// <exception cref="InvalidOperationException">Cyclic references detected.</exception>
/// <exception cref="InvalidOperationException">The schema reference path has not been resolved.</exception>
[MethodImpl((MethodImplOptions) 256)] // aggressive inlining
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private JsonSchema GetActualSchema(ref CheckedSchemaContainer checkedSchemas)
{
static void ThrowInvalidOperationException(string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using Newtonsoft.Json.Linq;
using System.Globalization;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -33,9 +34,7 @@ public class DateTimeFormatValidator : IFormatValidator
"yyyy"
];

private static readonly Regex DateTimeRegexPattern =
new(@"^((?:(\d{4}-\d{2}-\d{2})([Tt_]| )(\d{2}:\d{2}:\d{2}(?:\.\d+)?))([Zz]|[\+-]\d{2}:\d{2}))$",
RegexOptions.Compiled, TimeSpan.FromMilliseconds(250));
private static readonly Lazy<Regex> DateTimeRegexPattern = new(static () => new(@"^((?:(\d{4}-\d{2}-\d{2})([Tt_]| )(\d{2}:\d{2}:\d{2}(?:\.\d+)?))([Zz]|[\+-]\d{2}:\d{2}))$", RegexOptions.Compiled, TimeSpan.FromMilliseconds(250)));

/// <summary>Gets the format attribute's value.</summary>
public string Format { get; } = JsonFormatStrings.DateTime;
Expand All @@ -50,8 +49,8 @@ public class DateTimeFormatValidator : IFormatValidator
public bool IsValid(string value, JTokenType tokenType)
{
return tokenType == JTokenType.Date
|| DateTimeOffset.TryParseExact(value, _acceptableFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset _)
|| DateTimeRegexPattern.IsMatch(value);
|| DateTimeOffset.TryParseExact(value, _acceptableFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out _)
|| DateTimeRegexPattern.Value.IsMatch(value);
}
}
}
}
Loading