This project has moved. For the latest updates, please go here.

Serializing Interchange into JSON results in Exception due to circular reference

Jun 30 at 8:41 PM
Edited Jun 30 at 9:16 PM
I've been attempting to serialize the interchange XML document into JSON for quite a while now...

I just attempted to run JsonConvert.SerializeObject(interchange) and immediately experienced an exception due to a circular reference. See the below code to perform your own test.

It would seem the issue is the fact that the Interchange class includes an IEnumerable<FunctionGroup> which a FunctionGroup class contains an Interchange. I'm not wholly certain that this is the issue...

But being able to serialize this EASILY into a valid JSON object that could be converted back into X12 would be a wondeful addition to the library... I'm just not certain of how one would make a change either to fix the circular reference when attempting to serialize directly into JSON OR adding the additional functionality to the library such that it would provide such a serialization.

I've found I can get the Serialize Object to NOT throw if I pass in JsonSerializerSettings with the ReferenceLoopHandling set to Ignore, like so.
                var jsonSerializeSettings = new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                };

                var jsonEdi = JsonConvert.SerializeObject(interchange, jsonSerializeSettings);
using Newtonsoft.Json;
using OopFactory.X12.Parsing;
using System.Xml;

namespace X12ConversionTester
{
    class Program
    {
        private static string Edi = "ISA*00*          *00*          *ZZ*NBRICSAS2      *ZZ*RICSTEST       *110209*1204*U*00401*000000012*0*T*:~GS*SH*NBRICSAS2*RICSTEST*20110209*1204*12*X*004010~ST*856*0030~BSN*00*16290412281000050*20110209*115203*0001~HL*1**S~TD1*CTN76*2****G*6.05*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330603~DTM*011*20101228~N1*ST**92*001~HL*2*1*O~PRF*1227ROB***20101227~TD1*CNT25*1****G*2.95*LB~HL*3*2*P~MAN*GM*00000848810195873691~HL*4*3*I~LIN**UP*884577756900*VA*MR1012MC*SZ*105~SN1**1*EA~HL*5*1*O~PRF*122710SWENK***20101227~TD1*CNT25*1****G*3.1*LB~HL*6*5*P~MAN*GM*00000848810195875886~HL*7*6*I~LIN**UP*884577145483*VA*MT910GT*SZ*13~SN1**1*EA~CTT*7~SE*26*0030~ST*856*0031~BSN*00*10332312291000002*20110209*115203*0001~HL*1**S~TD1*CTN76*1****G*204*LB~TD5*****UPS Collect 3rd Party~REF*BM*122810 1000~DTM*011*20101229~N1*ST**92*001~HL*2*1*O~PRF*122810ROBINSON***20101228~TD1*CNT25*1****G*204*LB~HL*3*2*P~MAN*GM*00000848818524766812~HL*4*3*I~LIN**UP*821636164924*VA*WW811WT*SZ*09~SN1**1*EA~CTT*4~SE*18*0031~ST*856*0032~BSN*00*16283412291000037*20110209*115203*0001~HL*1**S~TD1*CTN76*1****G*2.95*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330625~DTM*011*20101229~N1*ST**92*001~HL*2*1*O~PRF*122810BROWN***20101228~TD1*CNT25*1****G*2.95*LB~HL*3*2*P~MAN*GM*00000848810195891695~HL*4*3*I~LIN**UP*883905366842*VA*MR993BK*SZ*12~SN1**1*EA~CTT*4~SE*18*0032~ST*856*0033~BSN*00*16200012301000042*20110209*115203*0001~HL*1**S~TD1*CTN76*3****G*7.95*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330640~DTM*011*20101230~N1*ST**92*001~HL*2*1*O~PRF*1229SLAUGHTER***20101229~TD1*CNT25*1****G*2.3*LB~HL*3*2*P~MAN*GM*00000848810195902988~HL*4*3*I~LIN**UP*883575252087*VA*WR1123MC*SZ*06~SN1**1*EA~HL*5*1*O~PRF*1229MANSAH***20101229~TD1*CNT25*1****G*2.85*LB~HL*6*5*P~MAN*GM*00000848810195901769~HL*7*6*I~LIN**UP*883905367474*VA*MR993GL*SZ*115~SN1**1*EA~HL*8*1*O~PRF*1229KULIK***20101229~TD1*CNT25*1****G*2.8*LB~HL*9*8*P~MAN*GM*00000848810195903442~HL*10*9*I~LIN**UP*883905367467*VA*MR993GL*SZ*11~SN1**1*EA~CTT*10~SE*34*0033~ST*856*0034~BSN*00*17144201031100058*20110209*115204*0001~HL*1**S~TD1*CTN76*1****G*2.4*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330662~DTM*011*20110103~N1*ST**92*001~HL*2*1*O~PRF*1231SHETRIT***20101231~TD1*CNT25*1****G*2.4*LB~HL*3*2*P~MAN*GM*00000848810195913205~HL*4*3*I~LIN**UP*883905366750*VA*MR993BK*SZ*075~SN1**1*EA~CTT*4~SE*18*0034~ST*856*0035~BSN*00*17080401041100081*20110209*115204*0001~HL*1**S~TD1*CTN76*2****G*8.55*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330695~DTM*011*20110104~N1*ST**92*001~HL*2*1*O~PRF*010310BURCH***20110103~TD1*CNT25*1****G*5.45*LB~HL*3*2*P~MAN*GM*00000848810193935506~HL*4*3*I~LIN**UP*827743147898*VA*MW811VK*SZ*115~SN1**1*EA~HL*5*3*I~LIN**UP*883905918348*VA*WW927BK*SZ*09~SN1**1*EA~HL*6*1*O~PRF*0103MIRK***20110103~TD1*CNT25*1****G*3.1*LB~HL*7*6*P~MAN*GM*00000848810195934668~HL*8*7*I~LIN**UP*883905366866*VA*MR993BK*SZ*13~SN1**1*EA~CTT*8~SE*29*0035~ST*856*0036~BSN*00*16110401071100052*20110209*115204*0001~HL*1**S~TD1*CTN76*1****G*2.9*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330765~DTM*011*20110107~N1*ST**92*001~HL*2*1*O~PRF*010611PETERS***20110106~TD1*CNT25*1****G*2.9*LB~HL*3*2*P~MAN*GM*00000848810196030529~HL*4*3*I~LIN**UP*883905366989*VA*MR993BK*SZ*11~SN1**1*EA~CTT*4~SE*18*0036~ST*856*0037~BSN*00*19090601071100068*20110209*115204*0001~HL*1**S~TD1*CTN76*1****G*3.05*LB~TD5*****UPS Collect~REF*BM*0000027746946~DTM*011*20110107~N1*ST**92*001~HL*2*1*O~PRF*010611DRISCOLL***20110106~TD1*CNT25*1****G*3.05*LB~HL*3*2*P~MAN*GM*00000848813341449788~HL*4*3*I~LIN**UP*883905583645*VA*CT1004W*SZ*105~SN1**1*EA~CTT*4~SE*18*0037~ST*856*0038~BSN*00*17320701101100061*20110209*115204*0001~HL*1**S~TD1*CTN76*2****G*5.3*LB~TD5*****UPS Collect 3rd Party~REF*BM*0000019330780~DTM*011*20110110~N1*ST**92*001~HL*2*1*O~PRF*0107WAINWRIGHT***20110107~TD1*CNT25*1****G*2.5*LB~HL*3*2*P~MAN*GM*00000848810196098727~HL*4*3*I~LIN**UP*883905366767*VA*MR993BK*SZ*08~SN1**1*EA~HL*5*1*O~PRF*0107MERRIGAN***20110107~TD1*CNT25*1****G*2.8*LB~HL*6*5*P~MAN*GM*00000848810196105005~HL*7*6*I~LIN**UP*827743147737*VA*MW811VK*SZ*11~SN1**1*EA~CTT*7~SE*26*0038~GE*9*12~IEA*1*000000012~";

        static void Main(string[] args)
        {
            var ediReceive = new X12Parser().ParseMultiple(Edi);
            var ediDocument = new XmlDocument();
            var interchange = ediReceive[0];
            var jsonEdi = JsonConvert.SerializeObject(interchange);
        }
    }
}