C#C
C#7mo ago
4 replies
VoidPointer

Self Referencing Loop Exception Trying to Serialize Object to JSON

I have a RestSharp IRestResponse object that looks like this:
    Content: "{\"TaxRefNumber\":\"0860111830\",\"TaxStatus\":{\"SuccessInd\":1,\"ComplianceStatus\":\"N\",\"CreatedOnDt\":\"2025-06-18T16:29:00\"}}"
    ContentEncoding: ""
    ContentLength: 117
    ContentType: "application/json"
    Cookies: Count = 0
    ErrorException: null
    ErrorMessage: null
    Headers: Count = 7
    IsSuccessful: true
    ProtocolVersion: {1.1}
    RawBytes: {byte[117]}
    Request: {RestSharp.RestRequest}
    ResponseStatus: Completed
    ResponseUri: {https://gateway1.ekss.co.za/sarsServices/resources/assemblers/taxStatus?AccountID=contactableDEMO&AccountHash=demo_SHAUN%402&TaxRefNumber=0860111830}
    Server: "nginx/1.23.3"
    StatusCode: OK
    StatusDescription: "OK"

and when I try and serialize it with Newtonsoft's JsonConvert.SerializeObject(response, Formatting.Indented), I get this exception:
'JsonConvert.SerializeObject(response, Formatting.Indented)' threw an exception of type 'Newtonsoft.Json.JsonSerializationException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233088
    HelpLink: null
    InnerException: null
    LineNumber: 0
    LinePosition: 0
    Message: "Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'Request.OnBeforeDeserialization.Method.Module.Assembly'."
    Path: "Request.OnBeforeDeserialization.Method.Module.Assembly"
    Source: "Newtonsoft.Json"
    StackTrace: ***<redacted>***


I don't see any such property as ManifestModule, but maybe I'm blinder than I thought, or sleep deprived, or something.

And please don't tell me to use STJ: that is way beyond my control at the moment, the best I can do is suggest using STJ, but it will take a long time for that to happen.
Was this page helpful?