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

Compare with JsonSubTypes package. #35

Closed
michael-freidgeim-webjet opened this issue Oct 8, 2023 · 3 comments
Closed

Compare with JsonSubTypes package. #35

michael-freidgeim-webjet opened this issue Oct 8, 2023 · 3 comments

Comments

@michael-freidgeim-webjet

There is a similar package https://github.com/manuc66/JsonSubTypes
Can you explain in Readme, what are pro and cons of your package , why someone should use your or their library?

@dmitry-bym
Copy link
Owner

dmitry-bym commented Oct 13, 2023

hey, yep, so actually some time ago i needed an ability to ser/deser polymorphic types and 1st solution i found https://github.com/manuc66/JsonSubTypes , but there is a critical nuance for me. Example from JsonSubTypes doc:

[JsonConverter(typeof(JsonSubtypes), "Kind")]
public interface IAnimal
{
    string Kind { get; }
}

public class Dog : IAnimal
{
    public string Kind { get; } = "Dog";
    public string Breed { get; set; }
}

public class Cat : IAnimal {
    public string Kind { get; } = "Cat";
    public bool Declawed { get; set;}
}
  1. As you can see here, need to specify property-discriminator what I really don't like todo, why i have to specify Kind if i already know that this is a Dod, i have class dog or why i need some kind of meta info inside data class, looks weird.
    I didn't even understand how to use it from the very beginning xDD
  2. also im not sure how it works with $type - field, cause this is default field everywhere to send discriminator

This is actualy why i desided to create my own solution. So as you can see on 2 block (from my lib doc), it doesn't change the class structure in any way, it works from "outside". Im working a lot with Mongo, so you can find in mongo driver BsonKnownTypesAttribute it works similar way

[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
[JsonDiscriminator(Name = "myType")] //add custom discriminator name
[JsonKnownType(typeof(BaseClass1Heir))] //could be deleted if you didn't turn off UseClassNameAsDiscriminator
[JsonKnownType(typeof(BaseClass2Heir), "myDiscriminator")]
public class BaseClass { ... }
  
public class BaseClass1Heir : BaseClass  { ... }
 
public class BaseClass2Heir : BaseClass  { ... }

So why to use mine or their, so ask yourself:

  1. What solution is more beautiful?
  2. Compare perfomance, but its not critical cause its ok for both, anyway if you need super perfomance you need to write your own non-generic solution, and also maybe use another serializer

@dmitry-bym
Copy link
Owner

I leave it here, maybe add to readme some day, just to lazy to do that xD

@michael-freidgeim-webjet
Copy link
Author

Дмитрий, спасибо

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

No branches or pull requests

2 participants