question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customize how _type is handled

See original GitHub issue

Currently the _type property always contains the assembly qualified name of the type. This means that if something changes between versions (e.g. an assembly or type is renamed), existing document can no longer be deserialized.

Is there a way to use custom type/name mappings instead? From what I see in the code, I don’t think so, but it would be a useful addition.

JSON.NET offers an extension point for this: the ISerializationBinder interface, which lets you map a type to a name and vice versa. Is this something you’d consider adding to LiteDB? It would be very useful to make it easier to evolve apps with existing data.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Oct 21, 2021

Is there any documentation on how to use this?

I don’t think so, but it’s pretty simple. Just pass a BsonMapper with your custom type name binder to the LiteDatabase ctor:

class MyCustomTypeBinder : ITypeNameBinder
{
    public string GetName(Type type)
    {
        // TODO
    }

    public Type GetType(string name)
    {
        // TODO
    }
}

using var db = new LiteDatabase(path, new BsonMapper(new MyCustomTypeBinder));

I’m looking for the ability with polymorphic docs to ignore documents where _type does not resolve to a loaded assembly. I was wondering if this might help me.

I’m not sure it could help for this scenario…

0reactions
tjmoorecommented, Oct 22, 2021

That actually works. Implementing GetName the same as the default, and GetType returns the base class type on matching a condition. In my case I’m looking at the name containing certain words I know all my derived types have, which suits my needs. Anything else is null and reverts to throwing error.

Deserialise then returns instances of the base class, obviously ignoring properties from the derived. That’s okay for what I need.

class MyCustomTypeBinder : ITypeNameBinder
{
    public string GetName(Type type) => type.FullName + ", " + type.GetTypeInfo().Assembly.GetName().Name;

    public Type GetType(string name)
    {
        // GetType without exception
        Type type = Type.GetType(name, false);
        if (type != null)
            return type;

        // Unknown type, but return base if a condition matches
        if (... SOME CONDITION ...)
            return typeof(MyBaseClass);

        // Otherwise null
        return null;
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Type Handling - Dapper .NET
To map — or better — deserialize data coming from your database into a complex custom object Dapper supports a feature named “Custom...
Read more >
How To Create Custom Types in TypeScript
This will allow you to create types that check specific string formats and add more customization to your TypeScript project.
Read more >
Handling Custom Types - Polyfactory - Litestar
Sometimes you need to handle custom types, either from 3rd party libraries or from your own codebase. To achieve this, you can extend...
Read more >
Can Eloquent handle custom types?
I'm working on building an API using Laravel 5 and Postgres for my database. Postgres allows for defining and using custom types. For...
Read more >
events - How do I handle editing of custom types in a C# ...
It turns out I need to handle the CellParsing event: e.Value = new ExDateTime(e.Value.ToString()); e.ParsingApplied = true;.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found