IJsonSchemaValidator.cs
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
namespace UniJSON
{
public class JsonSchemaValidationContext
{
Stack<string> m_stack = new Stack<string>();
public bool EnableDiagnosisForNotRequiredFields = false;
public JsonSchemaValidationContext(object o)
{
Push(o.GetType().Name);
}
public ActionDisposer Push(object o)
{
m_stack.Push(o.ToString());
return new ActionDisposer(Pop);
}
public void Pop()
{
m_stack.Pop();
}
public bool IsEmpty()
{
return m_stack.Count == 1; // A first element will be remained.
}
public override string ToString()
{
return string.Join(".", m_stack.ToArray(), 0, m_stack.Count);
}
}
public class JsonSchemaValidationException : Exception
{
public Exception Error
{
get; private set;
}
public JsonSchemaValidationException(JsonSchemaValidationContext context, string msg) : base(string.Format("[{0}] {1}", context, msg))
{
}
public JsonSchemaValidationException(JsonSchemaValidationContext context, Exception ex) : base(string.Format("[{0}] {1}", context, ex))
{
Error = ex;
}
}
public interface IJsonSchemaValidator
{
#region JsonSchema
void Merge(IJsonSchemaValidator rhs);
/// <summary>
/// Parse json schema
/// </summary>
/// <param name="fs"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool FromJsonSchema(IFileSystemAccessor fs, string key, ListTreeNode<JsonValue> value);
void ToJsonScheama(IFormatter f);
#endregion
#region Serializer
/// <summary>
///
/// </summary>
/// <param name="o"></param>
/// <returns>return null if validate value</returns>
JsonSchemaValidationException Validate<T>(JsonSchemaValidationContext context, T value);
void Serialize<T>(IFormatter f, JsonSchemaValidationContext context, T value);
void Deserialize<T, U>(ListTreeNode<T> src, ref U dst) where T : IListTreeItem, IValue<T>;
#endregion
}
}