Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net461.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public HttpAuthorization(string scheme, string parameter) { }
public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial interface IJsonSerializable
{
bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null);
bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null);
}
public partial class JsonPatchDocument
{
public JsonPatchDocument() { }
Expand Down Expand Up @@ -262,6 +267,13 @@ protected Response() { }
public override int GetHashCode() { throw null; }
public static implicit operator T (Azure.Response<T> response) { throw null; }
}
public partial class SerializableOptions
{
public SerializableOptions() { }
public bool HandleAdditionalProperties { get { throw null; } set { } }
public bool IncludeReadOnlyProperties { get { throw null; } set { } }
public bool PrettyPrint { get { throw null; } set { } }
}
public partial class SyncAsyncEventArgs : System.EventArgs
{
public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net5.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public HttpAuthorization(string scheme, string parameter) { }
public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial interface IJsonSerializable
{
bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null);
bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null);
}
public partial class JsonPatchDocument
{
public JsonPatchDocument() { }
Expand Down Expand Up @@ -262,6 +267,13 @@ protected Response() { }
public override int GetHashCode() { throw null; }
public static implicit operator T (Azure.Response<T> response) { throw null; }
}
public partial class SerializableOptions
{
public SerializableOptions() { }
public bool HandleAdditionalProperties { get { throw null; } set { } }
public bool IncludeReadOnlyProperties { get { throw null; } set { } }
public bool PrettyPrint { get { throw null; } set { } }
}
public partial class SyncAsyncEventArgs : System.EventArgs
{
public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public HttpAuthorization(string scheme, string parameter) { }
public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial interface IJsonSerializable
{
bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null);
bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null);
}
public partial class JsonPatchDocument
{
public JsonPatchDocument() { }
Expand Down Expand Up @@ -262,6 +267,13 @@ protected Response() { }
public override int GetHashCode() { throw null; }
public static implicit operator T (Azure.Response<T> response) { throw null; }
}
public partial class SerializableOptions
{
public SerializableOptions() { }
public bool HandleAdditionalProperties { get { throw null; } set { } }
public bool IncludeReadOnlyProperties { get { throw null; } set { } }
public bool PrettyPrint { get { throw null; } set { } }
}
public partial class SyncAsyncEventArgs : System.EventArgs
{
public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public HttpAuthorization(string scheme, string parameter) { }
public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial interface IJsonSerializable
{
bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null);
bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null);
}
public partial class JsonPatchDocument
{
public JsonPatchDocument() { }
Expand Down Expand Up @@ -262,6 +267,13 @@ protected Response() { }
public override int GetHashCode() { throw null; }
public static implicit operator T (Azure.Response<T> response) { throw null; }
}
public partial class SerializableOptions
{
public SerializableOptions() { }
public bool HandleAdditionalProperties { get { throw null; } set { } }
public bool IncludeReadOnlyProperties { get { throw null; } set { } }
public bool PrettyPrint { get { throw null; } set { } }
}
public partial class SyncAsyncEventArgs : System.EventArgs
{
public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public HttpAuthorization(string scheme, string parameter) { }
public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial interface IJsonSerializable
{
bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null);
bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null);
}
public partial class JsonPatchDocument
{
public JsonPatchDocument() { }
Expand Down Expand Up @@ -262,6 +267,13 @@ protected Response() { }
public override int GetHashCode() { throw null; }
public static implicit operator T (Azure.Response<T> response) { throw null; }
}
public partial class SerializableOptions
{
public SerializableOptions() { }
public bool HandleAdditionalProperties { get { throw null; } set { } }
public bool IncludeReadOnlyProperties { get { throw null; } set { } }
public bool PrettyPrint { get { throw null; } set { } }
}
public partial class SyncAsyncEventArgs : System.EventArgs
{
public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
Expand Down
7 changes: 6 additions & 1 deletion sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Animal : IJsonSerializable, IUtf8JsonSerializable
private Dictionary<string, BinaryData> RawData { get; set; } = new Dictionary<string, BinaryData>();

public bool IsHungry { get; set; } = false;
public double Weight { get; set; } = 0;
public double Weight { get; set; } = 1.1;
public string LatinName { get; private set; } = "Animalia";
public string Name { get; set; } = "Animal";

Expand All @@ -38,6 +38,11 @@ internal Animal(double weight, string latinName, string name, bool isHungry, Dic
RawData = rawData;
}

internal Animal(string name)
{
Name = name;
}

#region Serialization
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer, SerializableOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public class CatReadOnlyProperty : Animal, IJsonSerializable, IUtf8JsonSerializa

public CatReadOnlyProperty(double weight, string latinName, string name, bool isHungry, bool hasWhiskers) : base(weight, "Felis catus", name, isHungry)
{
CatLatinName = LatinName;
CatIsHungry = IsHungry;
CatWeight = Weight;
CatName = Name;
HasWhiskers = hasWhiskers;
}

Expand All @@ -28,11 +24,6 @@ internal CatReadOnlyProperty(double weight, string latinName, string name, bool

public bool HasWhiskers { get; set; } = true;

private string CatLatinName;
private bool CatIsHungry;
private double CatWeight;
private string CatName;

#region Serialization
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer, SerializableOptions options)
{
Expand All @@ -43,11 +34,11 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer, SerializableOptions opti
writer.WriteStringValue(LatinName);
}
writer.WritePropertyName("name"u8);
writer.WriteStringValue(CatName);
writer.WriteStringValue(Name);
writer.WritePropertyName("isHungry"u8);
writer.WriteBooleanValue(CatIsHungry);
writer.WriteBooleanValue(IsHungry);
writer.WritePropertyName("weight"u8);
writer.WriteNumberValue(CatWeight);
writer.WriteNumberValue(Weight);
writer.WritePropertyName("hasWhiskers"u8);
writer.WriteBooleanValue(HasWhiskers);

Expand Down Expand Up @@ -122,11 +113,10 @@ internal static CatReadOnlyProperty DeserializeCatReadOnlyProperty(JsonElement e
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeCatReadOnlyProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.CatLatinName = model.LatinName;
this.CatWeight = model.Weight;
this.CatIsHungry = model.IsHungry;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.HasWhiskers = model.HasWhiskers;
this.CatIsHungry = model.CatIsHungry;
this.IsHungry = model.IsHungry;
bytesConsumed = stream.Length;
return true;
}
Expand Down
165 changes: 165 additions & 0 deletions sdk/core/Azure.Core/tests/ModelSerializationTests/DogListProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace Azure.Core.Tests.ModelSerializationTests
{
public class DogListProperty : Animal, IJsonSerializable, IUtf8JsonSerializable
{
private Dictionary<string, BinaryData> RawData { get; set; } = new Dictionary<string, BinaryData>();
public List<string> FoodConsumed { get; set; } = new List<string> {"kibble", "egg", "peanut butter"};

public DogListProperty(string name) : base(name)
{
Name = name;
}

internal DogListProperty(double weight, string latinName, string name, bool isHungry, List<string> foodConsumed, Dictionary<string, BinaryData> rawData) : base(weight, latinName, name, isHungry, rawData)
{
RawData = rawData;
FoodConsumed = foodConsumed;
}

public DogListProperty()
{
}

#region Serialization
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer, SerializableOptions options)
{
writer.WriteStartObject();
if (options.IncludeReadOnlyProperties)
{
writer.WritePropertyName("latinName"u8);
writer.WriteStringValue(LatinName);
}
writer.WritePropertyName("name"u8);
writer.WriteStringValue(Name);
writer.WritePropertyName("isHungry"u8);
writer.WriteBooleanValue(IsHungry);
writer.WritePropertyName("weight"u8);
writer.WriteNumberValue(Weight);

writer.WritePropertyName("foodConsumed"u8);
writer.WriteStartArray();
foreach (var item in FoodConsumed)
{
writer.WriteStringValue($"{item}");
}
writer.WriteEndArray();

if (options.HandleAdditionalProperties)
{
//write out the raw data
foreach (var property in RawData)
{
writer.WritePropertyName(property.Key);
#if NET6_0_OR_GREATER
writer.WriteRawValue(property.Value);
#else
JsonSerializer.Serialize(writer, JsonDocument.Parse(property.Value.ToString()).RootElement);
#endif
}
}
writer.WriteEndObject();
}

internal static DogListProperty DeserializeDogListProperty(JsonElement element, SerializableOptions options)
{
double weight = default;
string name = "";
string latinName = "";
bool isHungry = default;
Dictionary<string, BinaryData> rawData = new Dictionary<string, BinaryData>();
List<string> foodConsumed = new List<string>();

foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("weight"u8))
{
weight = property.Value.GetDouble();
continue;
}
if (property.NameEquals("name"u8))
{
name = property.Value.GetString();
continue;
}
if (property.NameEquals("latinName"u8))
{
latinName = property.Value.GetString();
continue;
}
if (property.NameEquals("isHungry"u8))
{
isHungry = property.Value.GetBoolean();
continue;
}
if (property.NameEquals("foodConsumed"u8))
{
foreach (var item in property.Value.EnumerateArray())
{
foodConsumed.Add(item.GetString());
}
continue;
}
if (options.HandleAdditionalProperties)
{
//this means its an unknown property we got
rawData.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
}
}
return new DogListProperty(weight, latinName, name, isHungry, foodConsumed, rawData);
}
#endregion

#region InterfaceImplementation
public new bool TryDeserialize(Stream stream, out long bytesConsumed, SerializableOptions options = default)
{
bytesConsumed = 0;
try
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeDogListProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.Name = model.Name;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.FoodConsumed = model.FoodConsumed;
this.RawData = model.RawData;
bytesConsumed = stream.Length;
return true;
}
catch
{
return false;
}
}

public new bool TrySerialize(Stream stream, out long bytesWritten, SerializableOptions options = default)
{
bytesWritten = 0;
try
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
bytesWritten = (int)stream.Length;
return true;
}
catch
{
return false;
}
}
#endregion
}
}
Loading