yojimbo87 / ArangoDB-NET

C#/.NET/Mono driver for ArangoDB
MIT License
107 stars 66 forks source link

Invalid Cast Exception while retrieve SortedList<int, bool> for Arango DB #16

Closed panglivlal closed 9 years ago

panglivlal commented 9 years ago

Hi Yojimbo87,

I facing an Invalid Cast exception while try retrieve SortedList<int, bool> for ArangoDB.

My Class:

public class TestEntity
{
    public SortedList<int, bool> IsDayTypeEnabledLateness { get; set; }
}

and A simple Unit Test case

public void Test_Entity_save_and_retrieve()
{
    Database.CreateTestCollection(Database.TestDocumentCollectionName, ACollectionType.Document);
    var db = new ADatabase(Database.Alias);

    var isDayTypeEnabledLateness = new SortedList<int, bool>();
    isDayTypeEnabledLateness.Add(1,true);
    isDayTypeEnabledLateness.Add(2,false);
    isDayTypeEnabledLateness.Add(3,false);
    isDayTypeEnabledLateness.Add(4,false);

    var entity = new TestEntity();
    entity.IsDayTypeEnabledLateness = isDayTypeEnabledLateness;

    var createResult = db.Document.Create<TestEntity>(Database.TestDocumentCollectionName, entity);

    Assert.IsTrue(createResult.Success);

    var getresult = db.Document.Get<TestEntity>(createResult.Value.ID());

    Assert.IsTrue(getresult.Success);
    Assert.IsTrue(getresult.HasValue);
}
yojimbo87 commented 9 years ago

Hi,

fixed in reimplement branch.

One thing to note is that SortedList<TKey, TValue> is serialized into JSON structure as List<Dictionary<string, object>> since there is no direct representation of sorted list object in JSON format. Your object thus looks like this once stored inside ArangoDB:

{
  "SortedList": [
    {
      "v": true,
      "k": 1
    },
    {
      "v": false,
      "k": 2
    },
    {
      "v": false,
      "k": 3
    },
    {
      "v": false,
      "k": 4
    }
  ]
}