Compare json values.

About Monkey 2 Forums Monkey 2 Code Library Compare json values.

This topic contains 5 replies, has 3 voices, and was last updated by  Mark Sibly 1 year, 3 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #13659

    Mark Sibly
    Keymaster

    Here’s some code to compare json values.

    I initially tried to add something like this to the JsonValue class using the spaceship operator ‘<=>’, but this lead to some highly recursive problems because the spaceship operator redfines plain object compare etc, so stuff like ‘If Self=NullValue’ would cause the stack to explode!

    This highlights the difficulty involved in overloading comparison operators for reference types (ie: classes) and it’s something I’d recommend avoiding altogether. It’s still quite all right to provide comparison operators for value types though (ie: structs).

    It also underlines the need for more flexible comparison functionality in things like Map, to allow you to create eg: a  Map<JsonValue,Blah> using code like this without having to define <=> for JsonValue.

    #13660

    Ethernaut
    Participant

    So, let me see if I understand correctly… if the json values are identical it returns zero?
    (I always forget what the “<=>” operator does…)

    #13661

    Mark Sibly
    Keymaster

    Yes, x<=>y produces…

    an int value=0 if x=y,

    an int value<0 if x<y or

    an int value>0 if x>y.

    #13664

    nerobot
    Participant

    There may be a virtual method CompareTo:Int( value:JsonValue ) of JsonValue.

    By default it will return -1.

    And any sub-classes will override it.

    Technically it will be the same as your functions above.

    The difference is a way of call: json1.CompareTo( json2 ) in this case.

    If we aware of nullability of json1, then we can use ?. operator.

    Also in your code you check y=NullValue and return 1 if true, but x can be null itself and then we should get zero in result.

    Sorry, I did’n understand about stack exploding reason. 🙂

    If we use just functions like ones above – do we avoid stack exploding when using if Self.IsNull?

    (JsonValue has a property IsNull.)

    #13665

    nerobot
    Participant

    If we declare new sub-class JsonNull extends JsonValue and replace current NullValue with them – will it helps us to check if x is null-value w/o any stack-ish problems?

    #13667

    Mark Sibly
    Keymaster

    Yes, adding a JsonNull class is probably a better way to do the null value thing, and would prevent at least that particular stack explosion. Are there any others?

    I’m still a bit weird about comparing references in fancy ways, just been bitten by it so many times in c++ perhaps. I probably just need to go ahead and do it?

    Also in your code you check y=NullValue and return 1 if true, but x can be null itself and then we should get zero in result.

    Where in the code? There is a check in the ‘root’ CompareJon for when x and y are both null and it returns 0 there. This is the only place where x can be null, and in all other cases, x is always > null.

    The little RndJson test seems to work OK too.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.