Resolving Equality for PyJsonObjectAdapter

Hey everyone,

I am trying to check a diff between a previous tag value and current tag value in event streams and I'm running into issues with the equality semantics. I am pointing the tag source to an aggregation of tags via jsonvalues to be sent to external systems. I essentially need to filter out the aggregates that only have noisy tag changes like a heartbeat tag.

Without encoding and decoding from json (performance reasons) or recursively checking each field, is there a way to get a proper equality comparison? I'm hoping I am just missing something.

Example script:

from com.inductiveautomation.ignition.common.gson import JsonObject
from com.inductiveautomation.ignition.common.script.adapters import PyJsonObjectAdapter

# Assuming this is the correct way to construct PyJsonObjectAdapter to mimic Event Stream object type
jsonObject1 = JsonObject()
jsonObject1.addProperty("State", "Running")
jsonObject1.addProperty("Mode", "Auto")

jsonObject2 = JsonObject()
jsonObject2.addProperty("State", "Running")
jsonObject2.addProperty("Mode", "Auto")

value1 = PyJsonObjectAdapter(jsonObject1)
value2 = PyJsonObjectAdapter(jsonObject2)

print "value1 type: {}".format(type(value1))
print "value2 type: {}".format(type(value2))
print "value1: {}".format(value1)
print "value2: {}".format(value2)
# Expected false
print "Same object: {}".format(value1 is value2)
# Expected true based on contents. Seems as though it is falling back to identity comparison
print "'Python' equality: {}".format(value1 == value2)

print "---"
print "Field comparison:"
print "State equal: {}".format(value1["State"] == value2["State"])
print "Mode equal: {}".format(value1["Mode"] == value2["Mode"])

Output:

value1 type: <type 'com.inductiveautomation.ignition.common.script.adapters.PyJsonObjectAdapter'>
value2 type: <type 'com.inductiveautomation.ignition.common.script.adapters.PyJsonObjectAdapter'>
value1: {"State":"Running","Mode":"Auto"}
value2: {"State":"Running","Mode":"Auto"}
Same object: 0
'Python' equality: 0
---
Field comparison:
State equal: 1
Mode equal: 1

Javadoc reference:

EDIT:

Found the implementation for __eq__ that PyJsonObjectAdapter inherits from.

Any pointers much appreciated!

How else could a structural equality comparison work?

If you want deep equals, you have to do the recursive traversal.

If you can express your algorithm more cheaply or via a shortcut (e.g. only compare equality on keys) it's easy enough to write that out yourself.

In theory, if you can get the underlying JsonObject out of the adapter, you can use the equals method on it directly, which at least saves you writing the recursion yourself.

That's my goal :smiley: thanks!