diff --git a/Source/Editor/ProjectInfo.cs b/Source/Editor/ProjectInfo.cs index 0653fe101..64b425681 100644 --- a/Source/Editor/ProjectInfo.cs +++ b/Source/Editor/ProjectInfo.cs @@ -8,6 +8,110 @@ using Newtonsoft.Json; namespace FlaxEditor { + /// + /// + /// + public class FlaxVersionConverter : JsonConverter + { + // Original implementation is based on Newtonsoft.Json VersionConverter + /// + /// Writes the JSON representation of the object. + /// + /// The to write to. + /// The value. + /// The calling serializer. + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + if (value == null) + { + writer.WriteNull(); + } + else if (value is Version) + { + writer.WriteValue(value.ToString()); + } + else + { + throw new JsonSerializationException("Expected Version object value"); + } + } + + /// + /// Reads the JSON representation of the object. + /// + /// The to read from. + /// Type of the object. + /// The existing property value of the JSON that is being converted. + /// The calling serializer. + /// The object value. + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.Null) + { + return null; + } + else + { + if (reader.TokenType == JsonToken.StartObject) + { + try + { + reader.Read(); + Dictionary values = new Dictionary(); + while (reader.TokenType == JsonToken.PropertyName) + { + var key = reader.Value as string; + reader.Read(); + var val = (long)reader.Value; + reader.Read(); + values.Add(key, (int)val); + } + + int major = 0, minor = 0, build = 0; + values.TryGetValue("Major", out major); + values.TryGetValue("Minor", out minor); + values.TryGetValue("Build", out build); + + Version v = new Version(major, minor, build); + return v; + } + catch (Exception ex) + { + throw new Exception(String.Format("Error parsing version string: {0}", reader.Value), ex); + } + } + else if (reader.TokenType == JsonToken.String) + { + try + { + Version v = new Version((string)reader.Value!); + return v; + } + catch (Exception ex) + { + throw new Exception(String.Format("Error parsing version string: {0}", reader.Value), ex); + } + } + else + { + throw new Exception(String.Format("Unexpected token or value when parsing version. Token: {0}, Value: {1}", reader.TokenType, reader.Value)); + } + } + } + + /// + /// Determines whether this instance can convert the specified object type. + /// + /// Type of the object. + /// + /// true if this instance can convert the specified object type; otherwise, false. + /// + public override bool CanConvert(Type objectType) + { + return objectType == typeof(Version); + } + } + /// /// Contains information about Flax project. /// @@ -154,7 +258,7 @@ namespace FlaxEditor { // Load var contents = File.ReadAllText(path); - var project = JsonConvert.DeserializeObject(contents); + var project = JsonConvert.DeserializeObject(contents, new JsonSerializerSettings() { Converters = new[] { new FlaxVersionConverter() } }); project.ProjectPath = path; project.ProjectFolderPath = StringUtils.NormalizePath(Path.GetDirectoryName(path));