JSON and Pickle - mikahama/mikatools GitHub Wiki

  • Save JSON in UTF-8 in a human readable format. This means pretty printing and not escaping Unicode characters.
  • Dead easy encryption!

JSON

Handling JSON files

json_dump(data, file_path, sort_keys=True, allow_overwrite=True, append=False, password=None, salt="", key=None, key_password=None)

Save an object to a file and closes the file stream. By default, the method sorts the dictionary keys in order. This very useful when taking a look at the data, but it takes time with large amounts of data.

from mikatools import *
json_dump({"key":"value"}, "data.json")

If the password parameter is set, the JSON file will be encrypted with Fernet.

from mikatools import *
json_dump({"key":"value"}, "data.json", password="passw0rd", salt="so_secret")

If the key parameter is set, the JSON file will be encrypted with an RSA public key. Read more about RSA keys

from mikatools import *
json_dump({"key":"value"}, "data.json", key="/path/to/public_key/id_rsa.pub")

json_load(file_path, default_dictionary=None, password=None, salt ="", key=None, key_password=None)

The method will load a UTF-8 encoded JSON file into the memory. If default_dictionary is set, the default_dictionary is returned if the file does not exist, is not available for reading or is not properly formatted.

from mikatools import *
data = json_load("data.json")

If the password parameter is set, the file is decrypted while loading.

from mikatools import *
data = json_load("data.json", password="passw0rd", salt="so_secret")

If the key parameter is set, the file is decrypted while loading with an RSA private key. Read more about RSA keys

from mikatools import *
data = json_load("data.json", key="/path/to/private_key/id_rsa")

Pickle

A faster way of writing the code needed for pickling. Mikatools also supports encrypting and decrypting pickle files.

pickle_dump(data, file_path, password=None, salt="", key=None, key_password=None)

Save an object to a file and closes the file stream.

from mikatools import *
pickle_dump({"key":"value"}, "data.bin")

If the password parameter is set, the pickle file will be encrypted with Fernet.

from mikatools import *
pickle_dump({"key":"value"}, "data.bin", password="passw0rd", salt="so_secret")

If the key parameter is set, the pickle file will be encrypted with an RSA public key. Read more about RSA keys

from mikatools import *
pickle_dump({"key":"value"}, "data.bin", key="/path/to/public_key/id_rsa.pub")

pickle_load(file_path, password=None, salt="", key=None, key_password=None)

The method will load a pickle file into the memory.

from mikatools import *
data = pickle_load("data.bin")

If the password parameter is set, the file is decrypted while loading.

from mikatools import *
data = pickle_load("data.bin", password="passw0rd", salt="so_secret")

If the key parameter is set, the file is decrypted while loading with an RSA private key. Read more about RSA keys

from mikatools import *
data = pickle_load("data.bin", key="/path/to/private_key/id_rsa")