2020
2121namespace SysML2 . NET . Serializer . Json
2222{
23- using System ;
2423 using System . Collections . Generic ;
2524 using System . IO ;
2625 using System . Text . Json ;
@@ -31,11 +30,26 @@ namespace SysML2.NET.Serializer.Json
3130 using SysML2 . NET . Serializer . Json . AutoGenSerializer ;
3231
3332 /// <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"/>
3635 /// </summary>
3736 public class Serializer : ISerializer
3837 {
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>
3953 public void Serialize ( IEnumerable < IElement > elements , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions )
4054 {
4155 using ( var writer = new Utf8JsonWriter ( stream , jsonWriterOptions ) )
@@ -55,46 +69,88 @@ public void Serialize(IEnumerable<IElement> elements, SerializationModeKind seri
5569 }
5670 }
5771
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>
5887 public void Serialize ( IElement element , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions )
5988 {
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+ }
6795 }
6896
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>
69112 public async Task SerializeAsync ( IEnumerable < IElement > elements , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions , CancellationToken cancellationToken )
70113 {
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 ( ) ;
76117
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+ }
82124
83- // writer.WriteEndArray();
125+ writer . WriteEndArray ( ) ;
84126
85- // await writer.FlushAsync(cancellationToken);
86- // }
127+ await writer . FlushAsync ( cancellationToken ) ;
128+ }
87129 }
88130
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>
89146 public async Task SerializeAsync ( IElement element , SerializationModeKind serializationModeKind , Stream stream , JsonWriterOptions jsonWriterOptions , CancellationToken cancellationToken )
90147 {
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+ }
98154 }
99155 }
100- }
156+ }
0 commit comments