Skip to content

Json: How to handle not existing member #952

Closed

Description

@paule96

Currently, I am trying to implement a maybe weird case in my internal project. The basic concept is that any JSON object can be part of a JSON array.

So something like that:

[
   {
      "animal": "cat"
   },
   {
      "color": "green"
   }
]

If I now try to run something like that:

JsonDocument data = null; // something sets the value of JsonDocument so it's not null
data.Any(config: System.Linq.Dynamic.Core.SystemTextJson.Config.SystemTextJsonParsingConfig.Default, "x => x.number == 5"));

I will now get an exception like that:

The binary operator Equal is not defined for the types 'System.Object' and 'System.Int32'.

My question is, is there some kind of expression that I can write, that makes it possible to make a member optional?

Or some setting that I can set?

Activity

  1. self-assigned this
    on Oct 25, 2025
  2. paule96 commented on Oct 30, 2025

    @paule96
    Author

    @StefH
    We tried this but it doesn't help us.
    But I created reproduction tests with the commit:

    paule96@c0b8e02

    The following tests are falling:

    • compare a non exsisting value with a number value (notExisting > 1)
    • compare a non exsisting value with a boolean value (notExisting == true)

    Comparing to strings are just working

  3. changed the title [-]How to handle not existing member[/-] [+]SystemTextJson: How to handle not existing member[/+] on Nov 1, 2025
  4. added and removed on Nov 1, 2025
  5. StefH commented on Nov 1, 2025

    @StefH
    Collaborator

    @paule96
    OK, thanks for the unit tests.

    However, I would expect that when you try to query an non-existing member, you would get error like:
    Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'xxx' does not contain a definition for 'number'

  6. paule96 commented on Nov 1, 2025

    @paule96
    Author

    Yes I would expect the same for the default path.
    But also when you work with json, it's kinda normal to miss members.

    So it would be nice to have a syntax or a configuration that allows to compare to non existing members.

  7. changed the title [-]SystemTextJson: How to handle not existing member[/-] [+]Json: How to handle not existing member[/+] on Nov 2, 2025
  8. StefH commented on Nov 2, 2025

    @StefH
    Collaborator
  9. StefH commented on Nov 2, 2025

    @StefH
    Collaborator

    I did fix the code so that your unit tests pass. However you will need to provide ConvertObjectToSupportComparison = true and missing members are not yet supported.

  10. paule96 commented on Nov 3, 2025

    @paule96
    Author

    very cool thank you. We will try it out the next days

  11. StefH commented on Nov 5, 2025

    @StefH
    Collaborator

    I will probably create new version this weekend

  12. paule96 commented on Nov 10, 2025

    @paule96
    Author

    @StefH we found one scenario that may produce invalid results. But it's a bit hard to decide because we are in a world between JSON and C#.

    x.notExsting == false

    This currently returns true. But actually, because in JS/JSON world x.notExsting is undefined and not false, it should return false.

  13. StefH commented on Nov 10, 2025

    @StefH
    Collaborator

    What if you compare with a nullable bool with value false?

  14. paule96 commented on Nov 10, 2025

    @paule96
    Author

    How do you mean? From a plain C# view like that:

    bool? test = null;
    var result = test == false;
    Console.WriteLine("Hello " + result);

    The result would be Hello false.

  15. paule96 commented on Nov 13, 2025

    @paule96
    Author

    @StefH ping :)

  16. StefH commented on Nov 14, 2025

    @StefH
    Collaborator

    I will take a look this weekend.

    Probably I will add a bool config setting with default to true to normalize the json.

  17. StefH commented on Nov 15, 2025

    @StefH
    Collaborator

    This PR should solve this:
    #958

  18. paule96 commented on Nov 15, 2025

    @paule96
    Author

    Very cool. Will try ut next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions