Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generated serializer, fix tests #341

Merged
merged 4 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static string AliciaPath
{
get
{
return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.40/AliciaSolid_vrm-0.40.vrm")
return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm")
.Replace("\\", "/");
}
}
Expand Down Expand Up @@ -139,15 +139,15 @@ public void SerializerCompare()
File.WriteAllText("new.json", newJson);

// 比較
Assert.AreEqual(oldJson, newJson);
Assert.AreEqual(oldJson.ParseAsJson().ToString(), newJson.ParseAsJson().ToString());

// 生成デシリアライザでロードする
var ff = new JsonFormatter();
var des = GltfDeserializer.Deserialize(parsed);
ff.Clear();
ff.GenSerialize(des);
var desJson = ff.ToString().ParseAsJson().ToString(" ");
Assert.AreEqual(oldJson, desJson);
Assert.AreEqual(oldJson.ParseAsJson().ToString(), desJson.ParseAsJson().ToString());
}
}
}
2 changes: 1 addition & 1 deletion Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public void SameMeshButDifferentMaterialExport()
{
var context = new ImporterContext
{
UseUniJSONParser = true
SerializerType = SerializerTypes.UniJSON
};
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
Expand Down
9 changes: 9 additions & 0 deletions Assets/VRM/UniGLTF/Scripts/Format/SerializerTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace UniGLTF
{
public enum SerializerTypes
{
JsonSerializable, // manual, Obsolete
UniJSON, // reflection
Generated, // generated, experimental for mobile
}
}
11 changes: 11 additions & 0 deletions Assets/VRM/UniGLTF/Scripts/Format/SerializerTypes.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions Assets/VRM/UniGLTF/Scripts/Format/glTF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,21 +468,32 @@ string RemoveUnusedExtensions(string json)
return f.ToString();
}

public byte[] ToGlbBytes(bool UseUniJSONSerializer = false)
public byte[] ToGlbBytes(SerializerTypes serializer = SerializerTypes.UniJSON)
{
string json;
if (UseUniJSONSerializer)
if (serializer == SerializerTypes.UniJSON)
{
var c = new JsonSchemaValidationContext(this)
{
EnableDiagnosisForNotRequiredFields = true,
};
json = JsonSchema.FromType(GetType()).Serialize(this, c);
}
else
else if (serializer == SerializerTypes.Generated)
{
var f = new JsonFormatter();
f.GenSerialize(this);
json = f.ToString().ParseAsJson().ToString(" ");
}
else if(serializer == SerializerTypes.JsonSerializable)
{
// Obsolete
json = ToJson();
}
else
{
throw new Exception("[UniVRM Export Error] unknown serializer type");
}

RemoveUnusedExtensions(json);

Expand Down
13 changes: 10 additions & 3 deletions Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,25 @@ public void ParseGlb(Byte[] bytes)
new SimpleStorage(chunks[1].Bytes));
}

public bool UseUniJSONParser;
private SerializerTypes _serializerType = SerializerTypes.UniJSON;
public SerializerTypes SerializerType { get { return _serializerType; } set { _serializerType = value; } }

public virtual void ParseJson(string json, IStorage storage)
{
Json = json;
Storage = storage;

if (UseUniJSONParser)
if (_serializerType == SerializerTypes.UniJSON)
{
Json.ParseAsJson().Deserialize(ref GLTF);
}
else
else if (_serializerType == SerializerTypes.Generated)
{
GLTF = GltfDeserializer.Deserialize(json.ParseAsJson());
}
else if (_serializerType == SerializerTypes.JsonSerializable)
{
// Obsolete
GLTF = JsonUtility.FromJson<glTF>(Json);
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/UniVRM/Scripts/Format/VRMExportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void Export(string path, List<GameObject> destroy)
vrm.extensions.VRM.meta.title = Title;
vrm.extensions.VRM.meta.author = Author;

var bytes = vrm.ToGlbBytes(UseExperimentalExporter);
var bytes = vrm.ToGlbBytes(UseExperimentalExporter?SerializerTypes.Generated:SerializerTypes.UniJSON);
File.WriteAllBytes(path, bytes);
Debug.LogFormat("Export elapsed {0}", sw.Elapsed);
}
Expand Down