Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoid expanding linked objects when serialising
    text
    copied!<p>I am using JSON.NET to serialize some c# objects into JSON (and then write to a file).</p> <p>My two main classes are:</p> <pre><code>public class Reservoir { private Well[] mWells; public Well[] wells { get { return mWells; } set { mWells = value; } } } </code></pre> <p>and</p> <pre><code>public Well() { private string mWellName; private double mY; private double mX; public string wellName { get { return mWellName; } set { mWellName = value; } } public double y { get { return mY; } set { mY = value; } } public double x { get { return mX; } set { mX = value; } } private Well[] mWellCorrelations; } </code></pre> <p>The problem is that the output looks like:</p> <pre><code>'{"wells":[{"wellName":"B-B10","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B12","y":152.71005958395972,"x":459.02158140110026,"wellCorrelations":[{"wellName":"B-B13","y":475.0,"x":495.14804408905263,"wellCorrelations":[{"wellName":"B-B11","y":25.0,"x":50.0,"wellCorrelations":[]} </code></pre> <p>i.e. the associated wells of each well object are expanded as objects themselves and this becomes a serious problem of space and time when there lots of associated objects.</p> <p>I suppose I would have preferred something like:</p> <pre><code>'{"wells":[{"wellName":"B-B10","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B12"}], {"wellName":"B-B11","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B13"} </code></pre> <p>i.e maintaining only the well name as the link (assume its unique).</p> <p>Is there a way to do this with JSON.NET?</p> <p>You have set</p> <pre><code>serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; </code></pre> <p>but it doesn't make any difference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload