JavaCake
JavaCake

Reputation: 4115

How to make an object JSON serializable

I have an object called Exercise which has a lot of variables and i only wish to serialize two variables: id and name for each Exercise.

This is how my vars looks like:

{'exercises': [<Exercise: 16>, <Exercise: 1>, <Exercise: 177>, <Exercise: 163>, <Exercise: 291>, <Exercise: 209>], 'score': 16.0}

How do i turn this into:

{'exercises': [{16,"some name"}, {1,"some name"}, {177,"some name"}, {163,"some name"}, {291,"some name"}, {209,"some name"}], 'score': 16.0}

When i do a json_dumps(vars(exerciseobject)) i obviously get an error TypeError: <Exercise: 16> is not JSON serializable

Source:

# Model
class Exercise(models.Model):
    name = models.CharField(null=False,max_length=255)

    def __unicode__(self):
         return str(self.id)

# Object
class ExercisesObject:
    def __init__(self, aScore, aExercises):
        self.score = aScore
        self.exercises = aExercises # Contains an array of Exercise instances.

# Example:
firstExercise = Exercise.objects.get(pk=1)
secondExercise = Exercise.objects.get(pk=5)
aList = [firstExercise,secondExercise]
obj = ExercisesObject(23,aList)

Upvotes: 1

Views: 1632

Answers (2)

Mauro Baraldi
Mauro Baraldi

Reputation: 6575

The easier way to do that is using serialization native from Django.

from django.core import serializers


class Exercise(models.Model):
    name = models.CharField(null=False,max_length=255)

    def __unicode__(self):
        return str(self.id)

serialized_data = serializers.serialize("json", Exercise.objects.all(), fields=('name'))

Making your custom class return JSON

from json import dumps

class ExercisesObject:
    def __init__(self, aScore, aExercises):
        self.score = aScore
        self.exercises = [{e.id: e.name} for e in aExercises]

    def json(self):
        return dumps({'score': self.score, 'exercises': self.exercises})

Upvotes: 1

falsetru
falsetru

Reputation: 369414

You can provide custom json encoder which is subclass of json.JSONEncoder:

class Exercise:
    def __init__(self, value):
        self.value = value
        self.name = 'soem name'


import json
class CustomEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, Exercise):
            return {'value': o.value, 'name': o.name}
        return super(CustomEncoder, self).default(o)

obj = {
    'exercises': [
        Exercise(16),
        Exercise(1),
        Exercise(177),
        Exercise(163),
        Exercise(291),
        Exercise(209)
    ],
    'score': 16.0
}
print(json.dumps(obj, cls=CustomEncoder, indent=4))

output:

{
    "exercises": [
        {
            "name": "some name",
            "value": 16
        },
        ...
    ],
    "score": 16.0
}

Upvotes: 1

Related Questions