Skip to content

Commit 97ffd5f

Browse files
authored
JsonModel for protocol models (#53701)
1 parent a5243b2 commit 97ffd5f

File tree

5 files changed

+576
-0
lines changed

5 files changed

+576
-0
lines changed

sdk/core/System.ClientModel/api/System.ClientModel.net8.0.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ public JsonModelConverter(System.ClientModel.Primitives.ModelReaderWriterOptions
263263
public override System.ClientModel.Primitives.IJsonModel<object>? Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
264264
public override void Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.IJsonModel<object> value, System.Text.Json.JsonSerializerOptions options) { }
265265
}
266+
public abstract partial class JsonModel<T> : System.ClientModel.Primitives.IJsonModel<T>, System.ClientModel.Primitives.IPersistableModel<T>
267+
{
268+
protected JsonModel() { }
269+
public T Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
270+
protected abstract T CreateCore(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options);
271+
T System.ClientModel.Primitives.IJsonModel<T>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
272+
void System.ClientModel.Primitives.IJsonModel<T>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
273+
string System.ClientModel.Primitives.IPersistableModel<T>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
274+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<T>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
275+
protected abstract void WriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options);
276+
}
266277
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SCME0001")]
267278
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
268279
public partial struct JsonPatch

sdk/core/System.ClientModel/api/System.ClientModel.net9.0.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ public JsonModelConverter(System.ClientModel.Primitives.ModelReaderWriterOptions
263263
public override System.ClientModel.Primitives.IJsonModel<object>? Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
264264
public override void Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.IJsonModel<object> value, System.Text.Json.JsonSerializerOptions options) { }
265265
}
266+
public abstract partial class JsonModel<T> : System.ClientModel.Primitives.IJsonModel<T>, System.ClientModel.Primitives.IPersistableModel<T>
267+
{
268+
protected JsonModel() { }
269+
public T Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
270+
protected abstract T CreateCore(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options);
271+
T System.ClientModel.Primitives.IJsonModel<T>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
272+
void System.ClientModel.Primitives.IJsonModel<T>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
273+
string System.ClientModel.Primitives.IPersistableModel<T>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
274+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<T>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
275+
protected abstract void WriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options);
276+
}
266277
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SCME0001")]
267278
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
268279
public partial struct JsonPatch

sdk/core/System.ClientModel/api/System.ClientModel.netstandard2.0.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ public JsonModelConverter(System.ClientModel.Primitives.ModelReaderWriterOptions
262262
public override System.ClientModel.Primitives.IJsonModel<object>? Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
263263
public override void Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.IJsonModel<object> value, System.Text.Json.JsonSerializerOptions options) { }
264264
}
265+
public abstract partial class JsonModel<T> : System.ClientModel.Primitives.IJsonModel<T>, System.ClientModel.Primitives.IPersistableModel<T>
266+
{
267+
protected JsonModel() { }
268+
public T Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions? options = null) { throw null; }
269+
protected abstract T CreateCore(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options);
270+
T System.ClientModel.Primitives.IJsonModel<T>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
271+
void System.ClientModel.Primitives.IJsonModel<T>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
272+
string System.ClientModel.Primitives.IPersistableModel<T>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
273+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<T>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
274+
protected abstract void WriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options);
275+
}
265276
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
266277
public partial struct JsonPatch
267278
{
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Text.Json;
5+
6+
namespace System.ClientModel.Primitives;
7+
8+
/// <summary>
9+
/// Helper class for JSON models implementing <see cref="IJsonModel{T}"/>.
10+
/// </summary>
11+
/// <typeparam name="T">The type of the model represented by this JSON model.</typeparam>
12+
public abstract class JsonModel<T> : IJsonModel<T>, IPersistableModel<T>
13+
{
14+
/// <summary>
15+
/// Writes the model to the provided <see cref="Utf8JsonWriter"/>.
16+
/// </summary>
17+
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param>
18+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
19+
protected abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
20+
21+
/// <summary>
22+
/// Reads one JSON value (including objects or arrays) from the provided reader and converts it to a model.
23+
/// </summary>
24+
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
25+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
26+
/// <returns>The model created from the JSON value.</returns>
27+
protected abstract T CreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
28+
29+
#region MRW
30+
T IJsonModel<T>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
31+
{
32+
ValidateFormat(options);
33+
return CreateCore(ref reader, options);
34+
}
35+
36+
/// <summary>
37+
/// Converts the provided <see cref="BinaryData"/> into a model.
38+
/// </summary>
39+
/// <param name="data">The <see cref="BinaryData"/> to parse.</param>
40+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
41+
/// <returns>The model created from the data.</returns>
42+
public T Create(BinaryData data, ModelReaderWriterOptions? options = null)
43+
{
44+
options ??= ModelReaderWriterOptions.Json;
45+
ValidateFormat(options);
46+
47+
var reader = new Utf8JsonReader(data.ToMemory().Span);
48+
return CreateCore(ref reader, options);
49+
}
50+
51+
string IPersistableModel<T>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
52+
53+
void IJsonModel<T>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
54+
{
55+
ValidateFormat(options);
56+
WriteCore(writer, options);
57+
}
58+
59+
BinaryData IPersistableModel<T>.Write(ModelReaderWriterOptions options)
60+
{
61+
ValidateFormat(options);
62+
using MemoryStream stream = new MemoryStream();
63+
using Utf8JsonWriter writer = new Utf8JsonWriter(stream);
64+
WriteCore(writer, options);
65+
writer.Flush();
66+
byte[] buffer = stream.GetBuffer();
67+
ReadOnlyMemory<byte> memory = buffer.AsMemory(0, (int)stream.Position);
68+
return new BinaryData(memory);
69+
}
70+
71+
private static void ValidateFormat(ModelReaderWriterOptions options)
72+
{
73+
if (options.Format != "J" && options.Format != "W")
74+
{
75+
throw new FormatException($"The model does not support '{options.Format}' format. Supported formats are 'J' and 'W'.");
76+
}
77+
}
78+
79+
#endregion
80+
}

0 commit comments

Comments
 (0)