Convert JsonPatch instance to JSON string in nicer way.

This commit is contained in:
Alexander Shorin
2011-12-25 21:40:27 +04:00
parent d20e3a6f25
commit 17c634f6aa

View File

@@ -146,12 +146,20 @@ class JsonPatch(object):
'test': TestOperation
}
def __str__(self):
"""str(self) -> self.to_string()"""
return self.to_string()
@classmethod
def from_string(cls, patch_str):
"""Creates JsonPatch instance from string source."""
patch = json.loads(patch_str)
return cls(patch)
def to_string(self):
"""Returns patch set as JSON string."""
return json.dumps(self.patch)
def apply(self, obj):
"""Applies the patch to given object."""