7
7
using JsonApiDotNetCore . Models ;
8
8
using JsonApiDotNetCore . Services ;
9
9
using Newtonsoft . Json ;
10
+ using Newtonsoft . Json . Linq ;
10
11
11
12
namespace JsonApiDotNetCore . Serialization
12
13
{
@@ -23,14 +24,17 @@ public static object Deserialize(string requestBody, IJsonApiContext context)
23
24
24
25
var entity = Activator . CreateInstance ( contextEntity . EntityType ) ;
25
26
26
- return _setEntityAttributes ( entity , contextEntity , document . Data . Attributes ) ;
27
+ entity = _setEntityAttributes ( entity , contextEntity , document . Data . Attributes ) ;
28
+ entity = _setRelationships ( entity , contextEntity , document . Data . Relationships ) ;
29
+
30
+ return entity ;
27
31
}
28
32
29
33
private static object _setEntityAttributes (
30
34
object entity , ContextEntity contextEntity , Dictionary < string , object > attributeValues )
31
35
{
32
36
var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
33
-
37
+
34
38
foreach ( var attr in contextEntity . Attributes )
35
39
{
36
40
var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == attr . InternalAttributeName ) ;
@@ -39,10 +43,38 @@ private static object _setEntityAttributes(
39
43
throw new ArgumentException ( $ "{ contextEntity . EntityType . Name } does not contain an attribute named { attr . InternalAttributeName } ", nameof ( entity ) ) ;
40
44
41
45
object newValue ;
42
- if ( attributeValues . TryGetValue ( attr . PublicAttributeName , out newValue ) )
46
+ if ( attributeValues . TryGetValue ( attr . PublicAttributeName . Dasherize ( ) , out newValue ) )
47
+ {
48
+ var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
49
+ entityProperty . SetValue ( entity , convertedValue ) ;
50
+ }
51
+ }
52
+
53
+ return entity ;
54
+ }
55
+
56
+ private static object _setRelationships (
57
+ object entity , ContextEntity contextEntity , Dictionary < string , Dictionary < string , object > > relationships )
58
+ {
59
+ if ( relationships == null )
60
+ return entity ;
61
+
62
+ var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
63
+
64
+ foreach ( var attr in contextEntity . Relationships )
65
+ {
66
+ var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == $ "{ attr . RelationshipName } Id") ;
67
+
68
+ if ( entityProperty == null )
69
+ throw new ArgumentException ( $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . RelationshipName } ", nameof ( entity ) ) ;
70
+
71
+ Dictionary < string , object > relationshipData ;
72
+ if ( relationships . TryGetValue ( attr . RelationshipName . Dasherize ( ) , out relationshipData ) )
43
73
{
44
- Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
45
- entityProperty . SetValue ( entity , newValue ) ;
74
+ var data = ( ( JObject ) relationshipData [ "data" ] ) . ToObject < Dictionary < string , string > > ( ) ;
75
+ var newValue = data [ "id" ] ;
76
+ var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
77
+ entityProperty . SetValue ( entity , convertedValue ) ;
46
78
}
47
79
}
48
80
0 commit comments