Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime as dictionary key not serialized correctly. #1350

Closed
PassiveModding opened this issue Oct 20, 2019 · 5 comments
Closed

DateTime as dictionary key not serialized correctly. #1350

PassiveModding opened this issue Oct 20, 2019 · 5 comments

Comments

@PassiveModding
Copy link

@PassiveModding PassiveModding commented Oct 20, 2019

var k = K.IsEnum ? Enum.Parse(K, el.Key) : Convert.ChangeType(el.Key, K);

String '26/08/2019 6:57:26 AM' was not recognized as a valid DateTime.
System.FormatException: String '26/08/2019 6:57:26 AM' was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(ReadOnlySpan`1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at LiteDB.BsonMapper.DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocument value)
   at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
   at LiteDB.BsonMapper.DeserializeObject(Type type, Object obj, BsonDocument value)
   at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
   at LiteDB.BsonMapper.DeserializeObject(Type type, Object obj, BsonDocument value)
   at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
   at LiteDB.BsonMapper.ToObject[T](BsonDocument doc)
   at LiteDB.LiteCollection`1.Find(Query query, Int32 skip, Int32 limit)+MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at RavenBOT.ELO.Modules.Modules.Info.LeaderboardAsync()

It appears when using a datetime as a key for a dictionary stored the parser does not convert it into a litedb format but rather leaves it as a string

@PassiveModding

This comment has been minimized.

Copy link
Author

@PassiveModding PassiveModding commented Oct 20, 2019

I suppose the root of the issue is in the

private BsonDocument SerializeDictionary(Type type, IDictionary dict, int depth)
method then as it just sets the 'index' of the value to be key.ToString().

@PassiveModding

This comment has been minimized.

Copy link
Author

@PassiveModding PassiveModding commented Oct 21, 2019

Basically a dupe of #546

@mbdavid

This comment has been minimized.

Copy link
Owner

@mbdavid mbdavid commented Oct 22, 2019

Hi @PassiveModding, I think if this key.ToString() contains a special format for DateTime (like ISO-8601) will be possible to revert from string to datetime.

mbdavid added a commit that referenced this issue Oct 22, 2019
@PassiveModding

This comment has been minimized.

Copy link
Author

@PassiveModding PassiveModding commented Oct 23, 2019

@mbdavid thanks, yes that would work. Are there any plans to have a generic method of serializing classes to work as keys by any chance?

@mbdavid

This comment has been minimized.

Copy link
Owner

@mbdavid mbdavid commented Oct 23, 2019

In dictionary, keys must be a String type because are serialized as JSON document (each Key will be converted into a Field, and fields must be a string).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.