class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(default='A') b = jsonobject.ReadonlyJSONProperty(default='B') @jsonproperty def c(self): return 'C'
class AnotherObject(SomeObject): d = jsonobject.JSONProperty(default='D') e = jsonobject.ReadonlyJSONProperty(default='E') @jsonproperty def f(self): return 'F'
class SomeObject(jsonobject.JSONSerializableObject): # Though it's not recommended for most use cases, JSON properties # can be created dynamically at instance creation time. # This is sometimes useful for dynamically importing unknown JSON # but do not abuse this. This is tricky. For readability, an # ordinary property should be explicitly declared at the class # level. def __init__(self, **kwargs): # Dynamically create a new type, because properties # (to be precise, descriptors) works if and only if owned by a # class object. cls = type('__{}_{}'.format(self.__class__.__name__, id(self)), (jsonobject.JSONSerializableObject, ), dict(self.__class__.__dict__)) # Change the class of this instance. self.__class__ = cls # Create properties dynamically and add to the dynamically # created class object. You may want to import not from kwargs, # and use JSON instead. for key, value in kwargs.items(): if not hasattr(cls, key): setattr( cls, key, jsonobject.JSONProperty(name=key, default=value)) # Set the value using JSONSerializableObject's constructor. super(cls, self).__init__(**kwargs) # Note that, this class level property will be present in the new # dynamically created class, because self.__class__.__dict__ is # passed to type() above. foo = jsonobject.JSONProperty(default='FOO')
class SomeObject(jsonobject.JSONSerializableObject): # In Python 3, you use str everywhere. You can still pass unicode # strings. Follow this style also if you need to support both # Python 2 and 3. # If you don't need to support Python 2, you would drop 'u' literal # prefix. a = jsonobject.JSONProperty(default=u'A', value_type=str)
class SomeObject(jsonobject.JSONSerializableObject): # This is a bit simple version of dynamic property creation. # This way the class definition is much simpler... def __init__(self, **kwargs): super(SomeObject, self).__init__(**kwargs) # bar is a dynamically created property. You must provide the # property name. self.bar = jsonobject.JSONProperty(name='bar', default='BAR') foo = jsonobject.JSONProperty(default='FOO')
def __init__(self, **kwargs): cls = self.__class__ # Create properties dynamically and add to the shared class # object. for key, value in kwargs.items(): if not hasattr(cls, key): setattr( cls, key, jsonobject.JSONProperty(name=key, default=value)) # Set the value using JSONSerializableObject's constructor. super(cls, self).__init__(**kwargs)
class SomeObject(jsonobject.JSONSerializableObject): # Do not do something like this! # This is a bad example of a dynamic property, where I store a # property to a shared class object, which will produce a side # effect for other instances. def __init__(self, **kwargs): cls = self.__class__ # Create properties dynamically and add to the shared class # object. for key, value in kwargs.items(): if not hasattr(cls, key): setattr( cls, key, jsonobject.JSONProperty(name=key, default=value)) # Set the value using JSONSerializableObject's constructor. super(cls, self).__init__(**kwargs) foo = jsonobject.JSONProperty(default='FOO')
def __init__(self, **kwargs): # Dynamically create a new type, because properties # (to be precise, descriptors) works if and only if owned by a # class object. cls = type('__{}_{}'.format(self.__class__.__name__, id(self)), (jsonobject.JSONSerializableObject, ), dict(self.__class__.__dict__)) # Change the class of this instance. self.__class__ = cls # Create properties dynamically and add to the dynamically # created class object. You may want to import not from kwargs, # and use JSON instead. for key, value in kwargs.items(): if not hasattr(cls, key): setattr( cls, key, jsonobject.JSONProperty(name=key, default=value)) # Set the value using JSONSerializableObject's constructor. super(cls, self).__init__(**kwargs)
class ChildObject(jsonobject.JSONSerializableObject): bar = jsonobject.JSONProperty(default='BAR')
class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(default=123, value_type=long)
class SomeObject(jsonobject.JSONSerializableObject): foo = jsonobject.JSONProperty(value_type=int)
class AnotherObject(SomeObject): bar = jsonobject.JSONProperty(value_type=int)
class SomeObject(jsonobject.JSONSerializableObject): # There may be a case where the property name in Python and JSON # differs, especially if the name is keywords like "or", "and" or # such. foo_ = jsonobject.JSONProperty(name='foo', value_type=int)
class SomeObject(jsonobject.JSONSerializableObject): # These name are swapped in JSON world. Such a situation should # never happen in reality, but in the specification, this is still # allowed. foo = jsonobject.JSONProperty(name='bar', value_type=int) bar = jsonobject.JSONProperty(name='foo', value_type=int)
class SomeObject(jsonobject.JSONSerializableObject): foo = jsonobject.JSONProperty(default=u'FOO', value_type=str)
class AnotherObject(jsonobject.JSONSerializableObject): bar = jsonobject.JSONProperty(value_type=tuple, element_type=SomeObject)
class SomeObject(jsonobject.JSONSerializableObject): # default speficies the default value of the property. foo = jsonobject.JSONProperty(default='FOO')
class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(value_type=dict)
class SomeObject(jsonobject.JSONSerializableObject): # Dict passed as the default value. This should not be reused over # object instances. foo = jsonobject.JSONProperty(default=ChildObject())
class SomeObject(jsonobject.JSONSerializableObject): foo = jsonobject.JSONProperty()
class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(default='A') b = jsonobject.JSONProperty(default='B') def update_a(self, value): self.a = value
class SomeObject(jsonobject.JSONSerializableObject): # In Python 2, prefer using value_type of unicode if you pass text. a = jsonobject.JSONProperty(default=u'A', value_type=unicode) b = jsonobject.JSONProperty(default=123, value_type=int) c = jsonobject.JSONProperty(default='C')
class AnotherObject(SomeObject): # You can override some properties exported by the superclass. a = jsonobject.JSONProperty(default='AA')
class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(default='A', value_type=int)
class SomeObject(jsonobject.JSONSerializableObject): a = jsonobject.JSONProperty(value_type=tuple, element_type=int)
def __init__(self, **kwargs): super(SomeObject, self).__init__(**kwargs) # bar is a dynamically created property. You must provide the # property name. self.bar = jsonobject.JSONProperty(name='bar', default='BAR')
class SomeObject(jsonobject.JSONSerializableObject): # This does the same thing as @jsonproperty getter, setter and # deleter. # Note that the JSON name is optional. If omitted, it is inferred # from the Python field name. foo = jsonobject.JSONProperty()