-
Notifications
You must be signed in to change notification settings - Fork 6
/
JSONPatchWithServiceStackExtensions.cs
96 lines (90 loc) · 4.39 KB
/
JSONPatchWithServiceStackExtensions.cs
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
using JSONPatchWithServiceStack.Operations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
namespace JSONPatchWithServiceStack.Extensions
{
public static class JSONPatchWithServiceStackExtensions
{
public static void populateFromJsonPatch(this object updateObject, JsonPatchRequest jsonPatchRequest)
{
// read from request dto properties
PropertyInfo[] properties = updateObject.GetType().GetProperties();
foreach (var op in jsonPatchRequest)
{
string fieldName = op.path.Replace("/", "").ToLower(); // assume leading /slash only for example
string valueString = op.value;
// patch field is in type
if (properties.ToList().Where(x => x.Name.ToLower() == fieldName).Count() > 0)
{
// case-insensitive match by propertyname
var persistentProperty = properties.ToList().Where(x => x.Name.ToLower() == fieldName).First();
// update property on persistent object
// i'm sure this can be improved, but you get the idea...
if (persistentProperty.PropertyType == typeof(string))
{
persistentProperty.SetValue(updateObject, valueString, null);
}
else if (persistentProperty.PropertyType == typeof(int) || persistentProperty.PropertyType == typeof(int?))
{
int valInt = 0;
if (valueString == null)
{
// set to default value
persistentProperty.SetValue(updateObject, null, null);
}
else if (Int32.TryParse(valueString, out valInt))
{
persistentProperty.SetValue(updateObject, valInt, null);
}
}
else if (persistentProperty.PropertyType == typeof(long) || persistentProperty.PropertyType == typeof(long?))
{
long val = 0;
if (valueString == null)
{
// set to default value
persistentProperty.SetValue(updateObject, null, null);
}
else if (long.TryParse(valueString, out val))
{
persistentProperty.SetValue(updateObject, val, null);
}
}
else if (persistentProperty.PropertyType == typeof(DateTime) || persistentProperty.PropertyType == typeof(DateTime?))
{
DateTime valDt = default(DateTime);
if (string.IsNullOrEmpty(valueString))
{
// set to default value
persistentProperty.SetValue(updateObject, null, null);
}
else if (DateTime.TryParse(valueString, out valDt))
{
persistentProperty.SetValue(updateObject, valDt, null);
}
}
else if (persistentProperty.PropertyType == typeof(float) || persistentProperty.PropertyType == typeof(float?))
{
float val = default(float);
if (string.IsNullOrEmpty(valueString))
{
// set to default value
persistentProperty.SetValue(updateObject, null, null);
}
else if (float.TryParse(valueString, out val))
{
persistentProperty.SetValue(updateObject, val, null);
}
}
else
{
throw new InvalidCastException(string.Format("type {0} conversion not supported yet", persistentProperty.PropertyType.ToString()));
}
}
}
}
}
}