Fix C# serialization of scene objects when property throws and exception

#88
This commit is contained in:
Wojtek Figat
2023-05-06 14:57:44 +02:00
parent 3f2f567364
commit 2affebd375
2 changed files with 85 additions and 44 deletions

View File

@@ -26,7 +26,8 @@ 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)
{
@@ -50,6 +51,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);
@@ -122,9 +166,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();
}
@@ -141,9 +185,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();
}
@@ -161,9 +205,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();
}
@@ -176,21 +220,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>
@@ -214,22 +257,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;
}
@@ -242,22 +283,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;
}
@@ -270,8 +310,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);
@@ -291,14 +330,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>