1

I need to convert a complex python object to JSON, by complex I mean an object that contains int variables, string variables, and 2 lists of custom objects.

My Python object's constructor is:

 def __init__(self, skills="",vid=""):
    self.Skills = list([])
    for skillID in skills.split("-"):
        if not skillID == "":
            tmpSkill = Skill()
            tmpSkillObj = DBCommands.getSkill(skillID)
            tmpSkill.ID = tmpSkillObj[0][0] #tmpSkillObj[0][0]
            tmpSkill.Name = tmpSkillObj[0][1]
            tmpSkill.isMain = True
            tmpSkill.CurrentlyTesting = False
            tmpSkill.isSub = False
            tmpSkill.Level = 0
            tmpSkill.Tested = False
            tmpSkill.Score = 0
            tmpSkill.Confidence = 0
            tmpSkill.BestScore = 0
            tmpSkill.ParentID = 0
            self.Skills.append(tmpSkill)
            self.AskedQuestions.append(tmpSkill)
    self.Skills = list(self.Skills)
    if not skills  == "":
        self.Skills[0].CurrentlyTesting = True #Start testing the first skill
    if not vid  == "":
        self.VacancyID = int(vid)
    self.PlayerID = 0
    self.Score = float(0)
    self.AskedQuestions = list([])
    self.MaxLevel = 0
    self.AssessmentIsFinished = False

I need a mechanism to encode the object and decode it.

5
  • 3
    Does it have to be json? Why not pickle it instead? Commented Jul 22, 2016 at 19:17
  • The scenario is: The server should convert the object to String(JSON), then encrypt this string and send it as a Token. When the server receive the token, It should be able to do the opposite work, decrypte the token, parse the string(JSON) to a new object. Commented Jul 22, 2016 at 19:33
  • 1
    that should work just fine with pickle.dumps(). Your object will be serialized and returned as a string which you can encrypt and transmit using whatever means you like Commented Jul 22, 2016 at 19:37
  • 1
    Especially if you intend to rebuild an identical object from this file you're sending securely, I would recommend pickle for its ease of re-building the object instance. With a .json, you'd have to create a special unpacking script that creates a new instance (basically an alternate __init__ that reads json instead of normal inputs) Commented Jul 22, 2016 at 19:40
  • @Rawing and Aaron thank you guys, problem solved. I just used the pickle class and then encode it with base64 then decode it and load it into object. Commented Jul 22, 2016 at 21:07

1 Answer 1

1

Encode:

import base64
import pickle
token = base64.b64encode(pickle.dumps(token,-1))

Decode:

import pickle
import base64
Obj = pickle.loads(base64.b64decode(token))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.