Access the parent object in a converter
See original GitHub issueStack 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:
- Created 6 years ago
- Reactions:5
- Comments:8 (1 by maintainers)
Top 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 >
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 Free
Top 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
@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 manyVendor
s each with manyAsset
s and theAsset
had a cost field that needed the output formatted depending on the Contract Culture you would have to write a converter for theContract
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 passedJsonSerializer
and then retrieve the private_serializerStack
field. All it would need is forJsonSerializerInternalWriter
to expose the “_serializerStack” field as a property that theJsonSerializer
could also expose. My suggestion is to expose it as a read only collection to protect the internals.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?