| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Buffers; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.IO; |
| | 8 | | using System.Text.Json; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | namespace Azure.Data.AppConfiguration |
| | 13 | | { |
| | 14 | | internal static class ConfigurationServiceSerializer |
| | 15 | | { |
| | 16 | | public static void WriteSetting(Utf8JsonWriter writer, ConfigurationSetting setting) |
| | 17 | | { |
| 4 | 18 | | writer.WriteStartObject(); |
| 4 | 19 | | writer.WriteString("key", setting.Key); |
| 4 | 20 | | writer.WriteString("label", setting.Label); |
| | 21 | |
|
| 4 | 22 | | if (setting.IsReadOnly != default) |
| | 23 | | { |
| 4 | 24 | | writer.WriteBoolean("locked", setting.IsReadOnly.Value); |
| | 25 | | } |
| | 26 | |
|
| 4 | 27 | | if (setting.LastModified != default) |
| | 28 | | { |
| 4 | 29 | | writer.WriteString("last_modified", setting.LastModified.Value.ToString(CultureInfo.InvariantCulture)); |
| | 30 | | } |
| | 31 | |
|
| 4 | 32 | | WriteRequestBody(writer, setting); |
| 4 | 33 | | writer.WriteEndObject(); |
| 4 | 34 | | } |
| | 35 | |
|
| | 36 | | public static ReadOnlyMemory<byte> SerializeRequestBody(ConfigurationSetting setting) |
| | 37 | | { |
| 288 | 38 | | var writer = new Core.ArrayBufferWriter<byte>(); |
| 288 | 39 | | using var json = new Utf8JsonWriter(writer); |
| 288 | 40 | | json.WriteStartObject(); |
| 288 | 41 | | WriteRequestBody(json, setting); |
| 288 | 42 | | json.WriteEndObject(); |
| 288 | 43 | | json.Flush(); |
| 288 | 44 | | return writer.WrittenMemory; |
| 288 | 45 | | } |
| | 46 | |
|
| | 47 | | private static void WriteRequestBody(Utf8JsonWriter writer, ConfigurationSetting setting) |
| | 48 | | { |
| 292 | 49 | | writer.WriteString("value", setting.Value); |
| 292 | 50 | | writer.WriteString("content_type", setting.ContentType); |
| 292 | 51 | | if (setting.Tags != null) |
| | 52 | | { |
| 292 | 53 | | writer.WriteStartObject("tags"); |
| 1432 | 54 | | foreach (System.Collections.Generic.KeyValuePair<string, string> tag in setting.Tags) |
| | 55 | | { |
| 424 | 56 | | writer.WriteString(tag.Key, tag.Value); |
| | 57 | | } |
| | 58 | |
|
| 292 | 59 | | writer.WriteEndObject(); |
| | 60 | | } |
| | 61 | |
|
| 292 | 62 | | if (setting.ETag != default) |
| | 63 | | { |
| 20 | 64 | | writer.WriteString("etag", setting.ETag.ToString()); |
| | 65 | | } |
| 292 | 66 | | } |
| | 67 | |
|
| | 68 | | public static ConfigurationSetting ReadSetting(ref Utf8JsonReader reader) |
| | 69 | | { |
| 4 | 70 | | using JsonDocument json = JsonDocument.ParseValue(ref reader); |
| 4 | 71 | | JsonElement root = json.RootElement; |
| 4 | 72 | | return ReadSetting(root); |
| 4 | 73 | | } |
| | 74 | |
|
| | 75 | | private static ConfigurationSetting ReadSetting(JsonElement root) |
| | 76 | | { |
| | 77 | | // TODO (pri 2): make the deserializer version resilient |
| 4392 | 78 | | var setting = new ConfigurationSetting(); |
| 78480 | 79 | | foreach (JsonProperty property in root.EnumerateObject()) |
| | 80 | | { |
| 34848 | 81 | | ReadPropertyValue(setting, property); |
| | 82 | | } |
| 4392 | 83 | | return setting; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | private static void ReadPropertyValue(ConfigurationSetting setting, JsonProperty property) |
| | 87 | | { |
| 34848 | 88 | | if (property.NameEquals("content_type")) |
| | 89 | | { |
| 4384 | 90 | | setting.ContentType = property.Value.GetString(); |
| | 91 | | } |
| 30464 | 92 | | else if (property.NameEquals("etag")) |
| | 93 | | { |
| 4320 | 94 | | setting.ETag = new ETag(property.Value.GetString()); |
| | 95 | | } |
| 26144 | 96 | | else if (property.NameEquals("key")) |
| | 97 | | { |
| 4392 | 98 | | setting.Key = property.Value.GetString(); |
| | 99 | | } |
| 21752 | 100 | | else if (property.NameEquals("label")) |
| | 101 | | { |
| 4388 | 102 | | setting.Label = property.Value.GetString(); |
| | 103 | | } |
| 17364 | 104 | | else if (property.NameEquals("last_modified")) |
| | 105 | | { |
| 4288 | 106 | | if (property.Value.ValueKind == JsonValueKind.Null) |
| | 107 | | { |
| 0 | 108 | | setting.LastModified = null; |
| | 109 | | } |
| | 110 | | else |
| | 111 | | { |
| 4288 | 112 | | setting.LastModified = DateTimeOffset.Parse(property.Value.GetString(), CultureInfo.InvariantCulture |
| | 113 | | } |
| | 114 | | } |
| 13076 | 115 | | else if (property.NameEquals("locked")) |
| | 116 | | { |
| 4308 | 117 | | if (property.Value.ValueKind == JsonValueKind.Null) |
| | 118 | | { |
| 0 | 119 | | setting.IsReadOnly = null; |
| | 120 | | } |
| | 121 | | else |
| | 122 | | { |
| 4308 | 123 | | setting.IsReadOnly = property.Value.GetBoolean(); |
| | 124 | | } |
| | 125 | | } |
| 8768 | 126 | | else if (property.NameEquals("tags")) |
| | 127 | | { |
| 9984 | 128 | | foreach (JsonProperty element in property.Value.EnumerateObject()) |
| | 129 | | { |
| 608 | 130 | | setting.Tags.Add(element.Name, element.Value.GetString()); |
| | 131 | | } |
| | 132 | | } |
| 4384 | 133 | | else if (property.NameEquals("value")) |
| | 134 | | { |
| 4384 | 135 | | setting.Value = property.Value.GetString(); |
| | 136 | | } |
| 8768 | 137 | | } |
| | 138 | |
|
| | 139 | | public static async Task<ConfigurationSetting> DeserializeSettingAsync(Stream content, CancellationToken cancell |
| | 140 | | { |
| 158 | 141 | | using (JsonDocument json = await JsonDocument.ParseAsync(content, default, cancellation).ConfigureAwait(fals |
| | 142 | | { |
| 158 | 143 | | JsonElement root = json.RootElement; |
| 158 | 144 | | return ReadSetting(root); |
| | 145 | | } |
| 158 | 146 | | } |
| | 147 | |
|
| | 148 | | public static ConfigurationSetting DeserializeSetting(Stream content) |
| | 149 | | { |
| 202 | 150 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 202 | 151 | | JsonElement root = json.RootElement; |
| 202 | 152 | | return ReadSetting(root); |
| 202 | 153 | | } |
| | 154 | |
|
| | 155 | | public static async Task<SettingBatch> ParseBatchAsync(Response response, CancellationToken cancellation) |
| | 156 | | { |
| 54 | 157 | | Stream content = response.ContentStream; |
| 54 | 158 | | using (JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).Configure |
| | 159 | | { |
| 54 | 160 | | return ParseSettingBatch(response, json); |
| | 161 | | } |
| 54 | 162 | | } |
| | 163 | |
|
| | 164 | | public static SettingBatch ParseBatch(Response response) |
| | 165 | | { |
| 54 | 166 | | Stream content = response.ContentStream; |
| 54 | 167 | | using (JsonDocument json = JsonDocument.Parse(content)) |
| | 168 | | { |
| 54 | 169 | | return ParseSettingBatch(response, json); |
| | 170 | | } |
| 54 | 171 | | } |
| | 172 | |
|
| | 173 | | private static SettingBatch ParseSettingBatch(Response response, JsonDocument json) |
| | 174 | | { |
| 108 | 175 | | TryGetNextAfterValue(ref response, out string nextBatchUri); |
| | 176 | |
|
| 108 | 177 | | JsonElement itemsArray = json.RootElement.GetProperty("items"); |
| 108 | 178 | | int length = itemsArray.GetArrayLength(); |
| 108 | 179 | | ConfigurationSetting[] settings = new ConfigurationSetting[length]; |
| | 180 | |
|
| 108 | 181 | | int i = 0; |
| 8272 | 182 | | foreach (JsonElement item in itemsArray.EnumerateArray()) |
| | 183 | | { |
| 4028 | 184 | | settings[i++] = ReadSetting(item); |
| | 185 | | } |
| | 186 | |
|
| 108 | 187 | | return new SettingBatch(settings, nextBatchUri); |
| | 188 | | } |
| | 189 | |
|
| | 190 | | private const string Link = "Link"; |
| | 191 | | private const string After = "after="; |
| | 192 | | private static bool TryGetNextAfterValue(ref Response response, out string afterValue) |
| | 193 | | { |
| 108 | 194 | | afterValue = default; |
| 108 | 195 | | if (!response.Headers.TryGetValue(Link, out var headerValue)) |
| 68 | 196 | | return false; |
| | 197 | |
|
| | 198 | | // the headers value is something like this: "</kv?after={token}>; rel=\"next\"" |
| 40 | 199 | | var afterIndex = headerValue.IndexOf(After, StringComparison.Ordinal); |
| 40 | 200 | | if (afterIndex < 0) |
| 0 | 201 | | return false; |
| | 202 | |
|
| 40 | 203 | | int beginingToken = afterIndex + After.Length; |
| 40 | 204 | | int endToken = headerValue.IndexOf(">", StringComparison.Ordinal); |
| 40 | 205 | | int tokenLenght = endToken - beginingToken; |
| 40 | 206 | | afterValue = headerValue.Substring(beginingToken, tokenLenght); |
| 40 | 207 | | return true; |
| | 208 | | } |
| | 209 | | } |
| | 210 | | } |