From 535e26bf7c0b80a28045c997c822a64f96211609 Mon Sep 17 00:00:00 2001 From: Ivan Dimov <78815270+idimov-keeper@users.noreply.github.com> Date: Thu, 7 Sep 2023 18:19:12 -0500 Subject: [PATCH] fixed JSON serializer and tests --- .../JsonUtils.Test.cs | 26 ++++++++++++++++++- sdk/dotNet/SecretsManager/JsonUtils.cs | 6 ++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs b/sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs index 54c6051f..94567399 100644 --- a/sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs +++ b/sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs @@ -7,10 +7,34 @@ public class JsonUtilsTests [Test] public void ParseAndSerializeShouldNotChangeTheData() { - const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[]}"; + var rec = new KeeperRecordData { + type = "Login2", + title = "MyHomeLogin", + notes = "MyNotes", + fields = new KeeperRecordField[] { + new KeeperRecordField { type = "login", label = "Login", value = new string[] { "Login 1" }, required=true, privacyScreen=true }, + new KeeperRecordField { type = "password", label = "Password", value = new string[] { "3[OJ%sc7n].wX6+k5GY)6" }, required=true, privacyScreen=true, enforceGeneration=true, complexity=new PasswordComplexity{ length=21, caps=5, lowercase=5, digits=5, special=5} }, + new KeeperRecordField { type = "url", label = "URL", value = new string[] { "https://asdfjkasdfkdsa.com" }, required=true, privacyScreen=true }, + new KeeperRecordField { type = "securityQuestion", label = "Security Question & Answer", value = new SecurityQuestion[] { new SecurityQuestion { question= "asdf", answer= "asdf" } }, required=true, privacyScreen=true }, + new KeeperRecordField { type = "fileRef", label = "File or Photo", value = new object[] { } }, + new KeeperRecordField { type = "oneTimeCode", label = "Two-Factor Code", value = new object[] { } }, + }, + custom = new KeeperRecordField[] { } + }; + var jsonIn = CryptoUtils.BytesToString(JsonUtils.SerializeJson(rec)); + //const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[],\"notes\":\"MyNotes\"}"; var recordData = JsonUtils.ParseJson(CryptoUtils.StringToBytes(jsonIn)); var jsonOut = CryptoUtils.BytesToString(JsonUtils.SerializeJson(recordData)); Assert.AreEqual(jsonIn, jsonOut); + Assert.AreEqual(recordData.fields[1].value[0].ToString(), rec.fields[1].value[0]); + } + [Test] + public void ParseAndSerializeShouldPreserveDiacritics() + { + string recordTitle = "MySpéciàlHomèL°gin"; + var krdin = new KeeperRecordData { title = recordTitle, type = "login" }; + var krdout = JsonUtils.ParseJson(JsonUtils.SerializeJson(krdin)); + Assert.AreEqual(krdin.title, krdout.title); } } } \ No newline at end of file diff --git a/sdk/dotNet/SecretsManager/JsonUtils.cs b/sdk/dotNet/SecretsManager/JsonUtils.cs index 2fa5d836..30c874a8 100644 --- a/sdk/dotNet/SecretsManager/JsonUtils.cs +++ b/sdk/dotNet/SecretsManager/JsonUtils.cs @@ -8,13 +8,13 @@ public static class JsonUtils { private static readonly JsonSerializerOptions Options = new() { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + //Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // bad for strings with diacritics + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; public static T ParseJson(byte[] json) { - return JsonSerializer.Deserialize(json); + return JsonSerializer.Deserialize(json, Options); } public static byte[] SerializeJson(T obj)