11using System . Collections . Generic ;
2+ using JsonApiDotNetCore . Models . Pointers ;
23using Newtonsoft . Json ;
4+ using Newtonsoft . Json . Linq ;
5+ using Newtonsoft . Json . Schema ;
36
47namespace JsonApiDotNetCore . Models
58{
69 public class DocumentData
710 {
811 [ JsonProperty ( "type" ) ]
9- public string Type { get ; set ; }
12+ public object Type { get ; set ; }
1013
1114 [ JsonProperty ( "id" ) ]
12- public string Id { get ; set ; }
15+ public object Id { get ; set ; }
1316
1417 [ JsonProperty ( "attributes" ) ]
1518 public Dictionary < string , object > Attributes { get ; set ; }
1619
1720 [ JsonProperty ( "relationships" ) ]
1821 public Dictionary < string , RelationshipData > Relationships { get ; set ; }
1922 }
23+
24+ public class DocumentDataPointerReplacement < TPointer , TPointerBase >
25+ where TPointer : Pointer < TPointerBase > , new ( )
26+ {
27+ private readonly DocumentData _data ;
28+
29+ public DocumentDataPointerReplacement ( DocumentData data )
30+ {
31+ _data = data ;
32+ }
33+
34+ public void ReplacePointers ( List < TPointerBase > parentDoc )
35+ {
36+ ReplacePointer ( _data . Id , parentDoc ) ;
37+ ReplacePointer ( _data . Type , parentDoc ) ;
38+ }
39+
40+ private void ReplacePointer ( object reference , List < TPointerBase > parentDoc )
41+ {
42+ if ( reference is JObject jObj )
43+ if ( jObj . TryParse < TPointer , TPointerBase > ( Pointer < TPointerBase > . JsonSchema , out Pointer < TPointerBase > pointer ) )
44+ reference = pointer . GetValue ( parentDoc ) ;
45+ }
46+ }
2047}
48+
49+ public static class JObjectExtensions
50+ {
51+ public static bool TryParse < TPointer , TPointerBase > ( this JObject obj , JSchema schema , out Pointer < TPointerBase > pointer )
52+ where TPointer : Pointer < TPointerBase > , new ( )
53+ {
54+ if ( obj . IsValid ( schema ) )
55+ {
56+ pointer = obj . ToObject < TPointer > ( ) ;
57+ return true ;
58+ }
59+
60+ pointer = null ;
61+ return false ;
62+ }
63+ }
0 commit comments