JsonContentTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Encodings.Web;
  5. using NUnit.Framework;
  6. namespace SharpGLTF.IO
  7. {
  8. class _TestStructure
  9. {
  10. public string Author { get; set; }
  11. public int Integer1 { get; set; }
  12. public bool Bool1 { get; set; }
  13. public float Single1 { get; set; }
  14. public float Single2 { get; set; }
  15. public float Single3 { get; set; }
  16. // public float SinglePI { get; set; } // Fails on .Net Framework 471
  17. public double Double1 { get; set; }
  18. public double Double2 { get; set; }
  19. public double Double3 { get; set; }
  20. public double DoublePI { get; set; }
  21. public List<int> Array1 { get; set; }
  22. public List<float> Array2 { get; set; }
  23. public List<int[]> Array3 { get; set; }
  24. public List<_TestStructure2> Array4 { get; set; }
  25. public _TestStructure2 Dict1 { get; set; }
  26. public _TestStructure3 Dict2 { get; set; }
  27. public static Dictionary<string,object> CreateCompatibleDictionary()
  28. {
  29. var dict = new Dictionary<string, Object>();
  30. dict["author"] = "me";
  31. dict["integer1"] = 17;
  32. dict["bool1"] = true;
  33. dict["single1"] = 15.3f;
  34. dict["single2"] = 1.1f;
  35. dict["single3"] = -1.1f;
  36. // dict["singlePI"] = (float)Math.PI; // Fails on .Net Framework 471
  37. dict["double1"] = 15.3;
  38. dict["double2"] = 1.1;
  39. dict["double3"] = -1.1;
  40. dict["doublePI"] = Math.PI;
  41. dict["array1"] = new int[] { 1, 2, 3 };
  42. dict["array2"] = new float[] { 1.1f, 2.2f, 3.3f };
  43. dict["array3"] = new object[]
  44. {
  45. new int[] { 1,2,3 },
  46. new int[] { 4,5,6 }
  47. };
  48. dict["array4"] = new object[]
  49. {
  50. new Dictionary<string, int> { ["a0"] = 5, ["a1"] = 6 },
  51. new Dictionary<string, int> { ["a0"] = 7, ["a1"] = 8 }
  52. };
  53. dict["dict1"] = new Dictionary<string, int> { ["a0"] = 2, ["a1"] = 3 };
  54. dict["dict2"] = new Dictionary<string, Object>
  55. {
  56. ["a"] = 16,
  57. ["b"] = "delta",
  58. ["c"] = new List<int>() { 4, 6, 7 },
  59. ["d"] = new Dictionary<string, int> { ["a0"] = 1, ["a1"] = 2 }
  60. };
  61. if (!JsonContentTests.IsJsonRoundtripReady)
  62. {
  63. dict["array2"] = new float[] { 1, 2, 3 };
  64. }
  65. return dict;
  66. }
  67. }
  68. struct _TestStructure2
  69. {
  70. public int A0 { get; set; }
  71. public int A1 { get; set; }
  72. }
  73. class _TestStructure3
  74. {
  75. public int A { get; set; }
  76. public string B { get; set; }
  77. public int[] C { get; set; }
  78. public _TestStructure2 D { get; set; }
  79. }
  80. [Category("Core.IO")]
  81. public class JsonContentTests
  82. {
  83. public static bool IsJsonRoundtripReady
  84. {
  85. get
  86. {
  87. // when serializing a JsonContent object, it's important to take into account floating point values roundtrips.
  88. // it seems that prior NetCore3.1, System.Text.JSon was not roundtrip ready, so some values might have some
  89. // error margin when they complete a roundtrip.
  90. // On newer, NetCore System.Text.Json versions, it seems to use "G9" and "G17" text formatting are used.
  91. // https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/
  92. // https://github.com/dotnet/runtime/blob/76904319b41a1dd0823daaaaae6e56769ed19ed3/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs#L101
  93. // pull requests:
  94. // https://github.com/dotnet/corefx/pull/40408
  95. // https://github.com/dotnet/corefx/pull/38322
  96. // https://github.com/dotnet/corefx/pull/32268
  97. var framework = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
  98. return !framework.StartsWith(".NET Framework 4");
  99. }
  100. }
  101. public static bool AreEqual(System.Text.Json.Nodes.JsonNode a, System.Text.Json.Nodes.JsonNode b)
  102. {
  103. if (Object.ReferenceEquals(a, b)) return true;
  104. if (Object.ReferenceEquals(a, null)) return false;
  105. if (Object.ReferenceEquals(b, null)) return false;
  106. // A JsonContent ultimately represents a json block, so it seems fit to do the comparison that way.
  107. // also, there's the problem of floating point json writing, that can slightly change between
  108. // different frameworks.
  109. var ajson = a.ToJsonString();
  110. var bjson = b.ToJsonString();
  111. if (ajson == bjson) return true;
  112. // Net6.0 has better roundtrip handling that netstandard/net4
  113. #if NET6_0_OR_GREATER
  114. return false;
  115. #endif
  116. return true;
  117. }
  118. }
  119. [Category("Core.IO")]
  120. public class JsonSerializationTests
  121. {
  122. const string UNESCAPED = "你好";
  123. const string ESCAPED = "\u4f60\u597d";
  124. [Test]
  125. public void TestJsonExtendedCharacters()
  126. {
  127. TestContext.CurrentContext.AttachGltfValidatorLinks();
  128. // create a test model
  129. var model = Schema2.ModelRoot.CreateModel();
  130. var extras = new System.Text.Json.Nodes.JsonArray();
  131. extras.Add(UNESCAPED);
  132. extras.Add(ESCAPED);
  133. extras.Add(UNESCAPED);
  134. model.Asset.Copyright = UNESCAPED;
  135. model.UseScene(UNESCAPED);
  136. model.Asset.Extras = extras;
  137. model.CreateImage().Content = Memory.MemoryImage.DefaultPngImage;
  138. // create write settings
  139. var joptions = new System.Text.Json.JsonWriterOptions
  140. {
  141. Indented = true,
  142. Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  143. };
  144. var wsettings = new Schema2.WriteSettings();
  145. wsettings.JsonOptions = joptions;
  146. // model save-load roundtrip
  147. var roundtripPath = model.AttachToCurrentTest("extended 你好 characters.gltf", wsettings);
  148. var roundtripJson = System.IO.File.ReadAllText(roundtripPath);
  149. var roundtripModel = Schema2.ModelRoot.Load(roundtripPath);
  150. // checks
  151. TestContext.WriteLine(roundtripJson);
  152. Assert.That(roundtripJson, Does.Contain("你好"));
  153. // https://github.com/KhronosGroup/glTF/issues/1978#issuecomment-831744624
  154. Assert.That(roundtripJson, Does.Contain("extended%20%E4%BD%A0%E5%A5%BD%20characters.png"));
  155. Assert.That(roundtripModel.LogicalImages[0].Content.IsPng, Is.True);
  156. }
  157. }
  158. }