-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTEJsonConverter.cs
More file actions
50 lines (41 loc) · 1.66 KB
/
RTEJsonConverter.cs
File metadata and controls
50 lines (41 loc) · 1.66 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
using System;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Contentstack.Utils.Constants;
namespace Contentstack.Utils.Converters
{
public class RTEJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new InvalidOperationException(ErrorMessages.InvalidRteJson);
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
object targetObj = Activator.CreateInstance(objectType);
foreach (PropertyInfo prop in objectType.GetProperties()
.Where(p => p.CanRead && p.CanWrite))
{
JsonPropertyAttribute att = prop.GetCustomAttributes(true)
.OfType<JsonPropertyAttribute>()
.FirstOrDefault();
string jsonPath = (att != null ? att.PropertyName : prop.Name);
JToken token = jo.SelectToken(jsonPath);
if (token != null && token.Type != JTokenType.Null)
{
object value = token.ToObject(prop.PropertyType, serializer);
prop.SetValue(targetObj, value, null);
}
}
return targetObj;
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
}
}
}