Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

Support freezing a JToken

Open sharwell opened this issue 10 years ago • 7 comments

I'd like to propose the ability to "freeze" a JToken. A frozen JToken is essentially read-only; any attempt to change one of its properties results in an InvalidOperationException.

  • The term "a JToken" refers to any instance of a class derived from JToken.
  • When a JToken is initially created, it is not frozen.
  • When a JToken is frozen, all children of the JToken are frozen as well.

No changes would be made to any current public API.

I would be happy to implement this functionality and send a pull request if you like the idea.

sharwell avatar Jan 20 '15 20:01 sharwell

This is a little easier now that there is JsonLoadSettings. Could add a flag to that that loaded tokens should be frozen.

JamesNK avatar Jan 02 '16 02:01 JamesNK

Any news on this? I just stumbled into a problem which I could solve nicely with immutable JObjects

BalassaMarton avatar Nov 18 '16 10:11 BalassaMarton

Any update with this one?

Avivskie avatar Aug 13 '19 10:08 Avivskie

This feature would be useful for records and other immutable types.

scalablecory avatar Aug 18 '22 23:08 scalablecory

Any news on this ? JObject immutability would be incredibly usefull also for performances reasons, in all those scenarios where a datamodel represented as JObject must be passed to multiple consumers, but it must be ensured that such consumers don't alter it. Now the only way to obtain this, is to pass the source JSON STRING and parse it every time inside the logics of every consumer, which is a huge waste of CPU and RAM.

NinjaCross avatar Nov 10 '23 07:11 NinjaCross

I'm pretty sure that there will be no new features in NSJ since the project is entirely in frozen state. MS has switched to STJ and all development is happening there.

sungam3r avatar Nov 14 '23 11:11 sungam3r

How about to use DeepClone method ?

Soar360 avatar Nov 23 '23 05:11 Soar360