Ciro García
Ciro García

Reputation: 650

Serialize coordinates to JSON from Python

I have a game with an infinite world, so I render it using chunks. Those chunks are stored in a dict like this:

chunks = {
    (0,0) : {
        'floor' : 'stuff', 
        'items' : {}
    },
    (1,0) : {
        'floor' : 'more stuff', 
        'items' : {}
    }
}

As you can see, the chunk's coordinates are stored as a tuple, wjich is quite handy, but this stops me from serializing the dict to a JSON file.

Is there any way I can keep the key of each chunk as a tuple (x, y) and dumping the dict in to JSON?

Upvotes: 0

Views: 521

Answers (3)

deadshot
deadshot

Reputation: 9071

Using two helper methods create_json() and create_dict().

create_json() method store the dictionary in json file by converting the tuple keys to strings.

create_dict() method create the dictionary from the json file created with create_json() method.

import json

chunks = {
          (0,0) : {
                   'floor' : 'stuff', 
                   'items' : {}
                  },
          (1,0) : {
                   'floor' : 'more stuff', 
                   'items' : {}
                  }
         }

def create_json(data):
    with open('test.json', 'w') as fw:
        res = {str(k): v for k, v in data.items()}
        json.dump(res, fw)

def create_dcit(json_file):
    with open(json_file) as fp:
        data = json.loads(fp.read())
        return {eval(k): v for k, v in data.items()}

create_json(chunks)

y = create_dcit('test.json')

Upvotes: 0

cglacet
cglacet

Reputation: 10952

I feel like pickle is what you need here. You can read about the difference with JSON here. In your case that would give:

Saving

with open('map.pickle', 'wb') as f:
    pickle.dump(chunks, f)

Loading

with open('map.pickle', 'rb') as f:
    chunks = pickle.load(f)

Which gives you the exact same dictionary as you saved (in binary format). I think using JSON only makes sense if you plan on sharing it with another application, in particular with a non-python application:

JSON is interoperable and widely used outside of the Python ecosystem, while pickle is Python-specific;

Upvotes: 0

Sohaib Anwaar
Sohaib Anwaar

Reputation: 1547

Actually json required your keys to be in string. You can convert your keys in string and store it in json and when you need to take your values back than you can revert them back to the original shape. In the code below I tried my best to solve your problem

import json
chunks = {
          (0,0) : {
                   'floor' : 'stuff', 
                   'items' : {}
                  },
          (1,0) : {
                   'floor' : 'more stuff', 
                   'items' : {}
                  }
         }

# To store dict into json
chunks = {str(k): v for k,v in chunks.items()}
stored_in_json = json.dumps(chunks)

# Load json and revert it back to the orignal shape
chunks = {eval(k): v for k,v in json.loads(stored_in_json).items()}

Upvotes: 1

Related Questions