| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Text.Json; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Certificates |
| | 9 | | { |
| | 10 | | internal class ContactList : IJsonDeserializable, IJsonSerializable |
| | 11 | | { |
| | 12 | | private const string ContactsPropertyName = "contacts"; |
| | 13 | |
|
| 2 | 14 | | private static readonly JsonEncodedText s_contactsPropertyNameBytes = JsonEncodedText.Encode(ContactsPropertyNam |
| | 15 | |
|
| | 16 | | private IEnumerable<CertificateContact> _contacts; |
| | 17 | |
|
| 8 | 18 | | public ContactList() |
| | 19 | | { |
| | 20 | |
|
| 8 | 21 | | } |
| | 22 | |
|
| 4 | 23 | | public ContactList(IEnumerable<CertificateContact> contacts) |
| | 24 | | { |
| 4 | 25 | | _contacts = contacts; |
| 4 | 26 | | } |
| | 27 | |
|
| | 28 | | public IList<CertificateContact> ToList() |
| | 29 | | { |
| 8 | 30 | | if (!(_contacts is IList<CertificateContact> ret)) |
| | 31 | | { |
| 0 | 32 | | ret = _contacts.ToList(); |
| | 33 | |
|
| 0 | 34 | | _contacts = ret; |
| | 35 | | } |
| | 36 | |
|
| 8 | 37 | | return ret; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 41 | | { |
| 8 | 42 | | var contacts = new List<CertificateContact>(); |
| | 43 | |
|
| 8 | 44 | | if (json.TryGetProperty(ContactsPropertyName, out JsonElement contactsElement)) |
| | 45 | | { |
| 48 | 46 | | foreach (JsonElement entry in contactsElement.EnumerateArray()) |
| | 47 | | { |
| 16 | 48 | | var contact = new CertificateContact(); |
| | 49 | |
|
| 16 | 50 | | ((IJsonDeserializable)contact).ReadProperties(entry); |
| | 51 | |
|
| 16 | 52 | | contacts.Add(contact); |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| 8 | 56 | | _contacts = contacts; |
| 8 | 57 | | } |
| | 58 | |
|
| | 59 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | 60 | | { |
| 4 | 61 | | if (_contacts != null) |
| | 62 | | { |
| 4 | 63 | | json.WriteStartArray(s_contactsPropertyNameBytes); |
| | 64 | |
|
| 24 | 65 | | foreach (CertificateContact contact in _contacts) |
| | 66 | | { |
| 8 | 67 | | json.WriteStartObject(); |
| | 68 | |
|
| 8 | 69 | | ((IJsonSerializable)contact).WriteProperties(json); |
| | 70 | |
|
| 8 | 71 | | json.WriteEndObject(); |
| | 72 | | } |
| | 73 | |
|
| 4 | 74 | | json.WriteEndArray(); |
| | 75 | | } |
| 4 | 76 | | } |
| | 77 | | } |
| | 78 | | } |