-1

So I have two user defined python classes shown below

class cell(object):
def __init__(self,id,x_cor,y_cor,width = cell_size,height = cell_size ,color = lightblue ):
    self.id = id
    self.x_cor = x_cor
    self.y_cor = y_cor
    self.width = width
    self.height = height
    self.color = color

class edge(object):
def __init__(self,pos, x_cor,y_cor,state,color = lightgreen):
    global border_size
    global cell_size
    self.pos = pos
    self.x_cor = x_cor
    self.y_cor = y_cor
    self.state = state
    self.color = color
    if self.state == "H":
        self.width = cell_size+(border_size)*2
        self.height = border_size
        self.x_lower_bound = self.x_cor + border_size
        self.x_upper_bound = self.x_cor +border_size+ cell_size
        self.y_lower_bound = self.y_cor
        self.y_upper_bound = self.y_cor + border_size
    elif self.state == "V":
        self.width = border_size
        self.height = cell_size+(border_size*2)
        self.x_lower_bound = self.x_cor
        self.x_upper_bound = self.x_cor + border_size
        self.y_lower_bound = self.y_cor + border_size
        self.y_upper_bound = self.y_cor + border_size + cell_size

Now I have a 2-D list called cells It is defined and looks like this Click me.

It is a bit messy but you can see that it is a 16 by 16 matrix storing an object od cell as defined above

No i want to store this 2-D array in json file this is what i am doing

json_data = []
json_data.append(cells)
with open("default.json" , "w") as f:
    json.dump(json_data,f,indent = 2)

This is the error on terminal I am getting Click on the link to view

2
  • 1
    Does this answer your question? Serializing class instance to JSON Commented Mar 20, 2020 at 8:15
  • yes this works too and pickle worked too thank you! Commented Apr 24, 2020 at 12:28

1 Answer 1

0

Use pickle.

It will not be human readable and not in json format. But this module is intended to store Python objects and load it back again in future.

See more this for comparison with json module.

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.