diff --git a/Source/Engine/Serialization/JsonConverters.cs b/Source/Engine/Serialization/JsonConverters.cs index 3bb0f8a89..07388a09b 100644 --- a/Source/Engine/Serialization/JsonConverters.cs +++ b/Source/Engine/Serialization/JsonConverters.cs @@ -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; } diff --git a/Source/Engine/Serialization/JsonSerializer.cs b/Source/Engine/Serialization/JsonSerializer.cs index 8dad82d64..d988b307c 100644 --- a/Source/Engine/Serialization/JsonSerializer.cs +++ b/Source/Engine/Serialization/JsonSerializer.cs @@ -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(); } /// @@ -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(); } ///