Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to serialize custom class objects in JSON?
#1
The Python documentation on JSON reads, "This simple serialization technique can handle lists and dictionaries, but serializing arbitrary class instances in JSON requires a bit of extra effort. The reference for the json module contains an explanation of this." But I can't seem to find the explanation in the JSON reference. I'm very new to programming, so it's probably there; it just went right over my head.

The reason I need to serialize the objects of a class I created is that I'm making a random NPC generator for tabletop RPGs, and each generated NPC is an instance of my NPC class with semi-randomized characteristics. Users will want to save the ones they like.

So how does one serialize a custom class object with JSON?

Thanks in advance.
Quote
#2
google 'arbitrary class instance in JSON python'
Quote
#3
In essence, it just boils down to making a JSON object where the keys are the field names (and so the values are, well, the field values). If you can make a dictionary in that form, you can just use json.dumps to produce a string containing the JSON representation.
Quote
#4
(Sep-22-2019, 06:26 PM)ndc85430 Wrote: In essence, it just boils down to making a JSON object where the keys are the field names (and so the values are, well, the field values). If you can make a dictionary in that form, you can just use json.dumps to produce a string containing the JSON representation.

That's basically what I ended up doing, except I used a list instead of a dictionary:

def append_to_file(npc):
    """Append the attributes of an NPC to save file as a JSON string"""
    # Pull the attributes off an NPC class instance
    attrib_list = [npc.sex, npc.char, npc.principles, npc.law_disposition,
                   npc.trait1_type, npc.trait2_type, npc.trait1, npc.trait2,
                   npc.conf_ct1, npc.conf_ct2, npc.conf_tt, npc.quirk,
                   npc.prof_type, npc.prof, npc.con, npc.ill, npc.looks]
    # Open (or create) the save file in append mode
    with open("saved_npcs.txt", "a") as save_file:
        # Serializing attribute list to save file
        # as a JSON-formatted string
        save_file.write(json.dumps(attrib_list))
        save_file.write("\n")
Quote
#5
Hello!
Look at jasonpickle module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I write class objects to a file in binary mode? Exsul1 7 399 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Do objects get their own copy of the class methods? Charles1 1 309 Feb-02-2019, 04:40 PM
Last Post: ichabod801
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 1,657 Jun-19-2018, 02:44 PM
Last Post: jdrp
  How to check if class instance exists in a list of class instance objects? sonicblind 23 3,433 May-27-2018, 05:44 AM
Last Post: buran
  Class Objects... zowhair 2 831 Mar-10-2018, 08:01 AM
Last Post: wavic
  serialize/deserialize data from/to json bb8 1 804 Mar-09-2018, 08:11 PM
Last Post: Gribouillis
  Troubleshooting simple script and printing class objects Drone4four 11 2,619 Dec-16-2017, 05:12 AM
Last Post: Drone4four
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 1,010 Sep-22-2017, 11:31 AM
Last Post: WeInThis
  Creating a list of class objects hjuyrfc 2 903 Jul-22-2017, 07:41 PM
Last Post: hjuyrfc
  Json Objects in Python ramankoduri 3 1,816 Nov-09-2016, 02:35 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)