Example #1
0
        public static JsonSchema FromType(Type t,
                                          BaseJsonSchemaAttribute a  = null, // field attribute
                                          ItemJsonSchemaAttribute ia = null
                                          )
        {
            // class attribute
            var aa = t.GetCustomAttributes(typeof(JsonSchemaAttribute), true)
                     .FirstOrDefault() as JsonSchemaAttribute;

            if (a != null)
            {
                a.Merge(aa);
            }
            else
            {
                if (aa == null)
                {
                    a = new JsonSchemaAttribute();
                }
                else
                {
                    a = aa;
                }
            }

            if (ia == null)
            {
                ia = t.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true)
                     .FirstOrDefault() as ItemJsonSchemaAttribute;
            }

            IJsonSchemaValidator validator = null;
            bool skipComparison            = a.SkipSchemaComparison;

            if (t == typeof(object))
            {
                skipComparison = true;
            }

            if (a.EnumValues != null)
            {
                validator = JsonEnumValidator.Create(a.EnumValues);
            }
            else if (t.IsEnum)
            {
                validator = JsonEnumValidator.Create(t, a.EnumSerializationType, a.EnumExcludes);
            }
            else
            {
                validator = JsonSchemaValidatorFactory.Create(t, a, ia);
            }

            var schema = new JsonSchema
            {
                Title          = a.Title,
                Description    = a.Description,
                Validator      = validator,
                SkipComparison = skipComparison
            };

            return(schema);
        }
Example #2
0
        public static JsonSchema FromType(Type t,
                                          BaseJsonSchemaAttribute a  = null, // field attribute
                                          ItemJsonSchemaAttribute ia = null
                                          )
        {
            // class attribute
            var aa = t.GetCustomAttributes(typeof(JsonSchemaAttribute), true)
                     .FirstOrDefault() as JsonSchemaAttribute;

            if (a != null)
            {
                a.Merge(aa);
            }
            else
            {
                if (aa == null)
                {
                    a = new JsonSchemaAttribute();
                }
                else
                {
                    a = aa;
                }
            }

            if (ia == null)
            {
                ia = t.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true)
                     .FirstOrDefault() as ItemJsonSchemaAttribute;
            }

            IJsonSchemaValidator validator = null;
            bool skipComparison            = a.SkipSchemaComparison;

            if (t == typeof(object))
            {
                skipComparison = true;
            }

            if (a.EnumValues != null)
            {
                try
                {
                    validator = JsonEnumValidator.Create(a.EnumValues, a.EnumSerializationType);
                }
                catch (Exception)
                {
                    throw new Exception(String.Join(", ", a.EnumValues.Select(x => x.ToString()).ToArray()));
                }
            }
            else if (t.IsEnum)
            {
                validator = JsonEnumValidator.Create(t, a.EnumSerializationType, a.EnumExcludes);
            }
            else
            {
                validator = JsonSchemaValidatorFactory.Create(t, a, ia);
            }

            var schema = new JsonSchema
            {
                Title                       = a.Title,
                Description                 = a.Description,
                Validator                   = validator,
                SkipComparison              = skipComparison,
                ExplicitIgnorableValue      = a.ExplicitIgnorableValue,
                ExplicitIgnorableItemLength = a.ExplicitIgnorableItemLength,
            };

            return(schema);
        }