예제 #1
0
        class SomeObject(jsonobject.JSONSerializableObject):
            a = jsonobject.JSONProperty(default='A')
            b = jsonobject.ReadonlyJSONProperty(default='B')

            @jsonproperty
            def c(self):
                return 'C'
예제 #2
0
        class AnotherObject(SomeObject):
            d = jsonobject.JSONProperty(default='D')
            e = jsonobject.ReadonlyJSONProperty(default='E')

            @jsonproperty
            def f(self):
                return 'F'
예제 #3
0
        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')
예제 #4
0
 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)
예제 #5
0
        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')
예제 #6
0
 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)
예제 #7
0
        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')
예제 #8
0
 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)
예제 #9
0
 class ChildObject(jsonobject.JSONSerializableObject):
     bar = jsonobject.JSONProperty(default='BAR')
예제 #10
0
 class SomeObject(jsonobject.JSONSerializableObject):
     a = jsonobject.JSONProperty(default=123, value_type=long)
예제 #11
0
 class SomeObject(jsonobject.JSONSerializableObject):
     foo = jsonobject.JSONProperty(value_type=int)
예제 #12
0
 class AnotherObject(SomeObject):
     bar = jsonobject.JSONProperty(value_type=int)
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
0
 class SomeObject(jsonobject.JSONSerializableObject):
     foo = jsonobject.JSONProperty(default=u'FOO', value_type=str)
예제 #16
0
 class AnotherObject(jsonobject.JSONSerializableObject):
     bar = jsonobject.JSONProperty(value_type=tuple,
                                   element_type=SomeObject)
예제 #17
0
 class SomeObject(jsonobject.JSONSerializableObject):
     # default speficies the default value of the property.
     foo = jsonobject.JSONProperty(default='FOO')
예제 #18
0
 class SomeObject(jsonobject.JSONSerializableObject):
     a = jsonobject.JSONProperty(value_type=dict)
예제 #19
0
 class SomeObject(jsonobject.JSONSerializableObject):
     # Dict passed as the default value. This should not be reused over
     # object instances.
     foo = jsonobject.JSONProperty(default=ChildObject())
예제 #20
0
 class SomeObject(jsonobject.JSONSerializableObject):
     foo = jsonobject.JSONProperty()
예제 #21
0
        class SomeObject(jsonobject.JSONSerializableObject):
            a = jsonobject.JSONProperty(default='A')
            b = jsonobject.JSONProperty(default='B')

            def update_a(self, value):
                self.a = value
예제 #22
0
 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')
예제 #23
0
 class AnotherObject(SomeObject):
     # You can override some properties exported by the superclass.
     a = jsonobject.JSONProperty(default='AA')
예제 #24
0
 class SomeObject(jsonobject.JSONSerializableObject):
     a = jsonobject.JSONProperty(default='A', value_type=int)
예제 #25
0
 class SomeObject(jsonobject.JSONSerializableObject):
     a = jsonobject.JSONProperty(value_type=tuple, element_type=int)
예제 #26
0
 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')
예제 #27
0
 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()