sdk/sqlmanagement/Azure.ResourceManager.Sql/src/Custom/Models/ServiceObjectiveListResult.Serialization.cs (154 lines of code) (raw):
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable disable
using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using Azure.Core;
namespace Azure.ResourceManager.Sql.Models
{
internal partial class ServiceObjectiveListResult : IUtf8JsonSerializable, IJsonModel<ServiceObjectiveListResult>
{
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel<ServiceObjectiveListResult>)this).Write(writer, ModelSerializationExtensions.WireOptions);
void IJsonModel<ServiceObjectiveListResult>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
JsonModelWriteCore(writer, options);
writer.WriteEndObject();
}
/// <param name="writer"> The JSON writer. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
var format = options.Format == "W" ? ((IPersistableModel<ServiceObjectiveListResult>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ServiceObjectiveListResult)} does not support writing '{format}' format.");
}
writer.WritePropertyName("value"u8);
writer.WriteStartArray();
foreach (var item in Value)
{
writer.WriteObjectValue(item, options);
}
writer.WriteEndArray();
if (options.Format != "W" && _serializedAdditionalRawData != null)
{
foreach (var item in _serializedAdditionalRawData)
{
writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
writer.WriteRawValue(item.Value);
#else
using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions))
{
JsonSerializer.Serialize(writer, document.RootElement);
}
#endif
}
}
}
ServiceObjectiveListResult IJsonModel<ServiceObjectiveListResult>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
var format = options.Format == "W" ? ((IPersistableModel<ServiceObjectiveListResult>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ServiceObjectiveListResult)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeServiceObjectiveListResult(document.RootElement, options);
}
internal static ServiceObjectiveListResult DeserializeServiceObjectiveListResult(JsonElement element, ModelReaderWriterOptions options = null)
{
options ??= ModelSerializationExtensions.WireOptions;
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
IReadOnlyList<ServiceObjectiveData> value = default;
IDictionary<string, BinaryData> serializedAdditionalRawData = default;
Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>();
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("value"u8))
{
List<ServiceObjectiveData> array = new List<ServiceObjectiveData>();
foreach (var item in property.Value.EnumerateArray())
{
array.Add(ServiceObjectiveData.DeserializeServiceObjectiveData(item, options));
}
value = array;
continue;
}
if (options.Format != "W")
{
rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
}
}
serializedAdditionalRawData = rawDataDictionary;
return new ServiceObjectiveListResult(value, serializedAdditionalRawData);
}
private BinaryData SerializeBicep(ModelReaderWriterOptions options)
{
StringBuilder builder = new StringBuilder();
BicepModelReaderWriterOptions bicepOptions = options as BicepModelReaderWriterOptions;
IDictionary<string, string> propertyOverrides = null;
bool hasObjectOverride = bicepOptions != null && bicepOptions.PropertyOverrides.TryGetValue(this, out propertyOverrides);
bool hasPropertyOverride = false;
string propertyOverride = null;
builder.AppendLine("{");
hasPropertyOverride = hasObjectOverride && propertyOverrides.TryGetValue(nameof(Value), out propertyOverride);
if (hasPropertyOverride)
{
builder.Append(" value: ");
builder.AppendLine(propertyOverride);
}
else
{
if (Optional.IsCollectionDefined(Value))
{
if (Value.Any())
{
builder.Append(" value: ");
builder.AppendLine("[");
foreach (var item in Value)
{
BicepSerializationHelpers.AppendChildObject(builder, item, options, 4, true, " value: ");
}
builder.AppendLine(" ]");
}
}
}
builder.AppendLine("}");
return BinaryData.FromString(builder.ToString());
}
BinaryData IPersistableModel<ServiceObjectiveListResult>.Write(ModelReaderWriterOptions options)
{
var format = options.Format == "W" ? ((IPersistableModel<ServiceObjectiveListResult>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options);
case "bicep":
return SerializeBicep(options);
default:
throw new FormatException($"The model {nameof(ServiceObjectiveListResult)} does not support writing '{options.Format}' format.");
}
}
ServiceObjectiveListResult IPersistableModel<ServiceObjectiveListResult>.Create(BinaryData data, ModelReaderWriterOptions options)
{
var format = options.Format == "W" ? ((IPersistableModel<ServiceObjectiveListResult>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
{
using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions);
return DeserializeServiceObjectiveListResult(document.RootElement, options);
}
default:
throw new FormatException($"The model {nameof(ServiceObjectiveListResult)} does not support reading '{options.Format}' format.");
}
}
string IPersistableModel<ServiceObjectiveListResult>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
}
}