diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index aea22d96f..f3704b63d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.20 + 9.13.21 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj index ae01cf0dd..8eee1df11 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.20 + 9.13.21 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs index c623d5157..6d10dc768 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs @@ -13,7 +13,7 @@ namespace NJsonSchema.CodeGeneration.TypeScript /// Converts the default value to a TypeScript identifier. public class TypeScriptValueGenerator : ValueGeneratorBase { - private readonly List _formatCompatibleWithString = new List() + private readonly List _supportedFormatStrings = new List() { JsonFormatStrings.Uri, JsonFormatStrings.Guid, @@ -42,9 +42,9 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver); if (value == null) { - if (schema.Type.HasFlag(JsonObjectType.String) && _formatCompatibleWithString.Contains(schema.Format)) + if (schema.Type.HasFlag(JsonObjectType.String) && _supportedFormatStrings.Contains(schema.Format)) { - return "\"" + ConversionUtilities.ConvertToStringLiteral(value.ToString()) + "\""; + return GetDefaultAsStringLiteral(schema); } var isOptional = (schema as JsonProperty)?.IsRequired == false; diff --git a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj index 6407d29b5..5264def09 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.20 + 9.13.21 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs b/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs index 52ddd4eaf..3ef2475e6 100644 --- a/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs +++ b/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs @@ -17,7 +17,7 @@ namespace NJsonSchema.CodeGeneration public abstract class ValueGeneratorBase { private readonly CodeGeneratorSettingsBase _settings; - private readonly List _formatNotCompatibleWithString = new List() + private readonly List _unsupportedFormatStrings = new List() { JsonFormatStrings.Date, JsonFormatStrings.DateTime, @@ -54,28 +54,30 @@ public virtual string GetDefaultValue(JsonSchema4 schema, bool allowsNull, strin var actualSchema = schema is JsonProperty ? ((JsonProperty)schema).ActualTypeSchema : schema.ActualSchema; if (actualSchema.IsEnumeration && !actualSchema.Type.HasFlag(JsonObjectType.Object) && actualSchema.Type != JsonObjectType.None) + { return GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver); + } + + if (schema.Type.HasFlag(JsonObjectType.String) && _unsupportedFormatStrings.Contains(schema.Format) == false) + { + return GetDefaultAsStringLiteral(schema); + } + // TODO: Add conversion for format string, e.g. in C# DateTime.Parse() - if (schema.Type.HasFlag(JsonObjectType.String)) - return GetStringValue(schema.Type, schema.Default, schema.Format); - if (schema.Type.HasFlag(JsonObjectType.Boolean)) + { return schema.Default.ToString().ToLowerInvariant(); + } + if (schema.Type.HasFlag(JsonObjectType.Integer) || schema.Type.HasFlag(JsonObjectType.Number)) + { return GetNumericValue(schema.Type, schema.Default, schema.Format); + } return null; } - private string GetStringValue(JsonObjectType type, object value, string format) - { - if(!_formatNotCompatibleWithString.Contains(format)) - return "\"" + ConversionUtilities.ConvertToStringLiteral(value.ToString()) + "\""; - else - return null; - } - /// Converts the default value to a number literal. /// The JSON type. /// The value to convert. @@ -101,6 +103,14 @@ protected virtual string GetEnumDefaultValue(JsonSchema4 schema, JsonSchema4 act return typeName + "." + _settings.EnumNameGenerator.Generate(index, enumName, schema.Default, actualSchema); } + /// Gets the default value as string literal. + /// The schema. + /// The string literal. + protected string GetDefaultAsStringLiteral(JsonSchema4 schema) + { + return "\"" + ConversionUtilities.ConvertToStringLiteral(schema.Default.ToString()) + "\""; + } + /// Converts a number to its string representation. /// The value. /// The string. diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index f939d44ae..5e0cfa1af 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net45 JSON Schema reader, generator and validator for .NET - 9.13.20 + 9.13.21 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index 00b7fef3c..8b5ed074a 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,7 +2,7 @@ netstandard1.0;netstandard2.0;net40;net45 JSON Schema reader, generator and validator for .NET - 9.13.20 + 9.13.21 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md