JsonSchema supports ExplicitIgnorableItemLength to ignore length of Array/List. Can pass JsonSchemaValidationContext to Serializer optionally

This commit is contained in:
yutopp
2019-01-09 22:52:56 +09:00
parent 12519f26e3
commit eef02f7665
2 changed files with 21 additions and 4 deletions

View File

@@ -58,6 +58,7 @@ namespace UniJSON
public bool SkipComparison { get; set; }
public object ExplicitIgnorableValue { private get; set; }
public int ExplicitIgnorableItemLength { private get; set; }
public override string ToString()
{
@@ -179,6 +180,7 @@ namespace UniJSON
Validator = validator,
SkipComparison = skipComparison,
ExplicitIgnorableValue = a.ExplicitIgnorableValue,
ExplicitIgnorableItemLength = a.ExplicitIgnorableItemLength,
};
return schema;
@@ -386,9 +388,12 @@ namespace UniJSON
}
#endregion
public void Serialize<T>(IFormatter f, T o)
public void Serialize<T>(IFormatter f, T o, JsonSchemaValidationContext c = null)
{
var c = new JsonSchemaValidationContext(o);
if (c == null)
{
c = new JsonSchemaValidationContext(o);
}
var ex = Validator.Validate(c, o);
if (ex != null)
@@ -415,16 +420,22 @@ namespace UniJSON
return ExplicitIgnorableValue == null;
}
var iter = obj as System.Collections.ICollection;
if (ExplicitIgnorableItemLength != -1 && iter != null)
{
return iter.Count == ExplicitIgnorableItemLength;
}
return obj.Equals(ExplicitIgnorableValue);
}
}
public static class JsonSchemaExtensions
{
public static string Serialize<T>(this JsonSchema s, T o)
public static string Serialize<T>(this JsonSchema s, T o, JsonSchemaValidationContext c = null)
{
var f = new JsonFormatter();
s.Serialize(f, o);
s.Serialize(f, o, c);
return f.ToString();
}
}

View File

@@ -61,6 +61,12 @@ namespace UniJSON
/// </summary>
public object ExplicitIgnorableValue;
/// <summary>
/// Suppress errors if length of a value of the field which is not required by a schema is matched to this value.
/// This feature will be useful to ignore invalid value which is known.
/// </summary>
public int ExplicitIgnorableItemLength = -1;
public void Merge(BaseJsonSchemaAttribute rhs)
{
if (rhs == null) return;