Python Forum
How to serialize custom class objects in JSON?
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.
Reply
#2
google 'arbitrary class instance in JSON python'
Reply
#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.
Reply
#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")
Reply
#5
Hello!
Look at jasonpickle module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I access objects or widgets from one class in another class? Konstantin23 3 994 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
Question How to move a class to a custom module? python300 4 1,550 Mar-08-2022, 09:19 PM
Last Post: python300
  Custom file class deanhystad 11 4,362 Feb-01-2021, 05:09 PM
Last Post: nilamo
  Class objects Python_User 12 4,415 Aug-27-2020, 08:02 PM
Last Post: Python_User
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,028 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  printing class properties from numpy.ndarrays of objects pjfarley3 2 1,941 Jun-08-2020, 05:30 AM
Last Post: pjfarley3
  Parse JSON multiple objects larkin_L 8 5,689 May-27-2020, 11:18 AM
Last Post: nuffink
  Help with lists and class objects AlluminumFoil 15 5,460 Jan-15-2020, 07:32 PM
Last Post: AlluminumFoil
  How do I write class objects to a file in binary mode? Exsul1 7 5,729 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Do objects get their own copy of the class methods? Charles1 1 2,083 Feb-02-2019, 04:40 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020