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(); }
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(); }
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")); } Assert.True(c.IsEmpty()); }
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); }
public static IJsonSchemaValidator Create(object[] values, EnumSerializationType type) { foreach (var x in values) { if (x is string) { return(JsonStringEnumValidator.Create(values.Select(y => (string)y), type)); } if (x is int) { return(JsonIntEnumValidator.Create(values.Select(y => (int)y))); } } throw new NotImplementedException(); }
public static IJsonSchemaValidator Create(Type t, EnumSerializationType serializationType, object[] excludes) { switch (serializationType) { case EnumSerializationType.AsInt: return(JsonIntEnumValidator.Create(GetIntValues(t, excludes))); case EnumSerializationType.AsString: return(JsonStringEnumValidator.Create(GetStringValues(t, excludes, x => x), serializationType)); case EnumSerializationType.AsLowerString: return(JsonStringEnumValidator.Create(GetStringValues(t, excludes, x => x.ToLower()), serializationType)); case EnumSerializationType.AsUpperString: return(JsonStringEnumValidator.Create(GetStringValues(t, excludes, x => x.ToUpper()), serializationType)); default: throw new NotImplementedException(); } }