public void CreateJsonContent() { JsonContent a = JsonContent.CreateFrom(_TestStructure.CreateCompatibleDictionary()); // roundtrip to json var json = a.ToJson(); TestContext.Write(json); var b = IO.JsonContent.Parse(json); // roundtrip to a runtime object var x = a.Deserialize(typeof(_TestStructure)); var c = JsonContent.Serialize(x); Assert.IsTrue(AreEqual(a, b)); Assert.IsTrue(AreEqual(a, c)); foreach (var dom in new[] { a, b, c }) { Assert.AreEqual("me", dom.GetValue <string>("author")); Assert.AreEqual(17, dom.GetValue <int>("integer1")); Assert.AreEqual(15.3f, dom.GetValue <float>("single1")); Assert.AreEqual(3, dom.GetValue <int>("array1", 2)); Assert.AreEqual(2, dom.GetValue <int>("dict2", "d", "a1")); } }
public void TestJsonExtendedCharacters() { TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); // create a test model var model = Schema2.ModelRoot.CreateModel(); model.Asset.Copyright = UNESCAPED; model.UseScene(UNESCAPED); model.Asset.Extras = JsonContent.CreateFrom(new string[] { UNESCAPED, ESCAPED, UNESCAPED }); model.CreateImage().Content = Memory.MemoryImage.DefaultPngImage; // create write settings var joptions = new System.Text.Json.JsonWriterOptions { Indented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; var wsettings = new Schema2.WriteSettings(); wsettings.JsonOptions = joptions; // model save-load roundtrip var roundtripPath = model.AttachToCurrentTest("extended 你好 characters.gltf", wsettings); var roundtripJson = System.IO.File.ReadAllText(roundtripPath); var roundtripModel = Schema2.ModelRoot.Load(roundtripPath); // checks TestContext.WriteLine(roundtripJson); Assert.IsTrue(roundtripJson.Contains("你好")); // https://github.com/KhronosGroup/glTF/issues/1978#issuecomment-831744624 Assert.IsTrue(roundtripJson.Contains("extended%20%E4%BD%A0%E5%A5%BD%20characters.png")); Assert.IsTrue(roundtripModel.LogicalImages[0].Content.IsPng); }
public void TestFloatingPointJsonRoundtrip() { float value = 1.1f; // serialized by system.text.json as 1.1000002f var valueTxt = value.ToString("G9", System.Globalization.CultureInfo.InvariantCulture); var dict = new Dictionary <string, Object>(); dict["value"] = value; JsonContent a = JsonContent.CreateFrom(dict); // roundtrip to json var json = a.ToJson(); TestContext.Write(json); var b = IO.JsonContent.Parse(json); Assert.IsTrue(AreEqual(a, b)); }