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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IModelJsonSerializable<out T> : IModelSerializable<T>
/// </summary>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to serialize into.</param>
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="InvalidOperationException">If <see cref="ModelSerializerFormat.Wire"/> format is passed in and the model does not use JSON for its wire format.</exception>
#pragma warning disable AZC0014 // Avoid using banned types in public API
void Serialize(Utf8JsonWriter writer, ModelSerializerOptions options);
Expand All @@ -29,7 +29,7 @@ public interface IModelJsonSerializable<out T> : IModelSerializable<T>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read.</param>
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the JSON value.</returns>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="InvalidOperationException">If <see cref="ModelSerializerFormat.Wire"/> format is passed in and the model does not use JSON for its wire format.</exception>
#pragma warning disable AZC0014 // Avoid using banned types in public API
T Deserialize(ref Utf8JsonReader reader, ModelSerializerOptions options);
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/Azure.Core/src/Serialization/IModelSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IModelSerializable<out T>
/// </summary>
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A binary representation of the serialized model.</returns>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
BinaryData Serialize(ModelSerializerOptions options);

/// <summary>
Expand All @@ -26,7 +26,7 @@ public interface IModelSerializable<out T>
/// <param name="data">The <see cref="BinaryData"/> to parse.</param>
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the data.</returns>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
T Deserialize(BinaryData data, ModelSerializerOptions options);
}
}
16 changes: 8 additions & 8 deletions sdk/core/Azure.Core/src/Serialization/ModelSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class ModelSerializer
/// <param name="model">The model to convert.</param>
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A <see cref="BinaryData"/> representation of the model in the <see cref="ModelSerializerFormat"/> specified by the <paramref name="options"/>.</returns>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
public static BinaryData Serialize<T>(T model, ModelSerializerOptions? options = default) where T : IModelSerializable<T>
{
options ??= ModelSerializerOptions.DefaultWireOptions;
Expand All @@ -33,7 +33,7 @@ public static BinaryData Serialize<T>(T model, ModelSerializerOptions? options =
/// <param name="model">The model to convert.</param>
/// <param name="format">The <see cref="ModelSerializerFormat"/> to use.</param>
/// <returns>A <see cref="BinaryData"/> representation of the model in the <see cref="ModelSerializerFormat"/> specified by the <paramref name="format"/>.</returns>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
public static BinaryData Serialize<T>(T model, ModelSerializerFormat format)
where T : IModelSerializable<T>
=> Serialize<T>(model, ModelSerializerOptions.GetOptions(format));
Expand All @@ -45,7 +45,7 @@ public static BinaryData Serialize<T>(T model, ModelSerializerFormat format)
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A <see cref="BinaryData"/> representation of the model in the <see cref="ModelSerializerFormat"/> specified by the <paramref name="options"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <paramref name="model"/> does not implement <see cref="IModelSerializable{T}"/>.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
public static BinaryData Serialize(object model, ModelSerializerOptions? options = default)
{
options ??= ModelSerializerOptions.DefaultWireOptions;
Expand All @@ -66,7 +66,7 @@ public static BinaryData Serialize(object model, ModelSerializerOptions? options
/// <param name="format">The <see cref="ModelSerializerFormat"/> to use.</param>
/// <returns>A <see cref="BinaryData"/> representation of the model in the <see cref="ModelSerializerFormat"/> specified by the <paramref name="format"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <paramref name="model"/> does not implement <see cref="IModelSerializable{T}"/>.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
public static BinaryData Serialize(object model, ModelSerializerFormat format)
=> Serialize(model, ModelSerializerOptions.GetOptions(format));

Expand All @@ -77,7 +77,7 @@ public static BinaryData Serialize(object model, ModelSerializerFormat format)
/// <param name="options">The <see cref="ModelSerializerOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the <see cref="BinaryData"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <typeparamref name="T"/> does not have a public or internal parameterless constructor.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
public static T Deserialize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(BinaryData data, ModelSerializerOptions? options = default) where T : IModelSerializable<T>
{
options ??= ModelSerializerOptions.DefaultWireOptions;
Expand All @@ -92,7 +92,7 @@ public static BinaryData Serialize(object model, ModelSerializerFormat format)
/// <param name="format">The <see cref="ModelSerializerFormat"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the <see cref="BinaryData"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <typeparamref name="T"/> does not have a public or internal parameterless constructor.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
public static T Deserialize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(BinaryData data, ModelSerializerFormat format)
where T : IModelSerializable<T>
=> Deserialize<T>(data, ModelSerializerOptions.GetOptions(format));
Expand All @@ -106,7 +106,7 @@ public static BinaryData Serialize(object model, ModelSerializerFormat format)
/// <returns>A <paramref name="returnType"/> representation of the <see cref="BinaryData"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <paramref name="returnType"/> does not implement <see cref="IModelSerializable{T}"/>.</exception>
/// <exception cref="InvalidOperationException">Throws if <paramref name="returnType"/> does not have a public or internal parameterless constructor.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerOptions.Format"/>.</exception>
public static object Deserialize(BinaryData data, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type returnType, ModelSerializerOptions? options = default)
{
options ??= ModelSerializerOptions.DefaultWireOptions;
Expand All @@ -123,7 +123,7 @@ public static object Deserialize(BinaryData data, [DynamicallyAccessedMembers(Dy
/// <returns>A <paramref name="returnType"/> representation of the <see cref="BinaryData"/>.</returns>
/// <exception cref="InvalidOperationException">Throws if <paramref name="returnType"/> does not implement <see cref="IModelSerializable{T}"/>.</exception>
/// <exception cref="InvalidOperationException">Throws if <paramref name="returnType"/> does not have a public or internal parameterless constructor.</exception>
/// <exception cref="NotSupportedException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelSerializerFormat"/>.</exception>
public static object Deserialize(BinaryData data, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type returnType, ModelSerializerFormat format)
=> Deserialize(data, returnType, ModelSerializerOptions.GetOptions(format));

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/Shared/ModelSerializerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void ValidateFormat<T>(IModelSerializable<T> model, ModelSerialize
bool isValid = (format == ModelSerializerFormat.Json && implementsJson) || format == ModelSerializerFormat.Wire;
if (!isValid)
{
throw new NotSupportedException($"The model {model.GetType().Name} does not support '{format}' format.");
throw new FormatException($"The model {model.GetType().Name} does not support '{format}' format.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ThrowsIfUnsupportedFormat()
{
ModelXml model = ModelSerializer.Deserialize<ModelXml>(BinaryData.FromString(File.ReadAllText(TestData.GetLocation("ModelXml.xml"))));
ModelWriter writer = new ModelWriter(model, new ModelSerializerOptions("x"));
Assert.Throws<NotSupportedException>(() => writer.ToBinaryData());
Assert.Throws<FormatException>(() => writer.ToBinaryData());
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private bool AssertFailures(RoundTripStrategy<T> strategy, ModelSerializerFormat
}
else if (ModelInstance is not IModelJsonSerializable<T> && format == ModelSerializerFormat.Json)
{
Assert.Throws<NotSupportedException>(() => { T model = (T)strategy.Deserialize(serviceResponse, ModelInstance, options); });
Assert.Throws<NotSupportedException>(() => { var data = strategy.Serialize(ModelInstance, options); });
Assert.Throws<FormatException>(() => { T model = (T)strategy.Deserialize(serviceResponse, ModelInstance, options); });
Assert.Throws<FormatException>(() => { var data = strategy.Serialize(ModelInstance, options); });
result = true;
}
return result;
Expand All @@ -144,22 +144,22 @@ internal static Dictionary<string, BinaryData> GetRawData(object model)
public void ThrowsIfUnknownFormat()
{
ModelSerializerOptions options = new ModelSerializerOptions("x");
Assert.Throws<NotSupportedException>(() => ModelSerializer.Serialize(ModelInstance, options));
Assert.Throws<NotSupportedException>(() => ModelSerializer.Deserialize<T>(new BinaryData("x"), options));
Assert.Throws<FormatException>(() => ModelSerializer.Serialize(ModelInstance, options));
Assert.Throws<FormatException>(() => ModelSerializer.Deserialize<T>(new BinaryData("x"), options));

Assert.Throws<NotSupportedException>(() => ModelSerializer.Serialize((IModelSerializable<object>)ModelInstance, options));
Assert.Throws<NotSupportedException>(() => ModelSerializer.Deserialize(new BinaryData("x"), typeof(T), options));
Assert.Throws<FormatException>(() => ModelSerializer.Serialize((IModelSerializable<object>)ModelInstance, options));
Assert.Throws<FormatException>(() => ModelSerializer.Deserialize(new BinaryData("x"), typeof(T), options));
if (ModelInstance is IModelJsonSerializable<T> jsonModel)
{
Assert.Throws<NotSupportedException>(() => jsonModel.Serialize(new Utf8JsonWriter(new MemoryStream()), options));
Assert.Throws<NotSupportedException>(() => ((IModelJsonSerializable<object>)jsonModel).Serialize(new Utf8JsonWriter(new MemoryStream()), options));
Assert.Throws<FormatException>(() => jsonModel.Serialize(new Utf8JsonWriter(new MemoryStream()), options));
Assert.Throws<FormatException>(() => ((IModelJsonSerializable<object>)jsonModel).Serialize(new Utf8JsonWriter(new MemoryStream()), options));
bool gotException = false;
try
{
Utf8JsonReader reader = default;
jsonModel.Deserialize(ref reader, options);
}
catch (NotSupportedException)
catch (FormatException)
{
gotException = true;
}
Expand All @@ -174,7 +174,7 @@ public void ThrowsIfUnknownFormat()
Utf8JsonReader reader = default;
((IModelJsonSerializable<object>)jsonModel).Deserialize(ref reader, options);
}
catch (NotSupportedException)
catch (FormatException)
{
gotException = true;
}
Expand Down