@@ -16,7 +16,7 @@ public class JsonApiDeSerializer : IJsonApiDeSerializer
16
16
private readonly IJsonApiContext _jsonApiContext ;
17
17
private readonly IGenericProcessorFactory _genericProcessorFactor ;
18
18
19
- public JsonApiDeSerializer (
19
+ public JsonApiDeSerializer (
20
20
IJsonApiContext jsonApiContext ,
21
21
IGenericProcessorFactory genericProcessorFactory )
22
22
{
@@ -26,9 +26,16 @@ public JsonApiDeSerializer(
26
26
27
27
public object Deserialize ( string requestBody )
28
28
{
29
- var document = JsonConvert . DeserializeObject < Document > ( requestBody ) ;
30
- var entity = DocumentToObject ( document . Data ) ;
31
- return entity ;
29
+ try
30
+ {
31
+ var document = JsonConvert . DeserializeObject < Document > ( requestBody ) ;
32
+ var entity = DocumentToObject ( document . Data ) ;
33
+ return entity ;
34
+ }
35
+ catch ( Exception e )
36
+ {
37
+ throw new JsonApiException ( "400" , "Failed to deserialize request body" , e . Message ) ;
38
+ }
32
39
}
33
40
34
41
public object Deserialize < TEntity > ( string requestBody )
@@ -38,26 +45,40 @@ public object Deserialize<TEntity>(string requestBody)
38
45
39
46
public object DeserializeRelationship ( string requestBody )
40
47
{
41
- var data = JToken . Parse ( requestBody ) [ "data" ] ;
48
+ try
49
+ {
50
+ var data = JToken . Parse ( requestBody ) [ "data" ] ;
42
51
43
- if ( data is JArray )
44
- return data . ToObject < List < DocumentData > > ( ) ;
52
+ if ( data is JArray )
53
+ return data . ToObject < List < DocumentData > > ( ) ;
45
54
46
- return new List < DocumentData > { data . ToObject < DocumentData > ( ) } ;
55
+ return new List < DocumentData > { data . ToObject < DocumentData > ( ) } ;
56
+ }
57
+ catch ( Exception e )
58
+ {
59
+ throw new JsonApiException ( "400" , "Failed to deserialize request body" , e . Message ) ;
60
+ }
47
61
}
48
62
49
63
public List < TEntity > DeserializeList < TEntity > ( string requestBody )
50
64
{
51
- var documents = JsonConvert . DeserializeObject < Documents > ( requestBody ) ;
65
+ try
66
+ {
67
+ var documents = JsonConvert . DeserializeObject < Documents > ( requestBody ) ;
68
+
69
+ var deserializedList = new List < TEntity > ( ) ;
70
+ foreach ( var data in documents . Data )
71
+ {
72
+ var entity = DocumentToObject ( data ) ;
73
+ deserializedList . Add ( ( TEntity ) entity ) ;
74
+ }
52
75
53
- var deserializedList = new List < TEntity > ( ) ;
54
- foreach ( var data in documents . Data )
76
+ return deserializedList ;
77
+ }
78
+ catch ( Exception e )
55
79
{
56
- var entity = DocumentToObject ( data ) ;
57
- deserializedList . Add ( ( TEntity ) entity ) ;
80
+ throw new JsonApiException ( "400" , "Failed to deserialize request body" , e . Message ) ;
58
81
}
59
-
60
- return deserializedList ;
61
82
}
62
83
63
84
private object DocumentToObject ( DocumentData data )
@@ -66,7 +87,7 @@ private object DocumentToObject(DocumentData data)
66
87
_jsonApiContext . RequestEntity = contextEntity ;
67
88
68
89
var entity = Activator . CreateInstance ( contextEntity . EntityType ) ;
69
-
90
+
70
91
entity = SetEntityAttributes ( entity , contextEntity , data . Attributes ) ;
71
92
entity = SetRelationships ( entity , contextEntity , data . Relationships ) ;
72
93
@@ -106,8 +127,8 @@ private object SetEntityAttributes(
106
127
}
107
128
108
129
private object SetRelationships (
109
- object entity ,
110
- ContextEntity contextEntity ,
130
+ object entity ,
131
+ ContextEntity contextEntity ,
111
132
Dictionary < string , RelationshipData > relationships )
112
133
{
113
134
if ( relationships == null || relationships . Count == 0 )
@@ -117,18 +138,18 @@ private object SetRelationships(
117
138
118
139
foreach ( var attr in contextEntity . Relationships )
119
140
{
120
- entity = attr . IsHasOne
121
- ? SetHasOneRelationship ( entity , entityProperties , attr , contextEntity , relationships )
141
+ entity = attr . IsHasOne
142
+ ? SetHasOneRelationship ( entity , entityProperties , attr , contextEntity , relationships )
122
143
: SetHasManyRelationship ( entity , entityProperties , attr , contextEntity , relationships ) ;
123
144
}
124
145
125
146
return entity ;
126
147
}
127
148
128
- private object SetHasOneRelationship ( object entity ,
129
- PropertyInfo [ ] entityProperties ,
130
- RelationshipAttribute attr ,
131
- ContextEntity contextEntity ,
149
+ private object SetHasOneRelationship ( object entity ,
150
+ PropertyInfo [ ] entityProperties ,
151
+ RelationshipAttribute attr ,
152
+ ContextEntity contextEntity ,
132
153
Dictionary < string , RelationshipData > relationships )
133
154
{
134
155
var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == $ "{ attr . InternalRelationshipName } Id") ;
@@ -142,7 +163,7 @@ private object SetHasOneRelationship(object entity,
142
163
{
143
164
var relationshipAttr = _jsonApiContext . RequestEntity . Relationships
144
165
. SingleOrDefault ( r => r . PublicRelationshipName == relationshipName ) ;
145
-
166
+
146
167
var data = ( Dictionary < string , string > ) relationshipData . ExposedData ;
147
168
148
169
if ( data == null ) return entity ;
@@ -159,9 +180,9 @@ private object SetHasOneRelationship(object entity,
159
180
}
160
181
161
182
private object SetHasManyRelationship ( object entity ,
162
- PropertyInfo [ ] entityProperties ,
163
- RelationshipAttribute attr ,
164
- ContextEntity contextEntity ,
183
+ PropertyInfo [ ] entityProperties ,
184
+ RelationshipAttribute attr ,
185
+ ContextEntity contextEntity ,
165
186
Dictionary < string , RelationshipData > relationships )
166
187
{
167
188
var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == attr . InternalRelationshipName ) ;
@@ -179,7 +200,7 @@ private object SetHasManyRelationship(object entity,
179
200
180
201
var genericProcessor = _genericProcessorFactor . GetProcessor ( attr . Type ) ;
181
202
var ids = relationshipData . ManyData . Select ( r => r [ "id" ] ) ;
182
- genericProcessor . SetRelationships ( entity , attr , ids ) ;
203
+ genericProcessor . SetRelationships ( entity , attr , ids ) ;
183
204
}
184
205
185
206
return entity ;
0 commit comments