Skip to content

Commit

Permalink
void when takes a stream, comment strings
Browse files Browse the repository at this point in the history
  • Loading branch information
westin-m committed Jul 11, 2024
1 parent 297acc1 commit c40dfec
Showing 1 changed file with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,48 +598,51 @@ public static OpenIdConnectConfiguration Read(ref Utf8JsonReader reader, OpenIdC
#endregion

#region Write
/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> to a UTF8-encoded JSON string.
/// </summary>
/// <param name="OpenIdConnectConfiguration">The configuration to be written.</param>
/// <returns>A UTF8-encoded JSON string.</returns>
public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration)
{
using (MemoryStream memoryStream = new MemoryStream())
{
return Write(OpenIdConnectConfiguration, memoryStream);
Utf8JsonWriter writer = null;
try
{
writer = new Utf8JsonWriter(memoryStream, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Write(ref writer, OpenIdConnectConfiguration);
writer.Flush();
return Encoding.UTF8.GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
finally
{
writer?.Dispose();
}
}
}

public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration, Stream stream)
/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> to a UTF8-encoded JSON string using the provided <see cref="Stream"/>.
/// </summary>
/// <param name="OpenIdConnectConfiguration">The <see cref="OpenIdConnectConfiguration"/> to be written.</param>
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <remarks>Because a <see cref="Stream"/> is provided, this method does not return a value.</remarks>
public static void Write(OpenIdConnectConfiguration OpenIdConnectConfiguration, Stream stream)
{
Utf8JsonWriter writer = null;
try
{
writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Write(ref writer, OpenIdConnectConfiguration);
writer.Flush();
var bytes = GetBytesFromStream(stream);
return Encoding.UTF8.GetString(bytes, 0, (int)stream.Length);
}
finally
{
writer?.Dispose();
}
}

private static byte[] GetBytesFromStream(Stream stream)
{
if (stream is MemoryStream memStream)
{
return memStream.GetBuffer();
}

byte[] bytes;
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
bytes = memoryStream.GetBuffer();
}

return bytes;
}

public static void Write(ref Utf8JsonWriter writer, OpenIdConnectConfiguration config)
{
writer.WriteStartObject();
Expand Down

0 comments on commit c40dfec

Please sign in to comment.