Hi!

This week I wrote simple serialization and deserialization for json format so it's possible now to encode objects from and to json:


import django.core.serializers as s

class Foo(object):
    def __init__(self):
        self.bar = [Bar(), Bar(), Bar()]
        self.x = "X"

class Bar(object):
    def __init__(self):
        self.six = 6

class MyField2(s.Field):
    def deserialized_value(self, obj, instance,  field_name):
        pass

class MyField(s.Field):
    x = MyField2(label="my_attribute", attribute=True)

    def serialized_value(self, obj, field_name):
        return getattr(obj, field_name, "No field like this")

    def deserialized_value(self, obj, instance,  field_name):
        pass

class BarSerializer(s.ObjectSerializer):
    class Meta:
        class_name = Bar

class FooSerializer(s.ObjectSerializer):
    my_field=MyField(label="MYFIELD")
    bar = BarSerializer()
    class Meta:
        class_name = Foo


foos = [Foo(), Foo(), Foo()]
ser = s.serialize('json', foos, serializer=FooSerializer, indent=4)
new_foos = s.deserialize('json', ser, deserializer=FooSerializer)


There are cases that I don't like:

 * deserialized_value function with empty content - what to do with
   fields that we don't want to deserialize. Should be better way to
   handle this,
 * I put list foos but return generator new_foos, also bar in Foo
   object is generator, not list like in input. Generators are better
   for performance but if I put list in input I want list in output,
   not generator. I don't know what to do with this.


Next week I will handle rest of issues that I mentioned in my last week check-in and refactor json format (de)serialization - usage of streams and proper parameters handling (like indent, etc.)

--
Piotr Grabowski




--
You received this message because you are subscribed to the Google Groups "Django 
developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to