From 8156279025c0e9426643e3d533c56cfc84ea16b4 Mon Sep 17 00:00:00 2001 From: stefansjfw <57057282+stefansjfw@users.noreply.github.com> Date: Wed, 25 Mar 2020 11:11:18 +0100 Subject: [PATCH] Use JsonSerializer instead of Utf8Writer (#1670) * Use JsonSerializer instead of Utf8Writer * Rename method --- .../FancyZonesEditor/DashCaseNamingPolicy.cs | 20 +++ .../FancyZonesEditor/FancyZonesEditor.csproj | 2 + .../Models/CanvasLayoutModel.cs | 101 ++++++++----- .../Models/GridLayoutModel.cs | 108 +++++++------- .../FancyZonesEditor/Models/LayoutModel.cs | 136 +++++++++++------- .../editor/FancyZonesEditor/StringUtils.cs | 22 +++ 6 files changed, 246 insertions(+), 143 deletions(-) create mode 100644 src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs create mode 100644 src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs b/src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs new file mode 100644 index 0000000000..b88f25d7e4 --- /dev/null +++ b/src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json; + +using FancyZonesEditor.Utils; + +namespace FancyZonesEditor +{ + public class DashCaseNamingPolicy : JsonNamingPolicy + { + public static DashCaseNamingPolicy Instance { get; } = new DashCaseNamingPolicy(); + + public override string ConvertName(string name) + { + return name.UpperCamelCaseToDashCase(); + } + } +} diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj index b82ed2e428..9c450afcff 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj +++ b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj @@ -110,6 +110,8 @@ MSBuild:Compile Designer + + diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs index 415656ac7b..4e876e281c 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs @@ -122,48 +122,77 @@ namespace FancyZonesEditor.Models } } + private struct Zone + { + public int X { get; set; } + + public int Y { get; set; } + + public int Width { get; set; } + + public int Height { get; set; } + } + + private struct CanvasLayoutInfo + { + public int RefWidth { get; set; } + + public int RefHeight { get; set; } + + public Zone[] Zones { get; set; } + } + + private struct CanvasLayoutJson + { + public string Uuid { get; set; } + + public string Name { get; set; } + + public string Type { get; set; } + + public CanvasLayoutInfo Info { get; set; } + } + // PersistData // Implements the LayoutModel.PersistData abstract method protected override void PersistData() { + CanvasLayoutInfo layoutInfo = new CanvasLayoutInfo + { + RefWidth = _referenceWidth, + RefHeight = _referenceHeight, + Zones = new Zone[Zones.Count], + }; + for (int i = 0; i < Zones.Count; ++i) + { + Zone zone = new Zone + { + X = Zones[i].X, + Y = Zones[i].Y, + Width = Zones[i].Width, + Height = Zones[i].Height, + }; + + layoutInfo.Zones[i] = zone; + } + + CanvasLayoutJson jsonObj = new CanvasLayoutJson + { + Uuid = "{" + Guid.ToString().ToUpper() + "}", + Name = Name, + Type = "canvas", + Info = layoutInfo, + }; + + JsonSerializerOptions options = new JsonSerializerOptions + { + PropertyNamingPolicy = new DashCaseNamingPolicy(), + }; + try { - FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create); - using (var writer = new Utf8JsonWriter(outputStream, options: default)) - { - writer.WriteStartObject(); - writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}"); - writer.WriteString("name", Name); - - writer.WriteString("type", "canvas"); - - writer.WriteStartObject("info"); - - writer.WriteNumber("ref-width", _referenceWidth); - writer.WriteNumber("ref-height", _referenceHeight); - - writer.WriteStartArray("zones"); - foreach (Int32Rect rect in Zones) - { - writer.WriteStartObject(); - writer.WriteNumber("X", rect.X); - writer.WriteNumber("Y", rect.Y); - writer.WriteNumber("width", rect.Width); - writer.WriteNumber("height", rect.Height); - writer.WriteEndObject(); - } - - writer.WriteEndArray(); - - // end info object - writer.WriteEndObject(); - - // end root object - writer.WriteEndObject(); - writer.Flush(); - } - - outputStream.Close(); + string jsonString = JsonSerializer.Serialize(jsonObj, options); + File.WriteAllText(Settings.AppliedZoneSetTmpFile, jsonString); } catch (Exception ex) { diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs index 89a5a26098..806fd7c5af 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs @@ -171,65 +171,67 @@ namespace FancyZonesEditor.Models layout.ColumnPercents = colPercents; } + private struct GridLayoutInfo + { + public int Rows { get; set; } + + public int Columns { get; set; } + + public int[] RowsPercentage { get; set; } + + public int[] ColumnsPercentage { get; set; } + + public int[][] CellChildMap { get; set; } + } + + private struct GridLayoutJson + { + public string Uuid { get; set; } + + public string Name { get; set; } + + public string Type { get; set; } + + public GridLayoutInfo Info { get; set; } + } + // PersistData // Implements the LayoutModel.PersistData abstract method protected override void PersistData() { + GridLayoutInfo layoutInfo = new GridLayoutInfo + { + Rows = Rows, + Columns = Columns, + RowsPercentage = RowPercents, + ColumnsPercentage = ColumnPercents, + CellChildMap = new int[Rows][], + }; + for (int row = 0; row < Rows; row++) + { + layoutInfo.CellChildMap[row] = new int[Columns]; + for (int col = 0; col < Columns; col++) + { + layoutInfo.CellChildMap[row][col] = CellChildMap[row, col]; + } + } + + GridLayoutJson jsonObj = new GridLayoutJson + { + Uuid = "{" + Guid.ToString().ToUpper() + "}", + Name = Name, + Type = "grid", + Info = layoutInfo, + }; + JsonSerializerOptions options = new JsonSerializerOptions + { + PropertyNamingPolicy = new DashCaseNamingPolicy(), + }; + try { - FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create); - using (var writer = new Utf8JsonWriter(outputStream, options: default)) - { - writer.WriteStartObject(); - writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}"); - writer.WriteString("name", Name); - - writer.WriteString("type", "grid"); - - writer.WriteStartObject("info"); - - writer.WriteNumber("rows", Rows); - writer.WriteNumber("columns", Columns); - - writer.WriteStartArray("rows-percentage"); - for (int row = 0; row < Rows; row++) - { - writer.WriteNumberValue(RowPercents[row]); - } - - writer.WriteEndArray(); - - writer.WriteStartArray("columns-percentage"); - for (int col = 0; col < Columns; col++) - { - writer.WriteNumberValue(ColumnPercents[col]); - } - - writer.WriteEndArray(); - - writer.WriteStartArray("cell-child-map"); - for (int row = 0; row < Rows; row++) - { - writer.WriteStartArray(); - for (int col = 0; col < Columns; col++) - { - writer.WriteNumberValue(CellChildMap[row, col]); - } - - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - // end info object - writer.WriteEndObject(); - - // end root object - writer.WriteEndObject(); - writer.Flush(); - } - - outputStream.Close(); + string jsonString = JsonSerializer.Serialize(jsonObj, options); + File.WriteAllText(Settings.AppliedZoneSetTmpFile, jsonString); } catch (Exception ex) { diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs index 846c140f8e..515b76c3dd 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs @@ -135,23 +135,27 @@ namespace FancyZonesEditor.Models } } + private struct DeletedCustomZoneSetsWrapper + { + public List DeletedCustomZoneSets { get; set; } + } + public static void SerializeDeletedCustomZoneSets() { + DeletedCustomZoneSetsWrapper deletedLayouts = new DeletedCustomZoneSetsWrapper + { + DeletedCustomZoneSets = _deletedCustomModels, + }; + + JsonSerializerOptions options = new JsonSerializerOptions + { + PropertyNamingPolicy = new DashCaseNamingPolicy(), + }; + try { - FileStream outputStream = File.Open(Settings.CustomZoneSetsTmpFile, FileMode.Create); - var writer = new Utf8JsonWriter(outputStream, options: default); - writer.WriteStartObject(); - writer.WriteStartArray("deleted-custom-zone-sets"); - foreach (string zoneSet in _deletedCustomModels) - { - writer.WriteStringValue(zoneSet); - } - - writer.WriteEndArray(); - writer.WriteEndObject(); - writer.Flush(); - outputStream.Close(); + string jsonString = JsonSerializer.Serialize(deletedLayouts, options); + File.WriteAllText(Settings.CustomZoneSetsTmpFile, jsonString); } catch (Exception ex) { @@ -258,51 +262,75 @@ namespace FancyZonesEditor.Models Apply(); } + private struct ActiveZoneSetWrapper + { + public string Uuid { get; set; } + + public string Type { get; set; } + } + + private struct AppliedZoneSet + { + public string DeviceId { get; set; } + + public ActiveZoneSetWrapper ActiveZoneset { get; set; } + + public bool EditorShowSpacing { get; set; } + + public int EditorSpacing { get; set; } + + public int EditorZoneCount { get; set; } + } + public void Apply() { + ActiveZoneSetWrapper activeZoneSet = new ActiveZoneSetWrapper + { + Uuid = "{" + Guid.ToString().ToUpper() + "}", + }; + + switch (Type) + { + case LayoutType.Focus: + activeZoneSet.Type = "focus"; + break; + case LayoutType.Rows: + activeZoneSet.Type = "rows"; + break; + case LayoutType.Columns: + activeZoneSet.Type = "columns"; + break; + case LayoutType.Grid: + activeZoneSet.Type = "grid"; + break; + case LayoutType.PriorityGrid: + activeZoneSet.Type = "priority-grid"; + break; + case LayoutType.Custom: + activeZoneSet.Type = "custom"; + break; + } + + Settings settings = ((App)Application.Current).ZoneSettings; + + AppliedZoneSet zoneSet = new AppliedZoneSet + { + DeviceId = Settings.UniqueKey, + ActiveZoneset = activeZoneSet, + EditorShowSpacing = settings.ShowSpacing, + EditorSpacing = settings.Spacing, + EditorZoneCount = settings.ZoneCount, + }; + + JsonSerializerOptions options = new JsonSerializerOptions + { + PropertyNamingPolicy = new DashCaseNamingPolicy(), + }; + try { - FileStream outputStream = File.Open(Settings.ActiveZoneSetTmpFile, FileMode.Create); - var writer = new Utf8JsonWriter(outputStream, options: default); - - writer.WriteStartObject(); - writer.WriteString("device-id", Settings.UniqueKey); - - writer.WriteStartObject("active-zoneset"); - writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}"); - switch (Type) - { - case LayoutType.Focus: - writer.WriteString("type", "focus"); - break; - case LayoutType.Rows: - writer.WriteString("type", "rows"); - break; - case LayoutType.Columns: - writer.WriteString("type", "columns"); - break; - case LayoutType.Grid: - writer.WriteString("type", "grid"); - break; - case LayoutType.PriorityGrid: - writer.WriteString("type", "priority-grid"); - break; - case LayoutType.Custom: - writer.WriteString("type", "custom"); - break; - } - - writer.WriteEndObject(); - - Settings settings = ((App)Application.Current).ZoneSettings; - - writer.WriteBoolean("editor-show-spacing", settings.ShowSpacing); - writer.WriteNumber("editor-spacing", settings.Spacing); - writer.WriteNumber("editor-zone-count", settings.ZoneCount); - - writer.WriteEndObject(); - writer.Flush(); - outputStream.Close(); + string jsonString = JsonSerializer.Serialize(zoneSet, options); + File.WriteAllText(Settings.ActiveZoneSetTmpFile, jsonString); } catch (Exception ex) { diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs b/src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs new file mode 100644 index 0000000000..8dc4837d54 --- /dev/null +++ b/src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Linq; + +namespace FancyZonesEditor.Utils +{ + public static class StringUtils + { + public static string UpperCamelCaseToDashCase(this string str) + { + // If it's single letter variable, leave it as it is + if (str.Length == 1) + { + return str; + } + + return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "-" + x.ToString() : x.ToString())).ToLower(); + } + } +}