77using JsonApiDotNetCore . Models ;
88using JsonApiDotNetCore . Services ;
99using Newtonsoft . Json ;
10- using Newtonsoft . Json . Linq ;
1110
1211namespace JsonApiDotNetCore . Serialization
1312{
@@ -17,24 +16,50 @@ public static object Deserialize(string requestBody, IJsonApiContext context)
1716 {
1817 var document = JsonConvert . DeserializeObject < Document > ( requestBody ) ;
1918
20- var entityTypeName = document . Data . Type . ToProperCase ( ) ;
19+ var entity = DataToObject ( document . Data , context ) ;
20+
21+ return entity ;
22+ }
23+
24+ public static List < TEntity > DeserializeList < TEntity > ( string requestBody , IJsonApiContext context )
25+ {
26+ var documents = JsonConvert . DeserializeObject < Documents > ( requestBody ) ;
27+
28+ var deserializedList = new List < TEntity > ( ) ;
29+ foreach ( var data in documents . Data )
30+ {
31+ var entity = DataToObject ( data , context ) ;
32+ deserializedList . Add ( ( TEntity ) entity ) ;
33+ }
34+
35+ return deserializedList ;
36+ }
37+
38+ private static object DataToObject ( DocumentData data , IJsonApiContext context )
39+ {
40+ var entityTypeName = data . Type . ToProperCase ( ) ;
2141
2242 var contextEntity = context . ContextGraph . GetContextEntity ( entityTypeName ) ;
2343 context . RequestEntity = contextEntity ;
24-
44+
2545 var entity = Activator . CreateInstance ( contextEntity . EntityType ) ;
26-
27- entity = _setEntityAttributes ( entity , contextEntity , document . Data . Attributes ) ;
28- entity = _setRelationships ( entity , contextEntity , document . Data . Relationships ) ;
2946
30- return entity ;
47+ entity = _setEntityAttributes ( entity , contextEntity , data . Attributes ) ;
48+ entity = _setRelationships ( entity , contextEntity , data . Relationships ) ;
49+
50+ var identifiableEntity = ( IIdentifiable ) entity ;
51+
52+ if ( data . Id != null )
53+ identifiableEntity . Id = Convert . ChangeType ( data . Id , identifiableEntity . Id . GetType ( ) ) ;
54+
55+ return identifiableEntity ;
3156 }
3257
3358 private static object _setEntityAttributes (
3459 object entity , ContextEntity contextEntity , Dictionary < string , object > attributeValues )
3560 {
3661 var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
37-
62+
3863 foreach ( var attr in contextEntity . Attributes )
3964 {
4065 var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == attr . InternalAttributeName ) ;
@@ -56,22 +81,26 @@ private static object _setEntityAttributes(
5681 private static object _setRelationships (
5782 object entity , ContextEntity contextEntity , Dictionary < string , RelationshipData > relationships )
5883 {
59- if ( relationships == null )
84+ if ( relationships == null || relationships . Count == 0 )
6085 return entity ;
6186
6287 var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
63-
88+
6489 foreach ( var attr in contextEntity . Relationships )
6590 {
6691 var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == $ "{ attr . RelationshipName } Id") ;
6792
6893 if ( entityProperty == null )
69- throw new ArgumentException ( $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . RelationshipName } ", nameof ( entity ) ) ;
70-
94+ throw new JsonApiException ( "400" , $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . RelationshipName } ") ;
95+
96+ var relationshipName = attr . RelationshipName . Dasherize ( ) ;
7197 RelationshipData relationshipData ;
72- if ( relationships . TryGetValue ( attr . RelationshipName . Dasherize ( ) , out relationshipData ) )
98+ if ( relationships . TryGetValue ( relationshipName , out relationshipData ) )
7399 {
74- var data = ( Dictionary < string , string > ) relationshipData . ExposedData ;
100+ var data = ( Dictionary < string , string > ) relationshipData . ExposedData ;
101+
102+ if ( data == null ) continue ;
103+
75104 var newValue = data [ "id" ] ;
76105 var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
77106 entityProperty . SetValue ( entity , convertedValue ) ;
0 commit comments