Adrien Budet
Adrien Budet

Reputation: 337

JArray.Contains issue

I have a JArray, read from a file :

private void RemoveCatalog(Catalog catalog) {

    System.IO.StreamReader filereader = new System.IO.StreamReader(@appDirectory + "\\list");

    JArray myjarray = JArray.Parse(filereader.ReadToEnd());
    filereader.Close(); 

    string json = " {\"token\":\"" + catalog.Token + "\",\"name\":\"" + catalog.Name +"\",\"logo\":\"" + catalog.Logo + "\",\"theme\":\"" + catalog.Theme + "\"}";

    JObject myCatalogAsJObject = JObject.Parse(json);

    myjarray.Remove(myCatalogAsJObject);

}

I want to remove the JObject corresponding to myCatalogAsJObject variable, but it doesn't work, because the answer of myjarray.Contains(myCatalogAsJObject) is false.

The problem is that myjarray actually contains it : it's the only JObject in my JArray.

If I do myCatalogAsJObject.ToString().Equals(myjarray.First.ToString()), the answer is true however.

I'm stuck.

Upvotes: 6

Views: 11791

Answers (3)

JGilmartin
JGilmartin

Reputation: 9298

I had this issue and could not getting it working with the suggested extension method - so I've created an extension that uses JToken.DeepEquals which works well.

below is an example

public static class JsonExtensions
{
    public static bool ContainsJson(this JArray arr, JToken item)
    {
        return arr.Any(it =>
        {
            return JToken.DeepEquals(it, item);
        });
    }
}

Upvotes: -1

Ohad Schneider
Ohad Schneider

Reputation: 38162

As for the Contains part of your question, here's my take on a typed JArray.Contains:

public static bool ContainsTyped<T>(this JArray arr, T item)
{
    return arr.Any(it =>
    {
        T typed;
        try
        {
            typed = it.ToObject<T>();
        }
        catch (JsonException e)
        {
            Console.WriteLine("Couldn't parse array item {0} as type {1}: {2}", it, typeof(T), e);
            return false;
        }

        return typed.Equals(item);
    });
}

Now simply implement Equals on your type (e.g. Catalog) and call arr.ContainsTyped(catalog).

Upvotes: 3

D Stanley
D Stanley

Reputation: 152634

.Contains (and .Remove) by default will compare references. Since you're creating a new JObject, the array does not contain that instance.

You could get the instance of the object from the array and remove that:

JObject match = myjarray.FirstOrDefault(j => j.token == catalog.token &&
                                             j.name  == catalog.name  &&
                                             j.logo  == catalog.logo  &&
                                             j.theme == catalog.theme);

myjarray.Remove(match);

EDIT : Here is your code, simplified :

JToken match = myjarray.FirstOrDefault(j => j.ToString().Equals(myCatalogAsJObject.ToString()));

myjarray.Remove(match);

Upvotes: 11

Related Questions