Skip to content

Commit

Permalink
serialization of the two classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeming Liu committed Apr 21, 2021
1 parent 293a1f2 commit e808485
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// </summary>
public partial class SubResource : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input SubResource 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);
}
writer.WriteEndObject();
}

/// <summary>
/// Deserialize the input JSON element to a SubResource object.
/// </summary>
/// <param name="element">The JSON element to be deserialized.</param>
/// <returns>Deserialized SubResource object.</returns>
internal static SubResource DeserializeSubResource(JsonElement element)
{
Optional<string> id = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("id"))
{
id = property.Value.GetString();
continue;
}
}
return new SubResource(id.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ namespace Azure.ResourceManager.Core
/// A class representing a sub-resource that contains only the ID.
/// </summary>
[ReferenceType]
public abstract class SubResource
public partial class SubResource
{
/// <summary> Initializes a new instance of SubResource. </summary>
/// <param name="id"> ARM resource Id. </param>
internal SubResource(string id)
{
Id = id;
}

/// <summary>
/// ARM resource identifier.
/// </summary>
/// <value></value>
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
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// </summary>
public partial class SubResourceReadOnly : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input SubResourceReadOnly 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();
writer.WriteEndObject();
}

/// <summary>
/// Deserialize the input JSON element to a SubResourceReadOnly object.
/// </summary>
/// <param name="element">The JSON element to be deserialized.</param>
/// <returns>Deserialized SubResourceReadOnly object.</returns>
internal static SubResourceReadOnly DeserializeSubResourceReadOnly(JsonElement element)
{
Optional<string> id = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("id"))
{
id = property.Value.GetString();
continue;
}
}
return new SubResourceReadOnly(id.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ namespace Azure.ResourceManager.Core
/// A class representing a sub-resource that contains only the read-only ID.
/// </summary>
[ReferenceType]
public abstract class SubResourceReadOnly
public partial class SubResourceReadOnly
{
/// <summary> Initializes a new instance of SubResourceReadOnly. </summary>
/// <param name="id"> ARM resource Id. </param>
internal SubResourceReadOnly(string id)
{
Id = id;
}

/// <summary>
/// ARM resource identifier (read-only).
/// </summary>
/// <value></value>
public virtual ResourceIdentifier Id { get; }
public virtual ResourceIdentifier Id { get; protected set; }
}
}

0 comments on commit e808485

Please sign in to comment.