mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Serialization / deserialization

Open rozza opened this issue 13 years ago • 14 comments

Create a serializer and deserializer for mongodb documents...

Useful for testing / small data exporting..

Refs: #181

rozza avatar Jun 27 '11 13:06 rozza

from #230

example:

#serializing Person.objects.serialize('xml') Person.objects.serialize('yaml')

a = open('output.xml', 'wb') Person.objects.serialize('xml', fields=('data.name', 'endr'), out=a)

#deserialize data = open('input.xml', 'rb').read() objs = Person.objects.deserialize('xml', data)

for obj in objs; obj.save()

rozza avatar Jul 12 '11 13:07 rozza

IMO it'd be cool to have serialization/deserialization helper classes instead hooking it with queryset. My examples are inspired from django project.

data = serializers.serialize("xml", SomeModel.objects.all())
data = serializers.serialize("json", SomeModel.objects.all(), excludes=('password',))

def get_absolute_url(self): #document class method
    return "/user/%d" % self.pk

data = serializers.serialize("json", SomeModel.objects.all(), excludes=('password',),
                                                     extras=[('url', 'get_absolute_url'),])

for deserialized_object in serializers.deserialize("xml", data):
    if object_should_be_saved(deserialized_object):
        deserialized_object.save()

If someone try to load partial data into deserializer, it should work until it's valid but may not able to save it if constraint fails.

iapain avatar Jul 12 '11 20:07 iapain

@rozza. I shall work on that feature in the next days. is a very important feature for various projects.

wpjunior avatar Sep 26 '11 18:09 wpjunior

I am starting work in this branch: https://github.com/wpjunior/mongoengine/tree/serializers

wpjunior avatar Sep 26 '11 19:09 wpjunior

Example Encoder - https://gist.github.com/1372780

rozza avatar Nov 17 '11 09:11 rozza

Great, thanks for the pointer. Updated version to use json over simplejson, and dbref over pymongo:

https://gist.github.com/2726279

benjiec avatar May 18 '12 16:05 benjiec

I had to make a serializer for the app i'm working on. It surely requires some improvements, but it seems to work. It serializes querysets / lists of querysets. I wouldn't use it to serialize huge datasets however.

https://gist.github.com/3009143

benwah avatar Jun 28 '12 04:06 benwah

Good stuff! I after europython I will have time look into this.

Ross

On Thu, Jun 28, 2012 at 5:31 AM, benwah < [email protected]

wrote:

FYI.. the reason it's kinda long, is that I wanted to serialize things like lists of querysets, for json views, like so:

[AdCategory.objects.all(), Region.objects.all()]

I could do it with a simple serializer and string formatting, but I though building json using string formatting wasn't especially pretty.


Reply to this email directly or view it on GitHub: https://github.com/hmarr/mongoengine/issues/210#issuecomment-6621706

rozza avatar Jun 28 '12 08:06 rozza

Any progress on this?

mahmoudhossam avatar Sep 09 '12 08:09 mahmoudhossam

+1

honzajavorek avatar Oct 16 '12 13:10 honzajavorek

0.8 is underway :)

On Tue, Oct 16, 2012 at 2:37 PM, Honza Javorek [email protected]:

+1

— Reply to this email directly or view it on GitHubhttps://github.com/hmarr/mongoengine/issues/210#issuecomment-9488088.

rozza avatar Oct 16 '12 14:10 rozza

+1

CosmicAnalogue465 avatar Jun 19 '13 13:06 CosmicAnalogue465

+1

kurtismullins avatar Sep 20 '13 15:09 kurtismullins

I've made a MongoEngine Model Serializer , powered by Django Rest Framework give it a try https://github.com/umutbozkurt/mongoengine-serializer

umutbozkurt avatar Apr 18 '14 15:04 umutbozkurt