|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System.Collections.Immutable; |
| 6 | +using System.Text.Json; |
6 | 7 | using System.Text.Json.Serialization; |
7 | 8 | using Windows.UI; |
8 | 9 |
|
@@ -135,12 +136,52 @@ public enum DockSide |
135 | 136 | Bottom = 3, |
136 | 137 | } |
137 | 138 |
|
| 139 | +[JsonConverter(typeof(DockSizeJsonConverter))] |
138 | 140 | public enum DockSize |
139 | 141 | { |
140 | 142 | Default, |
141 | 143 | Compact, |
142 | 144 | } |
143 | 145 |
|
| 146 | +/// <summary> |
| 147 | +/// Custom converter for <see cref="DockSize"/> that preserves backward |
| 148 | +/// compatibility with previously-persisted values. Earlier builds shipped a |
| 149 | +/// <c>Small</c>/<c>Medium</c>/<c>Large</c> enum; those values are migrated to |
| 150 | +/// <see cref="DockSize.Default"/> so existing settings.json files continue to |
| 151 | +/// load instead of failing the entire deserialization. |
| 152 | +/// </summary> |
| 153 | +internal sealed class DockSizeJsonConverter : JsonConverter<DockSize> |
| 154 | +{ |
| 155 | + public override DockSize Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 156 | + { |
| 157 | + if (reader.TokenType == JsonTokenType.String) |
| 158 | + { |
| 159 | + var value = reader.GetString(); |
| 160 | + if (Enum.TryParse<DockSize>(value, ignoreCase: true, out var parsed)) |
| 161 | + { |
| 162 | + return parsed; |
| 163 | + } |
| 164 | + |
| 165 | + // Legacy values from the original Small/Medium/Large enum, or any |
| 166 | + // other unknown string — fall back to Default so the user's |
| 167 | + // settings file remains loadable after upgrading. |
| 168 | + return DockSize.Default; |
| 169 | + } |
| 170 | + |
| 171 | + if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var number)) |
| 172 | + { |
| 173 | + return Enum.IsDefined(typeof(DockSize), number) ? (DockSize)number : DockSize.Default; |
| 174 | + } |
| 175 | + |
| 176 | + return DockSize.Default; |
| 177 | + } |
| 178 | + |
| 179 | + public override void Write(Utf8JsonWriter writer, DockSize value, JsonSerializerOptions options) |
| 180 | + { |
| 181 | + writer.WriteStringValue(value.ToString()); |
| 182 | + } |
| 183 | +} |
| 184 | + |
144 | 185 | public enum DockBackdrop |
145 | 186 | { |
146 | 187 | Transparent, |
|
0 commit comments