0

The encoding is basically string representation of a dictionary, containing the object's fields. However, a dictionary does not respect order, and I could potentially get different encoding string on different runs. How do I preclude this from happening? Or should I use another library where I can ensure deterministic encoding?

By deterministic encoding, I mean if I create 100000 objects that are practically the same, i.e. same class and same constructor args, when I call encode() on each one of them, I get the exact same string every time.

So, for example, if I have

class MyClass(object):
   def __init__(self, a, b):
      self.a = a
      self.b = b

c1 = MyClass(1, 2)

c2 = MyClass(1, 2)

I want to be sure that the strings encode(c1) and encode(c2) are perfectly identical, character for character, i.e.

assert jsonpickle.encode(c1)==jsonpickle.encode(c2)
Baron Yugovich
  • 3,843
  • 12
  • 48
  • 76

1 Answers1

0

I think that jsonpickle will take care of what you call determininstic endocing.

Example

import jsonpickle
class Monopoly(object):

    def __init__(self):
        self.boardwalk_price = 500

    @property
    def boardwalk(self):
        self.boardwalk_price += 50
        return self.boardwalk_price



m = Monopoly()
serialized = jsonpickle.encode(m)

Take a look at

print (serialized)
{"py/object": "__main__.Monopoly", "boardwalk_price": 500}

Now, let's decode

d = jsonpickle.decode(serialized)
print (d)
<__main__.Monopoly object at 0x7f01bc093278>
d.boardwalk_price
500

For comparing objects,Python uses identifiers.

class MyClass(object):
   def __init__(self, a, b):
      self.a = a
      self.b = b

c1 = MyClass(1, 2)
c2 = MyClass(1, 2)

If you take a look at id

id(c1)
140154854189040
id(c2)
140154854190440
c1 == c2

False

You can override eq operator

def __eq__(self, x):
    if isinstance(x, number):
        return self.number == x.number
    return False
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121