|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace System.ClientModel.Primitives; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Helper class for JSON models implementing <see cref="IJsonModel{T}"/>. |
| 10 | +/// </summary> |
| 11 | +/// <typeparam name="T">The type of the model represented by this JSON model.</typeparam> |
| 12 | +public abstract class JsonModel<T> : IJsonModel<T>, IPersistableModel<T> |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Writes the model to the provided <see cref="Utf8JsonWriter"/>. |
| 16 | + /// </summary> |
| 17 | + /// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param> |
| 18 | + /// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param> |
| 19 | + protected abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Reads one JSON value (including objects or arrays) from the provided reader and converts it to a model. |
| 23 | + /// </summary> |
| 24 | + /// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param> |
| 25 | + /// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param> |
| 26 | + /// <returns>The model created from the JSON value.</returns> |
| 27 | + protected abstract T CreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options); |
| 28 | + |
| 29 | + #region MRW |
| 30 | + T IJsonModel<T>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 31 | + { |
| 32 | + ValidateFormat(options); |
| 33 | + return CreateCore(ref reader, options); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Converts the provided <see cref="BinaryData"/> into a model. |
| 38 | + /// </summary> |
| 39 | + /// <param name="data">The <see cref="BinaryData"/> to parse.</param> |
| 40 | + /// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param> |
| 41 | + /// <returns>The model created from the data.</returns> |
| 42 | + public T Create(BinaryData data, ModelReaderWriterOptions? options = null) |
| 43 | + { |
| 44 | + options ??= ModelReaderWriterOptions.Json; |
| 45 | + ValidateFormat(options); |
| 46 | + |
| 47 | + var reader = new Utf8JsonReader(data.ToMemory().Span); |
| 48 | + return CreateCore(ref reader, options); |
| 49 | + } |
| 50 | + |
| 51 | + string IPersistableModel<T>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 52 | + |
| 53 | + void IJsonModel<T>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 54 | + { |
| 55 | + ValidateFormat(options); |
| 56 | + WriteCore(writer, options); |
| 57 | + } |
| 58 | + |
| 59 | + BinaryData IPersistableModel<T>.Write(ModelReaderWriterOptions options) |
| 60 | + { |
| 61 | + ValidateFormat(options); |
| 62 | + using MemoryStream stream = new MemoryStream(); |
| 63 | + using Utf8JsonWriter writer = new Utf8JsonWriter(stream); |
| 64 | + WriteCore(writer, options); |
| 65 | + writer.Flush(); |
| 66 | + byte[] buffer = stream.GetBuffer(); |
| 67 | + ReadOnlyMemory<byte> memory = buffer.AsMemory(0, (int)stream.Position); |
| 68 | + return new BinaryData(memory); |
| 69 | + } |
| 70 | + |
| 71 | + private static void ValidateFormat(ModelReaderWriterOptions options) |
| 72 | + { |
| 73 | + if (options.Format != "J" && options.Format != "W") |
| 74 | + { |
| 75 | + throw new FormatException($"The model does not support '{options.Format}' format. Supported formats are 'J' and 'W'."); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + #endregion |
| 80 | +} |
0 commit comments