hmarr / mongoengine

[Moved to mongoengine/mongoengine]
https://github.com/MongoEngine/mongoengine
MIT License
795 stars 20 forks source link

Serialization / deserialization #210

Open rozza opened 13 years ago

rozza commented 13 years ago

Create a serializer and deserializer for mongodb documents...

Useful for testing / small data exporting..

Refs: #181

rozza commented 13 years ago

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()

iapain commented 13 years ago

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.

wpjunior commented 12 years ago

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

wpjunior commented 12 years ago

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

rozza commented 12 years ago

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

benjiec commented 12 years ago

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

https://gist.github.com/2726279

benwah commented 12 years ago

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

rozza commented 12 years ago

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

Ross

On Thu, Jun 28, 2012 at 5:31 AM, benwah < reply@reply.github.com

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

mahmoudhossam commented 12 years ago

Any progress on this?

honzajavorek commented 11 years ago

+1

rozza commented 11 years ago

0.8 is underway :)

On Tue, Oct 16, 2012 at 2:37 PM, Honza Javorek notifications@github.comwrote:

+1

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

CosmicAnalogue465 commented 11 years ago

+1

kurtismullins commented 10 years ago

+1

umutbozkurt commented 10 years ago

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