ValidationsJsonConverter.cs
5.24 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Contentful.Core.Extensions;
using Contentful.Core.Models;
using Contentful.Core.Models.Management;
using Contentful.Core.Search;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Contentful.Core.Configuration
{
/// <summary>
/// JsonConverter for converting <see cref="Contentful.Core.Models.Management.IFieldValidator"/>.
/// </summary>
public class ValidationsJsonConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">The type to convert to.</param>
public override bool CanConvert(Type objectType)
{
return objectType is IFieldValidator;
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The reader to use.</param>
/// <param name="objectType">The object type to serialize into.</param>
/// <param name="existingValue">The current value of the property.</param>
/// <param name="serializer">The serializer to use.</param>
/// <returns>The deserialized <see cref="Asset"/>.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
JToken jToken = null;
if (jsonObject.TryGetValue("size", out jToken))
{
return new SizeValidator(
jToken["min"].ToNullableInt(),
jToken["max"].ToNullableInt(),
jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("range", out jToken))
{
return new RangeValidator(
jToken["min"].ToNullableInt(),
jToken["max"].ToNullableInt(),
jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("in", out jToken))
{
return new InValuesValidator(jToken.Values<string>(), jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("linkMimetypeGroup", out jToken))
{
if(jToken is JValue)
{
//single string value returned for mime type field. This seems to be an inconsistency in the API that needs to be handled.
var type = jToken.Value<string>();
return new MimeTypeValidator(new[] { (MimeTypeRestriction)Enum.Parse(typeof(MimeTypeRestriction), type, true) },
jsonObject["message"]?.ToString());
}
var types = jToken.Values<string>();
return new MimeTypeValidator(types.Select(c => (MimeTypeRestriction)Enum.Parse(typeof(MimeTypeRestriction), c, true)),
jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("linkContentType", out jToken))
{
return new LinkContentTypeValidator(jToken.Values<string>(), jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("regexp", out jToken))
{
return new RegexValidator(jToken["pattern"]?.ToString(), jToken["flags"]?.ToString(), jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("unique", out jToken))
{
return new UniqueValidator();
}
if (jsonObject.TryGetValue("dateRange", out jToken))
{
return new DateRangeValidator(
jToken["min"]?.ToString(),
jToken["max"]?.ToString(),
jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("assetFileSize", out jToken))
{
return new FileSizeValidator(
jToken["min"].ToNullableInt(),
jToken["max"].ToNullableInt(),
SystemFileSizeUnits.Bytes,
SystemFileSizeUnits.Bytes,
jsonObject["message"]?.ToString());
}
if (jsonObject.TryGetValue("assetImageDimensions", out jToken))
{
int? minWidth = null;
int? maxWidth = null;
int? minHeight = null;
int? maxHeight = null;
if (jToken["width"] != null)
{
var width = jToken["width"];
minWidth = width["min"].ToNullableInt();
maxWidth = width["max"].ToNullableInt();
}
if (jToken["height"] != null)
{
var height = jToken["height"];
minHeight = height["min"].ToNullableInt();
maxHeight = height["max"].ToNullableInt();
}
return new ImageSizeValidator(minWidth, maxWidth, minHeight, maxHeight, jsonObject["message"]?.ToString());
}
return Activator.CreateInstance(objectType);
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, (value as IFieldValidator).CreateValidator());
}
}
}