20
20
21
21
namespace SysML2 . NET . Serializer . Json
22
22
{
23
- using System ;
24
23
using System . Collections . Generic ;
25
24
using System . IO ;
26
25
using System . Text . Json ;
@@ -31,11 +30,26 @@ namespace SysML2.NET.Serializer.Json
31
30
using SysML2 . NET . Serializer . Json . AutoGenSerializer ;
32
31
33
32
/// <summary>
34
- /// The purpose of the <see cref="Serializer"/> is to write an <see cref="IEnumerable{T }"/> of
35
- /// <see cref="Element"/> as JSON to a <see cref="Stream"/>
33
+ /// The purpose of the <see cref="Serializer"/> is to write an <see cref="IElement"/> and <see cref=" IEnumerable{IElement }"/>
34
+ /// as JSON to a <see cref="Stream"/>
36
35
/// </summary>
37
36
public class Serializer : ISerializer
38
37
{
38
+ /// <summary>
39
+ /// Serialize an <see cref="IEnumerable{IElement}"/> as JSON to a target <see cref="Stream"/>
40
+ /// </summary>
41
+ /// <param name="elements">
42
+ /// The <see cref="IEnumerable{IElement}"/> that shall be serialized
43
+ /// </param>
44
+ /// <param name="serializationModeKind">
45
+ /// The <see cref="SerializationModeKind"/> to use
46
+ /// </param>
47
+ /// <param name="stream">
48
+ /// The target <see cref="Stream"/>
49
+ /// </param>
50
+ /// <param name="jsonWriterOptions">
51
+ /// The <see cref="JsonWriterOptions"/> to use
52
+ /// </param>
39
53
public void Serialize ( IEnumerable < IElement > elements , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions )
40
54
{
41
55
using ( var writer = new Utf8JsonWriter ( stream , jsonWriterOptions ) )
@@ -55,46 +69,88 @@ public void Serialize(IEnumerable<IElement> elements, SerializationModeKind seri
55
69
}
56
70
}
57
71
72
+ /// <summary>
73
+ /// Serialize an <see cref="IElement"/> as JSON to a target <see cref="Stream"/>
74
+ /// </summary>
75
+ /// <param name="element">
76
+ /// The <see cref="IElement"/> that shall be serialized
77
+ /// </param>
78
+ /// <param name="serializationModeKind">
79
+ /// The <see cref="SerializationModeKind"/> to use
80
+ /// </param>
81
+ /// <param name="stream">
82
+ /// The target <see cref="Stream"/>
83
+ /// </param>
84
+ /// <param name="jsonWriterOptions">
85
+ /// The <see cref="JsonWriterOptions"/> to use
86
+ /// </param>
58
87
public void Serialize ( IElement element , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions )
59
88
{
60
- throw new NotImplementedException ( ) ;
61
-
62
- //using (var writer = new Utf8JsonWriter(stream, jsonWriterOptions))
63
- //{
64
- // element.Serialize(writer, serializationModeKind);
65
- // writer.Flush();
66
- //}
89
+ using ( var writer = new Utf8JsonWriter ( stream , jsonWriterOptions ) )
90
+ {
91
+ var serializationAction = SerializationProvider . Provide ( element . GetType ( ) ) ;
92
+ serializationAction ( element , writer , serializationModeKind ) ;
93
+ writer . Flush ( ) ;
94
+ }
67
95
}
68
96
97
+ /// <summary>
98
+ /// Asynchronously serialize an <see cref="IEnumerable{IElement}"/> as JSON to a target <see cref="Stream"/>
99
+ /// </summary>
100
+ /// <param name="elements">
101
+ /// The <see cref="IEnumerable{IElement}"/> that shall be serialized
102
+ /// </param>
103
+ /// <param name="serializationModeKind">
104
+ /// The <see cref="SerializationModeKind"/> to use
105
+ /// </param>
106
+ /// <param name="stream">
107
+ /// The target <see cref="Stream"/>
108
+ /// </param>
109
+ /// <param name="jsonWriterOptions">
110
+ /// The <see cref="JsonWriterOptions"/> to use
111
+ /// </param>
69
112
public async Task SerializeAsync ( IEnumerable < IElement > elements , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions , CancellationToken cancellationToken )
70
113
{
71
- throw new NotImplementedException ( ) ;
72
-
73
- //using (var writer = new Utf8JsonWriter(stream, jsonWriterOptions))
74
- //{
75
- // writer.WriteStartArray();
114
+ using ( var writer = new Utf8JsonWriter ( stream , jsonWriterOptions ) )
115
+ {
116
+ writer . WriteStartArray ( ) ;
76
117
77
- // foreach (var element in elements)
78
- // {
79
- // element.Serialize(writer, serializationModeKind);
80
- // await writer.FlushAsync(cancellationToken);
81
- // }
118
+ foreach ( var element in elements )
119
+ {
120
+ var serializationAction = SerializationProvider . Provide ( element . GetType ( ) ) ;
121
+ serializationAction ( element , writer , serializationModeKind ) ;
122
+ await writer . FlushAsync ( cancellationToken ) ;
123
+ }
82
124
83
- // writer.WriteEndArray();
125
+ writer . WriteEndArray ( ) ;
84
126
85
- // await writer.FlushAsync(cancellationToken);
86
- // }
127
+ await writer . FlushAsync ( cancellationToken ) ;
128
+ }
87
129
}
88
130
131
+ /// <summary>
132
+ /// Asynchronously serialize an <see cref="IElement"/> as JSON to a target <see cref="Stream"/>
133
+ /// </summary>
134
+ /// <param name="element">
135
+ /// The <see cref="IElement"/> that shall be serialized
136
+ /// </param>
137
+ /// <param name="serializationModeKind">
138
+ /// The <see cref="SerializationModeKind"/> to use
139
+ /// </param>
140
+ /// <param name="stream">
141
+ /// The target <see cref="Stream"/>
142
+ /// </param>
143
+ /// <param name="jsonWriterOptions">
144
+ /// The <see cref="JsonWriterOptions"/> to use
145
+ /// </param>
89
146
public async Task SerializeAsync ( IElement element , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions , CancellationToken cancellationToken )
90
147
{
91
- throw new NotImplementedException ( ) ;
92
-
93
- //using (var writer = new Utf8JsonWriter(stream, jsonWriterOptions))
94
- //{
95
- // element.Serialize(writer, serializationModeKind);
96
- // await writer.FlushAsync(cancellationToken);
97
- //}
148
+ using ( var writer = new Utf8JsonWriter ( stream , jsonWriterOptions ) )
149
+ {
150
+ var serializationAction = SerializationProvider . Provide ( element . GetType ( ) ) ;
151
+ serializationAction ( element , writer , serializationModeKind ) ;
152
+ await writer . FlushAsync ( cancellationToken ) ;
153
+ }
98
154
}
99
155
}
100
- }
156
+ }
0 commit comments