Skip to content

Why deserialize returns dict instead of actual Object ?  #5

Description

@AnumSheraz

Testing the example code as it is, I was expecting the deserializer would return me the actual object, instead of dict.

import datetime
from strainer import (serializer, field, child,
                           formatters, validators,
                           ValidationException)

artist_serializer = serializer(
  field('name', validators=[validators.required()])
)

album_schema = serializer(
  field('title', validators=[validators.required()]),
  field('release_date',
        validators=[validators.required(), validators.datetime()],
        formatters=[formatters.format_datetime()]),
  child('artist', serializer=artist_serializer, validators=[validators.required()])
)


class Artist(object):
    def __init__(self, name):
        self.name = name


class Album(object):
    def __init__(self, title, release_date, artist):
        self.title = title
        self.release_date = release_date
        self.artist = artist


bowie = Artist(name='David Bowie')
album = Album(
    artist=bowie,
    title='Hunky Dory',
    release_date=datetime.datetime(1971, 12, 17)
)

print(album.artist.name)
jsonString = album_schema.serialize(album)
print(jsonString)

deserObj = album_schema.deserialize(jsonString)
print(deserObj)
print(type(deserObj)) .   # <--- this returns dict object
print(deserObj.artist.name)    

the last line blows off sayingAttributeError: 'dict' object has no attribute 'artist' which is as expected !
What do I have to do to get the actual Album object from json String ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions