diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs new file mode 100644 index 0000000000000..764903ec81717 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using Azure.Core; + +namespace Azure.ResourceManager.Core +{ + /// + /// A class representing a sub-resource that contains only the ID. + /// + public partial class SubResource : IUtf8JsonSerializable + { + /// + /// Serialize the input SubResource object. + /// + /// Input Json writer. + 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); + } + writer.WriteEndObject(); + } + + /// + /// Deserialize the input JSON element to a SubResource object. + /// + /// The JSON element to be deserialized. + /// Deserialized SubResource object. + internal static SubResource DeserializeSubResource(JsonElement element) + { + Optional id = default; + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("id")) + { + id = property.Value.GetString(); + continue; + } + } + return new SubResource(id.Value); + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs index f0f92ae6dafaa..84673404628ff 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs @@ -7,8 +7,15 @@ namespace Azure.ResourceManager.Core /// A class representing a sub-resource that contains only the ID. /// [ReferenceType] - public abstract class SubResource + public partial class SubResource { + /// Initializes a new instance of SubResource. + /// ARM resource Id. + internal SubResource(string id) + { + Id = id; + } + /// /// ARM resource identifier. /// @@ -16,3 +23,6 @@ public abstract class SubResource public virtual ResourceIdentifier Id { get; set; } } } + +// Todo: we want to make the default one (SubResource) to represent read-only data, +// that is the pattern we used in Resource and TrackedResource diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.Serialization.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.Serialization.cs new file mode 100644 index 0000000000000..73da313642c81 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.Serialization.cs @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using Azure.Core; + +namespace Azure.ResourceManager.Core +{ + /// + /// A class representing a sub-resource that contains only the ID. + /// + public partial class SubResourceReadOnly : IUtf8JsonSerializable + { + /// + /// Serialize the input SubResourceReadOnly object. + /// + /// Input Json writer. + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) + { + if (writer is null) + { + throw new ArgumentNullException(nameof(writer)); + } + + writer.WriteStartObject(); + writer.WriteEndObject(); + } + + /// + /// Deserialize the input JSON element to a SubResourceReadOnly object. + /// + /// The JSON element to be deserialized. + /// Deserialized SubResourceReadOnly object. + internal static SubResourceReadOnly DeserializeSubResourceReadOnly(JsonElement element) + { + Optional id = default; + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("id")) + { + id = property.Value.GetString(); + continue; + } + } + return new SubResourceReadOnly(id.Value); + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.cs index cc99e43fe6d49..1f94564c78d88 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.cs @@ -7,12 +7,19 @@ namespace Azure.ResourceManager.Core /// A class representing a sub-resource that contains only the read-only ID. /// [ReferenceType] - public abstract class SubResourceReadOnly + public partial class SubResourceReadOnly { + /// Initializes a new instance of SubResourceReadOnly. + /// ARM resource Id. + internal SubResourceReadOnly(string id) + { + Id = id; + } + /// /// ARM resource identifier (read-only). /// /// - public virtual ResourceIdentifier Id { get; } + public virtual ResourceIdentifier Id { get; protected set; } } }