Merge remote-tracking branch 'origin/master' into 1.6
# Conflicts: # .github/workflows/tests.yml # Source/Engine/Content/JsonAsset.cs
This commit is contained in:
@@ -37,7 +37,7 @@ namespace FlaxEngine.Json
|
||||
{
|
||||
// Skip serialization as reference id for the root object serialization (eg. Script)
|
||||
var cache = JsonSerializer.Current.Value;
|
||||
if (cache != null && cache.IsDuringSerialization && cache.SerializerWriter.SerializeStackSize == 0)
|
||||
if (cache != null && cache.IsWriting && cache.SerializerWriter.SerializeStackSize == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -26,12 +26,14 @@ namespace FlaxEngine.Json
|
||||
public JsonSerializerInternalWriter SerializerWriter;
|
||||
public UnmanagedMemoryStream MemoryStream;
|
||||
public StreamReader Reader;
|
||||
public bool IsDuringSerialization;
|
||||
public bool IsWriting;
|
||||
public bool IsReading;
|
||||
|
||||
public unsafe SerializerCache(JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer = Newtonsoft.Json.JsonSerializer.CreateDefault(settings);
|
||||
JsonSerializer.Formatting = Formatting.Indented;
|
||||
JsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
|
||||
StringBuilder = new StringBuilder(256);
|
||||
StringWriter = new StringWriter(StringBuilder, CultureInfo.InvariantCulture);
|
||||
SerializerWriter = new JsonSerializerInternalWriter(JsonSerializer);
|
||||
@@ -50,6 +52,49 @@ namespace FlaxEngine.Json
|
||||
DateFormatString = JsonSerializer.DateFormatString,
|
||||
};
|
||||
}
|
||||
|
||||
public void ReadBegin()
|
||||
{
|
||||
if (IsReading)
|
||||
{
|
||||
// TODO: Reset reading state (eg if previous deserialization got exception)
|
||||
}
|
||||
IsWriting = false;
|
||||
IsReading = true;
|
||||
}
|
||||
|
||||
public void ReadEnd()
|
||||
{
|
||||
IsReading = false;
|
||||
}
|
||||
|
||||
public void WriteBegin()
|
||||
{
|
||||
if (IsWriting)
|
||||
{
|
||||
// Reset writing state (eg if previous serialization got exception)
|
||||
JsonWriter = new JsonTextWriter(StringWriter)
|
||||
{
|
||||
IndentChar = '\t',
|
||||
Indentation = 1,
|
||||
Formatting = JsonSerializer.Formatting,
|
||||
DateFormatHandling = JsonSerializer.DateFormatHandling,
|
||||
DateTimeZoneHandling = JsonSerializer.DateTimeZoneHandling,
|
||||
FloatFormatHandling = JsonSerializer.FloatFormatHandling,
|
||||
StringEscapeHandling = JsonSerializer.StringEscapeHandling,
|
||||
Culture = JsonSerializer.Culture,
|
||||
DateFormatString = JsonSerializer.DateFormatString,
|
||||
};
|
||||
}
|
||||
StringBuilder.Clear();
|
||||
IsWriting = true;
|
||||
IsReading = false;
|
||||
}
|
||||
|
||||
public void WriteEnd()
|
||||
{
|
||||
IsWriting = false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static JsonSerializerSettings Settings = CreateDefaultSettings(false);
|
||||
@@ -117,9 +162,9 @@ namespace FlaxEngine.Json
|
||||
var cache = isManagedOnly ? CacheManagedOnly.Value : Cache.Value;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.StringBuilder.Clear();
|
||||
cache.IsDuringSerialization = true;
|
||||
cache.WriteBegin();
|
||||
cache.SerializerWriter.Serialize(cache.JsonWriter, obj, type);
|
||||
cache.WriteEnd();
|
||||
|
||||
return cache.StringBuilder.ToString();
|
||||
}
|
||||
@@ -136,9 +181,9 @@ namespace FlaxEngine.Json
|
||||
var cache = isManagedOnly ? CacheManagedOnly.Value : Cache.Value;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.StringBuilder.Clear();
|
||||
cache.IsDuringSerialization = true;
|
||||
cache.WriteBegin();
|
||||
cache.SerializerWriter.Serialize(cache.JsonWriter, obj, type);
|
||||
cache.WriteEnd();
|
||||
|
||||
return cache.StringBuilder.ToString();
|
||||
}
|
||||
@@ -156,9 +201,9 @@ namespace FlaxEngine.Json
|
||||
var cache = isManagedOnly ? CacheManagedOnly.Value : Cache.Value;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.StringBuilder.Clear();
|
||||
cache.IsDuringSerialization = true;
|
||||
cache.WriteBegin();
|
||||
cache.SerializerWriter.SerializeDiff(cache.JsonWriter, obj, type, other);
|
||||
cache.WriteEnd();
|
||||
|
||||
return cache.StringBuilder.ToString();
|
||||
}
|
||||
@@ -171,21 +216,20 @@ namespace FlaxEngine.Json
|
||||
public static void Deserialize(object input, string json)
|
||||
{
|
||||
var cache = Cache.Value;
|
||||
cache.IsDuringSerialization = false;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.ReadBegin();
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
cache.JsonSerializer.Populate(reader, input);
|
||||
|
||||
if (!cache.JsonSerializer.CheckAdditionalContent)
|
||||
return;
|
||||
while (reader.Read())
|
||||
if (cache.JsonSerializer.CheckAdditionalContent)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
}
|
||||
}
|
||||
cache.ReadEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -209,22 +253,20 @@ namespace FlaxEngine.Json
|
||||
{
|
||||
object result;
|
||||
var cache = Cache.Value;
|
||||
cache.IsDuringSerialization = false;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.ReadBegin();
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
result = cache.JsonSerializer.Deserialize(reader, objectType);
|
||||
|
||||
if (!cache.JsonSerializer.CheckAdditionalContent)
|
||||
return result;
|
||||
while (reader.Read())
|
||||
if (cache.JsonSerializer.CheckAdditionalContent)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cache.ReadEnd();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -237,22 +279,21 @@ namespace FlaxEngine.Json
|
||||
{
|
||||
object result;
|
||||
var cache = Cache.Value;
|
||||
cache.IsDuringSerialization = false;
|
||||
Current.Value = cache;
|
||||
|
||||
cache.ReadBegin();
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
result = cache.JsonSerializer.Deserialize(reader);
|
||||
|
||||
if (!cache.JsonSerializer.CheckAdditionalContent)
|
||||
return result;
|
||||
while (reader.Read())
|
||||
if (cache.JsonSerializer.CheckAdditionalContent)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cache.ReadEnd();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -265,8 +306,7 @@ namespace FlaxEngine.Json
|
||||
public static unsafe void Deserialize(object input, byte* jsonBuffer, int jsonLength)
|
||||
{
|
||||
var cache = Cache.Value;
|
||||
cache.IsDuringSerialization = false;
|
||||
Current.Value = cache;
|
||||
cache.ReadBegin();
|
||||
|
||||
/*// Debug json string reading
|
||||
cache.MemoryStream.Initialize(jsonBuffer, jsonLength);
|
||||
@@ -286,14 +326,16 @@ namespace FlaxEngine.Json
|
||||
{
|
||||
cache.JsonSerializer.Populate(jsonReader, input);
|
||||
}
|
||||
|
||||
if (!cache.JsonSerializer.CheckAdditionalContent)
|
||||
return;
|
||||
while (jsonReader.Read())
|
||||
if (cache.JsonSerializer.CheckAdditionalContent)
|
||||
{
|
||||
if (jsonReader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
while (jsonReader.Read())
|
||||
{
|
||||
if (jsonReader.TokenType != JsonToken.Comment)
|
||||
throw new Exception("Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
}
|
||||
|
||||
cache.ReadEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -456,7 +456,7 @@ namespace Serialization
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = (T*)FindObject(id, T::GetStaticClass());
|
||||
v = (T*)::FindObject(id, T::GetStaticClass());
|
||||
}
|
||||
|
||||
// Scripting Object Reference
|
||||
|
||||
Reference in New Issue
Block a user