コード例 #1
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 override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, string targetType, string typeNameHint, bool useSchemaDefault, TypeResolverBase typeResolver)
        {
            var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver);

            if (value == null)
            {
                var isOptional = (schema as JsonProperty)?.IsRequired == false;
                if (schema != null && allowsNull == false && isOptional == false)
                {
                    if (typeResolver.GeneratesType(schema) &&
                        !schema.ActualTypeSchema.IsEnumeration &&
                        !schema.ActualTypeSchema.IsAbstract)
                    {
                        return("new " + targetType + "()");
                    }

                    if (schema.ActualTypeSchema.IsArray)
                    {
                        return("[]");
                    }

                    if (schema.ActualTypeSchema.IsDictionary)
                    {
                        return("{}");
                    }
                }
            }

            return(value);
        }
コード例 #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 override string GetDefaultValue(JsonSchema schema, bool allowsNull, string targetType, string typeNameHint, bool useSchemaDefault, TypeResolverBase typeResolver)
        {
            var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver);

            if (value == null)
            {
                if (schema.Default != null && useSchemaDefault)
                {
                    if (schema.Type.HasFlag(JsonObjectType.String) &&
                        _supportedFormatStrings.Contains(schema.Format))
                    {
                        return(GetDefaultAsStringLiteral(schema));
                    }
                }

                var isOptional = (schema as JsonSchemaProperty)?.IsRequired == false;
                if (schema != null && allowsNull == false && isOptional == false)
                {
                    if (typeResolver.GeneratesType(schema) &&
                        !schema.ActualTypeSchema.IsEnumeration &&
                        !schema.ActualTypeSchema.IsAbstract)
                    {
                        return("new " + targetType + "()");
                    }

                    if (schema.ActualTypeSchema.IsArray)
                    {
                        return("[]");
                    }

                    if (schema.ActualTypeSchema.IsDictionary)
                    {
                        return("{}");
                    }
                }
            }

            return(value);
        }