Datei: NDO.Mapping/NDO.Mapping/Serialization/SerializationExtensions.cs
Last Commit (a5a7f46)
1 | -- File didn't exist -- |
New Commit (b74c314)
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 |