From 008a0ab125d636aa40453cf2ee7bb7ff65eae9c6 Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Tue, 29 Aug 2023 17:41:06 +0100 Subject: [PATCH 1/2] Initial deserialise nested list option --- Serialiser_Engine/Compute/Deserialise.cs | 7 +++- Serialiser_Engine/Compute/Deserialise/List.cs | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Serialiser_Engine/Compute/Deserialise.cs b/Serialiser_Engine/Compute/Deserialise.cs index d7b01a368..108355dce 100644 --- a/Serialiser_Engine/Compute/Deserialise.cs +++ b/Serialiser_Engine/Compute/Deserialise.cs @@ -53,7 +53,12 @@ private static object IDeserialise(this BsonValue bson, string version, bool isU if (bson.IsBsonNull) return null; else if (bson.IsBsonArray) - return bson.DeserialiseList(new List(), version, isUpgraded); + { + if (IsNestedList(bson)) + return bson.DeserialiseNestedList(new List>(), version, isUpgraded); + else + return bson.DeserialiseList(new List(), version, isUpgraded); + } else if (bson.IsBsonDocument) { BsonDocument doc = bson.AsBsonDocument; diff --git a/Serialiser_Engine/Compute/Deserialise/List.cs b/Serialiser_Engine/Compute/Deserialise/List.cs index 7c595bad9..207978e1b 100644 --- a/Serialiser_Engine/Compute/Deserialise/List.cs +++ b/Serialiser_Engine/Compute/Deserialise/List.cs @@ -50,11 +50,49 @@ private static List DeserialiseList(this BsonValue bson, List value, st value = new List(); foreach (BsonValue item in bson.AsBsonArray) + { value.Add((T)item.IDeserialise(typeof(T), null, version, isUpgraded)); + } return value; } /*******************************************/ + + private static List> DeserialiseNestedList(this BsonValue bson, List> value, string version, bool isUpgraded) + { + bson = ExtractValue(bson); + + if (!bson.IsBsonArray) + { + BH.Engine.Base.Compute.RecordError("Expected to deserialise a List and received " + bson.ToString() + " instead."); + return value; + } + + if (value == null) + value = new List>(); + + foreach (BsonValue item in bson.AsBsonArray) + { + value.Add((List)item.IDeserialise(typeof(List), null, version, isUpgraded)); + } + + return value; + } + + /*******************************************/ + + private static bool IsNestedList(this BsonValue bson) + { + int nest = 0; + + while(bson.IsBsonArray) + { + nest++; + bson = bson[0]; + } + + return nest > 1; + } } } From 28246740877911a7c4674b8b8f5b391f29788627 Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Wed, 30 Aug 2023 10:18:35 +0100 Subject: [PATCH 2/2] Make changes as discussed --- Serialiser_Engine/Compute/Deserialise/List.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Serialiser_Engine/Compute/Deserialise/List.cs b/Serialiser_Engine/Compute/Deserialise/List.cs index 207978e1b..baaeb7226 100644 --- a/Serialiser_Engine/Compute/Deserialise/List.cs +++ b/Serialiser_Engine/Compute/Deserialise/List.cs @@ -50,9 +50,7 @@ private static List DeserialiseList(this BsonValue bson, List value, st value = new List(); foreach (BsonValue item in bson.AsBsonArray) - { value.Add((T)item.IDeserialise(typeof(T), null, version, isUpgraded)); - } return value; } @@ -73,9 +71,7 @@ private static List> DeserialiseNestedList(this BsonValue bson, List< value = new List>(); foreach (BsonValue item in bson.AsBsonArray) - { - value.Add((List)item.IDeserialise(typeof(List), null, version, isUpgraded)); - } + value.Add((List)item.DeserialiseList(new List(), version, isUpgraded)); return value; }