Skip to content

Commit

Permalink
Added support to control json encoding when writing models.
Browse files Browse the repository at this point in the history
  • Loading branch information
vpenades committed May 4, 2021
1 parent 6b4dd2f commit 05d0a5f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void WriteTextSchema2(string baseName, MODEL model)

using (var m = new MemoryStream())
{
model._WriteJSON(m, this.JsonIndented);
model._WriteJSON(m, this.JsonOptions);

WriteAllBytesToEnd($"{baseName}.gltf", m.ToArraySegment());
}
Expand Down
41 changes: 33 additions & 8 deletions src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public WriteSettings(WriteSettings other)

#endregion

#region data

private System.Text.Json.JsonWriterOptions _JsonOptions = default;

#endregion

#region properties

/// <summary>
Expand All @@ -78,7 +84,20 @@ public WriteSettings(WriteSettings other)
/// <summary>
/// Gets or sets a value indicating whether the JSON formatting will include indentation.
/// </summary>
public Boolean JsonIndented { get; set; }
public Boolean JsonIndented
{
get => _JsonOptions.Indented;
set => _JsonOptions.Indented = value;
}

/// <summary>
/// Gets or sets a value indicating the Json options to be used for writing.
/// </summary>
public System.Text.Json.JsonWriterOptions JsonOptions
{
get => _JsonOptions;
set => _JsonOptions = value;
}

/// <summary>
/// Gets or sets a value indicating the level of validation applied when loading a file.
Expand All @@ -96,7 +115,7 @@ public void CopyTo(WriteSettings other)
other.ImageWriting = this.ImageWriting;
other.ImageWriteCallback = this.ImageWriteCallback;
other.MergeBuffers = this.MergeBuffers;
other.JsonIndented = this.JsonIndented;
other._JsonOptions = this._JsonOptions;
other.Validation = this.Validation;
}

Expand Down Expand Up @@ -170,10 +189,20 @@ public void SaveGLTF(string filePath, WriteSettings settings = null)
/// <param name="indented">The formatting of the JSON document.</param>
/// <returns>A JSON content.</returns>
public string GetJSON(bool indented)
{
var options = new System.Text.Json.JsonWriterOptions
{
Indented = indented
};

return GetJSON(options);
}

public string GetJSON(System.Text.Json.JsonWriterOptions options)
{
using (var mm = new System.IO.MemoryStream())
{
_WriteJSON(mm, indented);
_WriteJSON(mm, options);

mm.Position = 0;

Expand Down Expand Up @@ -227,12 +256,8 @@ public void WriteGLB(Stream stream, WriteSettings settings = null)

#region core

internal void _WriteJSON(System.IO.Stream sw, bool indented)
internal void _WriteJSON(System.IO.Stream sw, System.Text.Json.JsonWriterOptions options)
{
System.Text.Json.JsonWriterOptions options = default;

options.Indented = indented;

using (var writer = new System.Text.Json.Utf8JsonWriter(sw, options))
{
this.Serialize(writer);
Expand Down
Binary file added tests/Assets/extended 你好 characters.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions tests/SharpGLTF.Core.Tests/IO/JsonContentTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;

using NUnit.Framework;

Expand Down Expand Up @@ -197,6 +198,57 @@ public void CreateJsonContent()

}

[Category("Core.IO")]
public class JsonSerializationTests
{
const string UNESCAPED = "你好";
const string ESCAPED = "\u4f60\u597d";

[Test]
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);
}
}

[Category("Core.IO")]
public class ContextTests
{
Expand Down

0 comments on commit 05d0a5f

Please sign in to comment.