| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text.Json; |
| | 8 | |
|
| | 9 | | namespace Azure.Security.KeyVault |
| | 10 | | { |
| | 11 | | internal interface IJsonSerializable |
| | 12 | | { |
| | 13 | | void WriteProperties(Utf8JsonWriter json); |
| | 14 | | } |
| | 15 | |
|
| | 16 | | internal interface IJsonDeserializable |
| | 17 | | { |
| | 18 | | void ReadProperties(JsonElement json); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | internal static class SerializationExtensions |
| | 22 | | { |
| | 23 | | public static void Deserialize(this IJsonDeserializable obj, Stream content) |
| | 24 | | { |
| 1286 | 25 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 1286 | 26 | | obj.ReadProperties(json.RootElement); |
| 2572 | 27 | | } |
| | 28 | |
|
| | 29 | | public static ReadOnlyMemory<byte> Serialize(this IJsonSerializable obj) |
| | 30 | | { |
| 1006 | 31 | | var writer = new ArrayBufferWriter<byte>(); |
| | 32 | |
|
| 1006 | 33 | | using (var json = new Utf8JsonWriter(writer)) |
| | 34 | | { |
| 1006 | 35 | | json.WriteStartObject(); |
| | 36 | |
|
| 1006 | 37 | | obj.WriteProperties(json); |
| | 38 | |
|
| 1006 | 39 | | json.WriteEndObject(); |
| 1006 | 40 | | json.Flush(); |
| 1006 | 41 | | } |
| | 42 | |
|
| 1006 | 43 | | return writer.WrittenMemory; |
| | 44 | | } |
| | 45 | | } |
| | 46 | | } |