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.

Access the parent object in a converter

See original GitHub issue

Stack overflow example

NewtonSoft JsonConverter - Access other properties

Problem

There is no way to access the parent object from a JsonConverter. For example if there was a need to alter the output of the converter that is dependent on containing object that is being serialized. In my specific use case, I need to format the JSON output for a property as a currency string, dependent on the Culture that is stored within the containing object.

eg.

public class Contract {
   [JsonIgnore]
   public CultureInfo Culture {get; set;}

   [JsonConverter(typeof(CurrencyConverter))]
   public decimal Cost {get;set;}

   // Lots of additional properties with serialization controlled by attributes.
}

With an array of contracts I’d get the following JSON [{ Cost : "£5000.00"}, { Cost : "$8000.00"}, { Cost : "€599.00"}]

Suggested solution

Expose the JsonSerializeWriter’s _serializerStack as a ReadOnlyCollection as a public property on the serializer object passed to WriteJson.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
tankbobcommented, Dec 18, 2017

@JonHanna Because that means you need to handle the conversion of all the properties in the containing object rather that relying on the default behaviour using properties.

Also if you had a Contract with many Vendors each with many Assets and the Asset had a cost field that needed the output formatted depending on the Contract Culture you would have to write a converter for the Contract that then serialized all the child properties. That creates a lot of extra code for the converter where a simple converter could walk the serialization tree to find that culture and format the output.

Your DTO objects would be relatively clean with attributes defining their behaviour and your converter also would be relatively simple without the need to map all the extra properties.

I’ve got it working by using reflection to call GetInternalSerializer() on the passed JsonSerializer and then retrieve the private _serializerStack field. All it would need is for JsonSerializerInternalWriter to expose the “_serializerStack” field as a property that the JsonSerializer could also expose. My suggestion is to expose it as a read only collection to protect the internals.

3reactions
hybridherbstcommented, Nov 11, 2020

I’d like to bump this, as it seems reflecting into the serializer is still the only way to get to the serialization stack. Or is there another way by now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonConverter: Get parent object in ReadJson, without $refs
I'm trying to create a CustomCreationConverter to set the Parent property of each object to the parent node in the JSON input using...
Read more >
How can composed sub-objects access the parent object?
In a magical perfect-c++-land I'd be able to create some sort of a namespace inside the Car class, adding a eg. Engine:: prefix...
Read more >
How to access parent object fields fron child ...
You're populating an sObject Id in Apttus_Proposal__Proposal__c . This does not provide you with access to related-record fields: xyz ...
Read more >
Accessing object in seq of parent type
I want to have a sequence with a parent type, so I can access the value, but without casting it every time. I...
Read more >
Get reference to parent object in model from itemArray for ...
Question: Given a model that uses a parent-child relationship in which the child objects are used to power an itemArray, is it possible...
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