| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="Utf8JsonWriter"/> |
| | 5 | | /// </summary> |
| | 6 | | public static class Utf8JsonWriterExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Wrap <see cref="Utf8JsonWriter"/> statements |
| | 10 | | /// inside <see cref="Utf8JsonWriter.WriteStartObject()"/> |
| | 11 | | /// and <see cref="Utf8JsonWriter.WriteEndObject"/>. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="writer">the <see cref="Utf8JsonWriter"/></param> |
| | 14 | | /// <param name="writerAction"></param> |
| | 15 | | /// <remarks> |
| | 16 | | /// This method is for building a JSON object. |
| | 17 | | /// </remarks> |
| | 18 | | public static Utf8JsonWriter? WriteObject(this Utf8JsonWriter? writer, Action? writerAction) |
| 2 | 19 | | { |
| 2 | 20 | | if (writer == null) return writer; |
| | 21 | |
|
| 2 | 22 | | writer.WriteStartObject(); |
| 2 | 23 | | writerAction?.Invoke(); |
| 2 | 24 | | writer.WriteEndObject(); |
| | 25 | |
|
| 2 | 26 | | return writer; |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Wrap <see cref="Utf8JsonWriter"/> statements |
| | 31 | | /// inside <see cref="Utf8JsonWriter.WritePropertyName(string)"/> |
| | 32 | | /// <see cref="Utf8JsonWriter.WriteStartObject()"/> |
| | 33 | | /// and <see cref="Utf8JsonWriter.WriteEndObject"/>. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="writer">the <see cref="Utf8JsonWriter"/></param> |
| | 36 | | /// <param name="propertyName"></param> |
| | 37 | | /// <param name="writerAction"></param> |
| | 38 | | /// <remarks> |
| | 39 | | /// This method is for building a JSON object for a JSON property. |
| | 40 | | /// </remarks> |
| | 41 | | public static Utf8JsonWriter? WriteObject(this Utf8JsonWriter? writer, string propertyName, Action? writerAction) |
| 4 | 42 | | { |
| 4 | 43 | | if (writer == null) return writer; |
| 4 | 44 | | propertyName.ThrowWhenNullOrWhiteSpace(); |
| | 45 | |
|
| 4 | 46 | | writer.WritePropertyName(propertyName); |
| 4 | 47 | | writer.WriteStartObject(); |
| 4 | 48 | | writerAction?.Invoke(); |
| 4 | 49 | | writer.WriteEndObject(); |
| | 50 | |
|
| 4 | 51 | | return writer; |
| 4 | 52 | | } |
| | 53 | | } |