0

I have one situation where A method runs some LINQ Query on a JArray and returns me an IEnumerable.

On top of that I want to run a JSONPATH Query using the SelectTokens method, but the SelectToken method is obviously not available on IEnumerable<>.

So what I have done was simply created an JArray out of the IEnumerable.

new JArray(myIEnumerable);

The Problem is that when I do this, the elements are copied, but I would really need to have them as reference (preserving same Hashcode).

I tried many different cases from setting PreserveReferencesHandling on SerializerSettings or by trying to add items manually...

var myNewArray = new JArray();
foreach(var item in myIEnumerable)
{               
    myNewArray.Add(item);
    //item.GetHashCode not the same as the just added item!!!! :-(
}

As I really need to keep the reference while still being able to call the JSonPAth(SelectToken, SelectTokens), does anyone have an idea how could I achive this?

Best Regards,

PCBL

PCBL
  • 71
  • 1
  • 4
  • JSON doesn't have references. It's a text format. [JSON.NET's object references](https://www.newtonsoft.com/json/help/html/preserveobjectreferences.htm) using `$id` and `$ref` are a custom convention that has no meaning at all for other serializers. An object's hash code is *not* guaranteed to be the same from one application execution to the next. The only guarantee is that the same object *instance* will produce the same hash code. – Panagiotis Kanavos May 27 '22 at 11:45
  • Hey @PanagiotisKanavos, The thing is that if I make changes on JTokens within my original JArray I can see that the references are applied to the array or IEnumerables that refer to it. The issue on my case is that I really need to create a JArray preserving the references, but it seems not doable. – PCBL May 27 '22 at 11:49
  • I repeat, JSON is a text format (it's just a string) and has no references. It doesn't deal with hash codes either. A JArray doesn't contain arbitrary objects, only JToken instances. Those instances don't refer to your DTOs, they contain simple values. The question is unclear as it is. Post an example of your C# classes, the desired JSON and what you got. – Panagiotis Kanavos May 27 '22 at 11:50
  • @PanagiotisKanavos, I even considered creating an [SelectToken](https://github.com/JamesNK/Newtonsoft.Json/blob/5803c7722af4cf0dc3984e55404694aab09e51bf/Src/Newtonsoft.Json/Linq/JToken.cs#L2346) Extension Method on IEnumerable<> but the [JPath](https://github.com/JamesNK/Newtonsoft.Json/blob/5803c7722af4cf0dc3984e55404694aab09e51bf/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs#L34) class is internal, which makes it a bit harder.... – PCBL May 27 '22 at 11:52
  • @PanagiotisKanavos, the JSON is irrelevanmt on this case..... I am talking that: 1. Initially I have a JArray 2. I make a Linq Query on this JArray, producing a IEnumerable 3. I Need toi runa. JPath agains the IEnumerable, then I convert it to JArray 4. The results from JPath are copy(no reference) of the original JArray – PCBL May 27 '22 at 11:53
  • The last comment is self-contradictory. If you talk about JArray you talk about JSON. A JArray only contains JToken instances. Those instances can only be containers or [simple values](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JValue.htm) – Panagiotis Kanavos May 27 '22 at 11:54
  • The Reference I am mening is a C# Reference, no $ref or anything like that. It is just c# reference. – PCBL May 27 '22 at 11:56
  • This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - you have a problem X and assume Y is the solution. When that doesn't work, you ask about Y, not the actual problem X. What is the actual problem you want to solve? Why are you talking about references, paths and hash codes? – Panagiotis Kanavos May 27 '22 at 11:56
  • `It is just c# reference.` there's no such thing in a JArray or the JTokens it contains. It's not needed. JSON.NET represent a JSON document's parse tree, they aren't some kind of generic container – Panagiotis Kanavos May 27 '22 at 11:56
  • @PanagiotisKanavos,this question tags [Json.Net](https://www.newtonsoft.com/json), so is c# and Json.net related. Basically I read a JSON and manipulate it on my C# Program using Newtonsoft Json.Net. And the problem is simpler as you can imagine. I Have a JArray, which contain JTokens. If I change a property in one of this tokens, it will be reflected on any variables point to them. My issue is that once I create a new JArray but initialize it from an IEnumerable, if I change something on a JToken within that array, it not automatically updates the JToken on the IEnumerable... – PCBL May 27 '22 at 12:05
  • 1
    If you want to add a `JToken` to an array **without cloning the `JToken`**, you must first remove it from its current parent, because a `JToken` cannot have two parents at once. For why, see [this answer](https://stackoverflow.com/a/29260565/3744182) to [nested json objects dont update / inherit using Json.NET](https://stackoverflow.com/q/29255489/3744182) -- which looks to be a duplicate based on [this comment](https://stackoverflow.com/questions/72404928/create-jarray-from-ienumerablejtoken-but-preserve-references#comment127908665_72404928) – dbc May 27 '22 at 12:58

0 Answers0