List are converted to Discionnary
This commit is contained in:
parent
f5a938f314
commit
f7338aaf93
@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
@ -8,6 +9,10 @@ public class Question
|
|||||||
{
|
{
|
||||||
public int index;
|
public int index;
|
||||||
public string promptId;
|
public string promptId;
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ArrayToDictionaryConverter<Proposition>))]
|
||||||
public Dictionary<int, Proposition> propositions;
|
public Dictionary<int, Proposition> propositions;
|
||||||
|
|
||||||
public double creationDate;
|
public double creationDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,15 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
[JsonObject]
|
[JsonObject]
|
||||||
public class Room
|
public class Room
|
||||||
{
|
{
|
||||||
public string code;
|
public string code;
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ArrayToDictionaryConverter<Question>))]
|
||||||
public Dictionary<int, Question> questions;
|
public Dictionary<int, Question> questions;
|
||||||
public Dictionary<string, Player> players;
|
public Dictionary<string, Player> players;
|
||||||
public string currentQuestionId;
|
public string currentQuestionId;
|
||||||
@ -92,3 +95,43 @@ public List<Proposition> GetPropositionsByPlayer(Player player)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ArrayToDictionaryConverter<T> : JsonConverter<Dictionary<int, T>>
|
||||||
|
{
|
||||||
|
public override Dictionary<int, T> ReadJson(JsonReader reader, Type objectType, Dictionary<int, T> existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonToken.StartArray)
|
||||||
|
{
|
||||||
|
var jsonArray = JArray.Load(reader);
|
||||||
|
var dictionary = new Dictionary<int, T>();
|
||||||
|
|
||||||
|
for (int i = 0; i < jsonArray.Count; i++)
|
||||||
|
{
|
||||||
|
var item = jsonArray[i].ToObject<T>(serializer);
|
||||||
|
dictionary.Add(i, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, Dictionary<int, T> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user