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

Add a new "RootObject" option for TypeNameHandling #2183

thomaslevesque opened this issue Oct 3, 2019 · 1 comment · May be fixed by #2184


Copy link

@thomaslevesque thomaslevesque commented Oct 3, 2019

I'd like to use the TypeNameHandling feature in one of my projects, unfortunately none of the existing options in the TypeNameHandling enum does exactly what I need.

Basically, I want the behavior of Auto, except that I want the $type property to also be emitted for the root object. I know there's a way to do it, by explicitly specifying the type in the call to SerializeObject. However, in my use-case, it's not a very convenient option: I'm using JSON.NET indirectly, through the Cosmos DB SDK, and it doesn't give me the option to specify the type explicitly¹.

I'm aware of #990, and I understand that you don't want to change the current behavior of Auto, so it's not what I'm suggesting (it would be a breaking change for too many people anyway).

However, the behavior I want could be supported without breaking anything, by introducing a new RootObject value for the TypeNameHandling enum. This value would mean always emit the $type property for the root object.

If you're willing to go ahead with this approach, I'll POC it and submit a PR soon.

¹ that's not exactly true: the CreateItemAsync method is generic, and the generic type argument is passed down to the serializer. But because of type inference, it's very easy to forget to specify the type, and write CreateItemAsync(new Derived()) instead of CreateItemAsync<Base>(new Derived()), resulting in missing $type property. This is too error prone, I can't count on myself or my colleagues to never forget it.

@thomaslevesque thomaslevesque changed the title Add a new "Root" option for TypeNameHandling Add a new "RootObject" option for TypeNameHandling Oct 3, 2019
@thomaslevesque thomaslevesque linked a pull request that will close this issue Oct 3, 2019

This comment has been minimized.

Copy link

@thomaslevesque thomaslevesque commented Oct 3, 2019

I went ahead and submtted a PR. The change is pretty small.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.