Merge pull request #341 from hiroj/add_generated_serializer

add generated serializer, fix tests
This commit is contained in:
ousttrue
2019-12-16 19:06:19 +09:00
committed by GitHub
7 changed files with 49 additions and 11 deletions

View File

@@ -25,7 +25,7 @@ namespace VRM.Samples
{
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("\\", "/");
}
}
@@ -139,7 +139,7 @@ namespace VRM.Samples
File.WriteAllText("new.json", newJson);
// 比較
Assert.AreEqual(oldJson, newJson);
Assert.AreEqual(oldJson.ParseAsJson().ToString(), newJson.ParseAsJson().ToString());
// 生成デシリアライザでロードする
var ff = new JsonFormatter();
@@ -147,7 +147,7 @@ namespace VRM.Samples
ff.Clear();
ff.GenSerialize(des);
var desJson = ff.ToString().ParseAsJson().ToString(" ");
Assert.AreEqual(oldJson, desJson);
Assert.AreEqual(oldJson.ParseAsJson().ToString(), desJson.ParseAsJson().ToString());
}
}
}

View File

@@ -668,7 +668,7 @@ namespace UniGLTF
{
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);

View File

@@ -0,0 +1,9 @@
namespace UniGLTF
{
public enum SerializerTypes
{
JsonSerializable, // manual, Obsolete
UniJSON, // reflection
Generated, // generated, experimental for mobile
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2b17414c7f3ba564083dbcebda778633
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -468,10 +468,10 @@ namespace UniGLTF
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)
{
@@ -479,9 +479,20 @@ namespace UniGLTF
};
json = JsonSchema.FromType(GetType()).Serialize(this, c);
}
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
{
json = ToJson();
throw new Exception("[UniVRM Export Error] unknown serializer type");
}
RemoveUnusedExtensions(json);

View File

@@ -271,18 +271,25 @@ namespace UniGLTF
}
}
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);
}

View File

@@ -383,7 +383,7 @@ namespace VRM
vrm.extensions.VRM.meta.reference = Reference;
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);
}