cereal /

Filename Size Date modified Message
52 B
46 B
3.6 KB
1.2 KB
4.3 KB
340 B
<!--*-markdown-*-->

# Cereal

Cereal is a simple library for constructing serializers. It leans towards use with Django models, but can be used with pretty much any object. To use it, you define transformations from one object space to another using a very simple class-based DSL. The following example shows how the entire workflow might be laid out:

    from django.db import models
    
    from cereal import *
    
    
    class Book(models.Model):
        isbn = models.CharField(max_length=16, unique=True)
        publisher = models.ForeignKey('Publisher', related_name='books')
        name = models.CharField(max_length=255)
        author_name = models.CharField(max_length=255)
        title = models.CharField(max_length=255)
        created_at = models.DateTimeField()
        preview = models.TextField()
        stores = models.ManyToManyField('BookStore', related_name='books')
    
    
    class BookSerializer(Serializer):
        
        proxies_for('name', 'author_name', 'title', 'created_at', 'preview')
        
        def publisher(book):
            # `PublisherSerializer` may be defined elsewhere. `_without()` will
            # dynamically create a new serializer without the `books`
            # attribute; this is used to prevent an infinite loop.
            return PublisherSerializer._without('books')(book.publisher)
        
        @with_key('isbn_uri')
        def isbn(book):
            return u'urn:isbn:' + book.isbn
        
        def store_count(book):
            return book.stores.count()
        
        def stores(book):
            # Return a list of the first 100 book stores. `StoreSerializer` may
            # be defined elsewhere. We don't want each store to return its list
            # of books, since that might result in an infinite loop.
            return map(StoreSerializer._without('books'), book.stores[:100])
    
    
    # This view could be routed to via '/book/<book_id>/'
    def myview(request, book_id):
        import simplejson
        
        book = Book.objects.get(pk=int(book_id))
        serialized = BookSerializer(book) # Returns a serialized dictionary.
        return HttpResponse(simplejson.dumps(serialized),
            mimetype='application/json')

**DISCLAIMER**: This library may kill your cat. It is nowhere near feature-completeness, but at the moment is still in one of those 'proof of concept'-type alpha stages.


## (Un)license

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.