| | 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 | | { |
| 0 | 25 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 0 | 26 | | obj.ReadProperties(json.RootElement); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public static ReadOnlyMemory<byte> Serialize(this IJsonSerializable obj) |
| | 30 | | { |
| 0 | 31 | | var writer = new ArrayBufferWriter<byte>(); |
| | 32 | |
|
| 0 | 33 | | using (var json = new Utf8JsonWriter(writer)) |
| | 34 | | { |
| 0 | 35 | | json.WriteStartObject(); |
| | 36 | |
|
| 0 | 37 | | obj.WriteProperties(json); |
| | 38 | |
|
| 0 | 39 | | json.WriteEndObject(); |
| 0 | 40 | | json.Flush(); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return writer.WrittenMemory; |
| | 44 | | } |
| | 45 | | } |
| | 46 | | } |