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.

How to use TypeNameHandling to TypeNameHandling.Auto for deserialization only

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
Mpdreamzcommented, Dec 17, 2014

Instead of globally setting the the type name handling you can also mark properties with:

[JsonProperty(TypeNameHandling = TypeNameHandling.Auto)]

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!

1reaction
Mpdreamzcommented, Jan 15, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

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