2

I am trying to create an extension class of JObject, which is immutable.

If I create a class like below, can it prevent the actual JObject being modified?

public class ImmutableJObejct: JObject
{
    public readonly JObject JObject;
    public ImmutableJObejct(JObject jObject)
    {
        this.JObject = jObject;
    }
}

Which is the right approach? should I not be expending the code and implement each methods that I need from JOBject?

3
  • This would make the reference readonly, but not the object instance itself. You couldn't assign directly to the JObject field, but you could call it's Add method and change the value. Commented Nov 15, 2017 at 1:42
  • 2
    It's not currently implemented. Your question is a duplicate of How to make Newtonsoft.Json.Linq.JObject immutable? which links to an open issue requesting this functionality: Support freezing a JToken #468 Commented Nov 15, 2017 at 2:27
  • Thanks for the reference, I wasn't able to find the question mentioned Commented Nov 15, 2017 at 19:45

2 Answers 2

2

To answer your question: Yes and no. Or "it depends".

There are basically 2 kinds of immutable objects: "shallow" immutable objects where all properies are readonly and does not have any methods that modify state, and "deep" immutable objects where not only are all properties readonly, they are also either themselves immutable or copies of a mutable object.

Your example is an example of a shallow immutable object. It doesn't allow you to give your ImmutableJobject a new JOject once it is created, but you can possibly change properties on that object. To make it a deep immutable object, the field JObject needs to be changed to a readonly property, that creates a new JObject everytime it is accessed.

Update: As with all things, immutablility comes with cost and benefits. The primary cost is in the need to create more objects, either to capture a new state, or to keep deep immutable objects from having their properties changed. Depending upon how many such objects are created and how long they are tracked/referenced, this can impact both cpu and memory. Unless you control all of the objects in the object's graph/hierarchy, there is little you can do to avoid this cost.

Sign up to request clarification or add additional context in comments.

1 Comment

If new JObject is created everytime it's access, wouldn't it be heavy on memory usage? is there a work-around for such concern?
2

Basically you could return a new instance of JObject each time.

public class ImmutableJObejct
{
    private readonly JObject _jObject;

    public ImmutableJObejct(JObject jObject)
    {
        _jObject = jObject;
    }

    public JObject JObject
    {
        get { return new JObject(_jObject); }
    }
}

1 Comment

I went with returning deep copy of new JObject for every request. However, There is a concern in memory usage. what are possible work-around for such issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.