How to use TypeNameHandling to TypeNameHandling.Auto for deserialization only
See original GitHub issueI need to set the TypeNameHandling to TypeNameHandling.Auto to deserialize my objects returned by elasticsearch. I set the settings
new ConnectionSettings(uri).SetJsonSerializerSettingsModifier(s => s.TypeNameHandling = TypeNameHandling.Auto)
But this also change the type name handling of when sending the query. So Nest sends the query with multiple $type in it which doesn’t work :
{
"_source": {
"$type": "Nest.SearchSourceDescriptor`1[[Pelican.Web.Models.Search.EntityContainer`1[[Pelican.Models.FormModel, Pelican.Models]], Pelican.Web]], Nest",
"include": [
"Name",
"Date",
"Data"
]
},
"query": {
"filtered": {
"$type": "Nest.FilteredQueryDescriptor`1[[Pelican.Web.Models.Search.EntityContainer`1[[Pelican.Models.FormModel, Pelican.Models]], Pelican.Web]], Nest",
"filter": {
"terms": {
"Name": [
"Document name"
],
"execution": "or"
}
}
}
}
}
So, how can I set the deserialization settings of Newtownsoft without touching the serialization?
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top Results From Across the Web
TypeNameHandling setting
This sample uses the TypeNameHandling setting to include type information when serializing JSON and read type information so that the create types are...
Read more >CA2326: Do not use TypeNameHandling values other than ...
This rule finds Newtonsoft.Json.TypeNameHandling values other than None . If you want to deserialize only when a Newtonsoft.Json.Serialization.
Read more >c# - is Newtonsoft TypeNameHandling.all with a basic ...
On our API we need to take in json, deserialize it to an interface, set a field, and ship it off. To achieve...
Read more >Json.NET Serializer • Newtonsoft Serializer
The NewtonsoftJsonSerializer is using TypeNameHandling.None by default. If TypeNameHandling.Auto is required it can be configured via custom ...
Read more >A Case Of Newtonsoft.Json, TypeNameHandling.All, And ...
With that we use TypeNameHandling.All to serialize the type information as well. ... Best fix: Just don't read it.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Instead of globally setting the the type name handling you can also mark properties with:
There is also a
ItemTypeNameHandling
option if you are working with collections.This is the safer way adding support for this only local to your objects.
Hope this solves your issue!
Ok then while not a bug it should be a feature request on JSON.NET to be able to specify it at the interface level.
I understand your frustration but we are all users of json.net here and your needs should be able to be specified locally on your scope only. By saying to us you expect to to alter a pretty intrusive setting globally without altering how our objects are serialized you are looking at it the wrong way IMO.
You can write a custom converter for now and apply it to your
IMyInterfaceType
for the time being. With NEST 2.0 we are exploring options at generating the serializing/deserialization of our objects ahead of time (no longer relying on JSON.NET defaults (perhaps not using JSON.NET at all)) but this will take a while to implement.