Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GenericResource Serialization #19898

Merged
merged 15 commits into from
Apr 2, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using Azure.Core;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing a generic azure resource along with the instance operations that can be performed on it.
/// </summary>
public partial class GenericResource : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input GenericResourceData object.
/// </summary>
/// <param name="writer"> Input Json writer. </param>
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteStartObject();
if (Optional.IsDefined(Id))
{
writer.WritePropertyName("id");
writer.WriteStringValue(Id.StringValue);
}
if (Optional.IsDefined(Data))
{
writer.WritePropertyName("data");
writer.WriteObjectValue(Data);
}
writer.WriteEndObject();
}

/// <summary>
/// Deserialize the input Json object.
/// </summary>
/// <param name="element"> The Json object need to be deserialized. </param>
/// <param name="operations"> The operations object to copy the client parameters from. </param>
internal static GenericResource DeserializeGenericResource(ResourceOperationsBase operations, JsonElement element)
{
Optional<GenericResourceData> data = default;
foreach (JsonProperty property in element.EnumerateObject())
HarveyLink marked this conversation as resolved.
Show resolved Hide resolved
{
if (property.NameEquals("data"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
data = GenericResourceData.DeserializeGenericResourceData(property.Value);
continue;
}
}
return new GenericResource(operations, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing a generic azure resource along with the instance operations that can be performed on it.
/// </summary>
public class GenericResource : GenericResourceOperations
public partial class GenericResource : GenericResourceOperations
{
/// <summary>
/// Initializes a new instance of the <see cref="GenericResource"/> class.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using Azure.Core;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing the generic azure resource data model.
/// </summary>
public partial class GenericResourceData : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input GenericResourceData object.
/// </summary>
/// <param name="writer"> Input Json writer. </param>
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteStartObject();
if (Optional.IsDefined(Id))
{
writer.WritePropertyName("id");
writer.WriteStringValue(Id.StringValue);
}
if (Optional.IsDefined(Kind))
{
writer.WritePropertyName("kind");
writer.WriteStringValue(Kind);
}
if (Optional.IsDefined(Location))
{
writer.WritePropertyName("location");
writer.WriteStringValue(Location.Name);
}
if (Optional.IsDefined(ManagedBy))
{
writer.WritePropertyName("managedBy");
writer.WriteStringValue(ManagedBy);
}
if (Optional.IsDefined(Name))
HarveyLink marked this conversation as resolved.
Show resolved Hide resolved
{
writer.WritePropertyName("name");
writer.WriteStringValue(Name);
}
if (Optional.IsDefined(Plan))
{
writer.WritePropertyName("plan");
writer.WriteObjectValue(Plan);
}
if (Optional.IsDefined(Sku))
{
writer.WritePropertyName("sku");
writer.WriteObjectValue(Sku);
}
if (Optional.IsCollectionDefined(Tags))
{
writer.WritePropertyName("tags");
writer.WriteStartObject();
if (Tags != null)
{
foreach (var item in Tags)
{
writer.WritePropertyName(item.Key);
writer.WriteStringValue(item.Value);
}
}
writer.WriteEndObject();
}
if (Optional.IsDefined(Type))
{
writer.WritePropertyName("type");
writer.WriteStringValue(Type.ToString());
}
writer.WriteEndObject();
}

/// <summary>
/// Deserialize the input Json object.
/// </summary>
/// <param name="element"> The Json object need to be deserialized. </param>
internal static GenericResourceData DeserializeGenericResourceData(JsonElement element)
{
Optional<Plan> plan = default;
Optional<string> kind = default;
Optional<string> managedBy = default;
Optional<Sku> sku = default;
Optional<TenantResourceIdentifier> id = default;
Optional<string> name = default;
Optional<ResourceType> type = default;
Optional<LocationData> location = default;
Optional<IDictionary<string, string>> tags = default;
foreach (JsonProperty property in element.EnumerateObject())
{
if (property.NameEquals("plan"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
plan = Plan.DeserializePlan(property.Value);
continue;
}
if (property.NameEquals("kind"))
{
kind = property.Value.GetString();
continue;
}
if (property.NameEquals("managedBy"))
{
managedBy = property.Value.GetString();
continue;
}
if (property.NameEquals("sku"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
sku = Sku.DeserializeSku(property.Value);
continue;
}
if (property.NameEquals("id"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
id = (Optional<TenantResourceIdentifier>)ResourceIdentifier.Create(property.Value.GetString());
continue;
}
if (property.NameEquals("name"))
{
name = property.Value.GetString();
continue;
}
if (property.NameEquals("type"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
type = new ResourceType(property.Value.GetString());
continue;
}
if (property.NameEquals("location"))
{
location = (LocationData)property.Value.GetString();
continue;
}
if (property.NameEquals("tags"))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
property.ThrowNonNullablePropertyIsNull();
continue;
}
Dictionary<string, string> dictionary = new();
foreach (JsonProperty property0 in property.Value.EnumerateObject())
{
dictionary.Add(property0.Name, property0.Value.GetString());
}
tags = dictionary;
continue;
}
}

GenericResourceData data = new(id.Value, location.Value)
{
Plan = plan.Value,
Kind = kind.Value,
ManagedBy = managedBy.Value,
Sku = sku.Value,
Id = id.Value,
Location = location.Value,
};
if (data.Tags != null)
{
data.Tags.Clear();
foreach (KeyValuePair<string, string> item in tags.Value)
{
data.Tags.Add(item);
}
}
return data;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing the generic azure resource data model.
/// </summary>
public class GenericResourceData : TrackedResource<TenantResourceIdentifier, ResourceManager.Resources.Models.GenericResource>
public partial class GenericResourceData : TrackedResource<TenantResourceIdentifier, ResourceManager.Resources.Models.GenericResource>
{
/// <summary>
/// Initializes a new instance of the <see cref="GenericResourceData"/> class.
Expand Down
Loading