diff --git a/Assets/Scripts/DatabaseClasses/Question.cs b/Assets/Scripts/DatabaseClasses/Question.cs index 6b103c0..cd3ea2d 100644 --- a/Assets/Scripts/DatabaseClasses/Question.cs +++ b/Assets/Scripts/DatabaseClasses/Question.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -8,6 +9,10 @@ public class Question { public int index; public string promptId; + + [JsonConverter(typeof(ArrayToDictionaryConverter))] public Dictionary propositions; + public double creationDate; } + diff --git a/Assets/Scripts/DatabaseClasses/Room.cs b/Assets/Scripts/DatabaseClasses/Room.cs index f34cda6..5ba913d 100644 --- a/Assets/Scripts/DatabaseClasses/Room.cs +++ b/Assets/Scripts/DatabaseClasses/Room.cs @@ -2,12 +2,15 @@ using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; [Serializable] [JsonObject] public class Room { public string code; + + [JsonConverter(typeof(ArrayToDictionaryConverter))] public Dictionary questions; public Dictionary players; public string currentQuestionId; @@ -92,3 +95,43 @@ public List GetPropositionsByPlayer(Player player) } } +public class ArrayToDictionaryConverter : JsonConverter> +{ + public override Dictionary ReadJson(JsonReader reader, Type objectType, Dictionary existingValue, bool hasExistingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.StartArray) + { + var jsonArray = JArray.Load(reader); + var dictionary = new Dictionary(); + + for (int i = 0; i < jsonArray.Count; i++) + { + var item = jsonArray[i].ToObject(serializer); + dictionary.Add(i, item); + } + + return dictionary; + } + + return null; + } + + public override void WriteJson(JsonWriter writer, Dictionary value, JsonSerializer serializer) + { + if (value != null) + { + var jsonArray = new JArray(); + + foreach (var kvp in value) + { + var itemJson = JToken.FromObject(kvp.Value, serializer); + jsonArray.Add(itemJson); + } + + jsonArray.WriteTo(writer); + } + } +} + + +