Datei: NDO.Mapping/NDO.Mapping/Serialization/SerializationExtensions.cs

Last Commit (0bf30ca)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Text.Json;
5 using System.Text.Json.Nodes;
6
7 namespace NDO.Mapping.Serialization
8 {
9 ····/// <summary>
10 ····/// Extension class to convert attribute instances to the correct type.
11 ····/// </summary>
12 ····public static class SerializationExtensions
13 ····{
14 ········/// <summary>
15 ········/// Serializes an object which should be an NDORelationAttribute
16 ········/// </summary>
17 ········/// <param name="attr"></param>
18 ········/// <returns></returns>
 
 
 
 
19 ········public static string Serialize( this object attr )
20 ········{
21 ············if (attr.GetType().Name != "NDORelationAttribute")
22 ················throw new ArgumentException( "Wrong parameter type", "attr" );
23
24 var d = ( dynamic) attr;
25 string rt = null;
26 if ( d. RelationType != null)
27 rt = d. RelationType. FullName;
28 return JsonSerializer. Serialize( new { FullName = rt, d. Info, d. RelationName } ) ;
 
 
29 ········}
30
31 ········/// <summary>
32 ········/// Converts an attribute object to the correct type
33 ········/// </summary>
34 ········/// <param name="attr"></param>
35 ········/// <returns></returns>
 
 
 
 
36 ········public static NDORelationAttribute ConvertToNdoRelation( this object attr )
37 ········{
38 var json = Serialize( attr) ;
39 var obj = JsonSerializer. Deserialize<JsonObject>( json) ;
40 var rt = ( string) obj["FullName"];
 
 
41 ············Type relationType = null;
42 ············if (rt != null)
43 ················relationType = Type.GetType( rt );
44 return new NDORelationAttribute( relationType, ( RelationInfo) ( int) obj["Info"], ( string) obj["RelationName"] ) ;
 
 
 
 
45 ········}
46 ····}
47 }
48
New Commit (2c6a6fe)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
 
 
4
5 namespace NDO.Mapping.Serialization
6 {
7 ····/// <summary>
8 ····/// Extension class to convert attribute instances to the correct type.
9 ····/// </summary>
10 ····public static class SerializationExtensions
11 ····{
12 ········/// <summary>
13 ········/// Serializes an object which should be an NDORelationAttribute
14 ········/// </summary>
15 ········/// <param name="attr"></param>
16 ········/// <returns></returns>
17 ········/// <remarks>
18 ········/// We use a serialization mechanism which is independent from the runtime.
19 ········/// System.Text.Json imports dependencies to certain runtime implementations.
20 ········/// </remarks>
21 ········public static string Serialize( this object attr )
22 ········{
23 ············if (attr.GetType().Name != "NDORelationAttribute")
24 ················throw new ArgumentException( "Wrong parameter type", "attr" );
25 Type t = attr. GetType( ) ;
26 Type rt = ( Type) t. GetProperty( "RelationType") . GetValue( attr) ;
27 string rts = null;
28 if ( rt != null)
29 rts = rt. FullName;
30 string ri = t. GetProperty( "Info") . GetValue( attr) . ToString( ) ;
31 ············string rn = (string)t.GetProperty("RelationName").GetValue(attr);
32 ············return $@"{rts??"#null"}, {ri}, {rn??"#null"}";
33 ········}
34
35 ········/// <summary>
36 ········/// Converts an attribute object to the correct type
37 ········/// </summary>
38 ········/// <param name="attr"></param>
39 ········/// <returns></returns>
40 ········/// <remarks>
41 ········/// We use a serialization mechanism which is independent from the runtime.
42 ········/// System.Text.Json imports dependencies to certain runtime implementations.
43 ········/// </remarks>
44 ········public static NDORelationAttribute ConvertToNdoRelation( this object attr )
45 ········{
46 var strAttr = Serialize( attr) ;
47 var arr = strAttr. Split( ', ') ;
48 var rt = arr[0];
49 ············if (rt == "#null")
50 ················rt = null;
51 ············Type relationType = null;
52 ············if (rt != null)
53 ················relationType = Type.GetType( rt );
54 Enum. TryParse<RelationInfo>( arr[1], out var ri) ;
55 ············var rn = arr[1];
56 ············if (rn == "#null")
57 ················rn = null;
58 ············return new NDORelationAttribute( relationType, ri, rn );
59 ········}
60 ····}
61 }
62