Ejemplo n.º 1
0
        public static IJsonSchemaValidator Create(JsonNode value)
        {
            foreach (var x in value.ArrayItems)
            {
                switch (x.Value.ValueType)
                {
                case JsonValueType.Integer:
                case JsonValueType.Number:
                    return(JsonIntEnumValidator.Create(value.ArrayItems
                                                       .Where(y => y.Value.ValueType == JsonValueType.Integer || y.Value.ValueType == JsonValueType.Number)
                                                       .Select(y => y.GetInt32())
                                                       ));

                case JsonValueType.String:
                    return(JsonStringEnumValidator.Create(value.ArrayItems
                                                          .Where(y => y.Value.ValueType == JsonValueType.String)
                                                          .Select(y => y.GetString())
                                                          ));

                default:
                    break;
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 2
0
        public static IJsonSchemaValidator Create(IEnumerable <JsonSchema> composition, EnumSerializationType type)
        {
            foreach (var x in composition)
            {
                if (x.Validator is JsonStringEnumValidator)
                {
                    return(JsonStringEnumValidator.Create(composition
                                                          .Select(y => y.Validator as JsonStringEnumValidator)
                                                          .Where(y => y != null)
                                                          .SelectMany(y => y.Values),
                                                          type
                                                          ));
                }
                if (x.Validator is JsonIntEnumValidator)
                {
                    return(JsonIntEnumValidator.Create(composition
                                                       .Select(y => y.Validator as JsonIntEnumValidator)
                                                       .Where(y => y != null)
                                                       .SelectMany(y => y.Values)
                                                       ));
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 3
0
        public static IJsonSchemaValidator Create(ListTreeNode <JsonValue> value)
        {
            foreach (var x in value.ArrayItems())
            {
                if (x.IsInteger() || x.IsFloat())
                {
                    return(JsonIntEnumValidator.Create(value.ArrayItems()
                                                       .Where(y => y.IsInteger() || y.IsFloat())
                                                       .Select(y => y.GetInt32())
                                                       ));
                }
                else if (x.IsString())
                {
                    return(JsonStringEnumValidator.Create(value.ArrayItems()
                                                          .Where(y => y.IsString())
                                                          .Select(y => y.GetString())
                                                          , EnumSerializationType.AsString
                                                          ));
                }
                else
                {
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 4
0
        public void StringEnumValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = JsonStringEnumValidator.Create(new string[] { "a", "b" }, EnumSerializationType.AsString);
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, "c"));
            }
        }
        public void StringEnumValidator()
        {
            var c = new JsonSchemaValidationContext("test");

            {
                var v = new JsonStringEnumValidator();
                v.Values = new string[] { "a", "b" };
                Assert.Null(v.Validate(c, "a"));
                Assert.NotNull(v.Validate(c, "c"));
            }
        }
Ejemplo n.º 6
0
        public static IJsonSchemaValidator Create(Type t, EnumSerializationType serializationType, object[] excludes)
        {
            switch (serializationType)
            {
            case EnumSerializationType.AsLowerString:
                return(JsonStringEnumValidator.Create(GetStringValues(t, excludes, x => x.ToLower())));

            case EnumSerializationType.AsInt:
                return(JsonIntEnumValidator.Create(GetIntValues(t, excludes)));

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 7
0
        public static IJsonSchemaValidator Create(object[] values)
        {
            foreach (var x in values)
            {
                if (x is string)
                {
                    return(JsonStringEnumValidator.Create(values.Select(y => (string)y)));
                }
                if (x is int)
                {
                    return(JsonIntEnumValidator.Create(values.Select(y => (int)y)));
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 8
0
            public static void Serialize(JsonStringEnumValidator validator,
                                         IFormatter f, JsonSchemaValidationContext c, T o)
            {
                if (s_serializer == null)
                {
                    var t = typeof(T);
                    if (t.IsEnum)
                    {
                        s_serializer = (vv, ff, cc, oo) =>
                        {
                            var value = Enum.GetName(t, oo);
                            if (vv.SerializationType == EnumSerializationType.AsLowerString)
                            {
                                value = value.ToLower();
                            }
                            else if (vv.SerializationType == EnumSerializationType.AsUpperString)
                            {
                                value = value.ToUpper();
                            }
                            ff.Value(value);
                        };
                    }
                    else if (t == typeof(string))
                    {
                        s_serializer = (vv, ff, cc, oo) =>
                        {
                            var value = GenericCast <T, string> .Cast(oo);

                            if (vv.SerializationType == EnumSerializationType.AsLowerString)
                            {
                                value = value.ToLower();
                            }
                            else if (vv.SerializationType == EnumSerializationType.AsUpperString)
                            {
                                value = value.ToUpper();
                            }
                            ff.Value(value);
                        };
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                s_serializer(validator, f, c, o);
            }