コード例 #1
0
        /// <summary>Gets the enum default value.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="actualSchema">The actual schema.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <param name="typeResolver">The type resolver.</param>
        /// <returns>The enum default value.</returns>
        protected virtual string GetEnumDefaultValue(JsonSchema schema, JsonSchema actualSchema, string typeNameHint, TypeResolverBase typeResolver)
        {
            var typeName = typeResolver.Resolve(actualSchema, false, typeNameHint);

            var index    = actualSchema.Enumeration.ToList().IndexOf(schema.Default);
            var enumName = index >= 0 && actualSchema.EnumerationNames?.Count > index
                ? actualSchema.EnumerationNames.ElementAt(index)
                : schema.Default.ToString();

            return(typeName.Trim('?') + "." + _settings.EnumNameGenerator.Generate(index, enumName, schema.Default, actualSchema));
        }
コード例 #2
0
        /// <summary>Gets the default value code.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="allowsNull">Specifies whether the default value assignment also allows null.</param>
        /// <param name="targetType">The type of the target.</param>
        /// <param name="typeNameHint">The type name hint to use when generating the type and the type name is missing.</param>
        /// <param name="useSchemaDefault">if set to <c>true</c> uses the default value from the schema if available.</param>
        /// <param name="typeResolver">The type resolver.</param>
        /// <returns>The code.</returns>
        public virtual string GetDefaultValue(JsonSchema schema, bool allowsNull, string targetType, string typeNameHint, bool useSchemaDefault, TypeResolverBase typeResolver)
        {
            if (schema.Default == null || !useSchemaDefault)
            {
                return(null);
            }

            var actualSchema = schema is JsonSchemaProperty ? ((JsonSchemaProperty)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.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);
        }
コード例 #3
0
 /// <summary>Initializes a new instance of the <see cref="ValueGeneratorBase" /> class.</summary>
 /// <param name="typeResolver">The type typeResolver.</param>
 /// <param name="enumNameGenerator">The enum name generator.</param>
 protected ValueGeneratorBase(TypeResolverBase typeResolver, IEnumNameGenerator enumNameGenerator)
 {
     _typeResolver      = typeResolver;
     _enumNameGenerator = enumNameGenerator;
 }
コード例 #4
0
 /// <summary>Initializes a new instance of the <see cref="GeneratorBase"/> class.</summary>
 /// <param name="rootObject">The root object.</param>
 /// <param name="typeResolver">The type resolver.</param>
 /// <param name="settings">The settings.</param>
 protected GeneratorBase(object rootObject, TypeResolverBase typeResolver, CodeGeneratorSettingsBase settings)
 {
     RootObject = rootObject;
     _resolver  = typeResolver;
     _settings  = settings;
 }
コード例 #5
0
        /// <summary>Gets the default value code.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="allowsNull">Specifies whether the default value assignment also allows null.</param>
        /// <param name="targetType">The type of the target.</param>
        /// <param name="typeNameHint">The type name hint to use when generating the type and the type name is missing.</param>
        /// <param name="useSchemaDefault">if set to <c>true</c> uses the default value from the schema if available.</param>
        /// <param name="typeResolver">The type resolver.</param>
        /// <returns>The code.</returns>
        public virtual string GetDefaultValue(JsonSchema4 schema, bool allowsNull, string targetType, string typeNameHint, bool useSchemaDefault, TypeResolverBase typeResolver)
        {
            if (schema.Default == null || !useSchemaDefault)
            {
                return(null);
            }

            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))
            {
                return("\"" + ConversionUtilities.ConvertToStringLiteral(schema.Default.ToString()) + "\"");
            }
            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);
        }