Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3331378
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:34:42+00:00 2026-05-17T23:34:42+00:00

got a question regarding serializing classes that I’ve defined. I have some classes like

  • 0

got a question regarding serializing classes that I’ve defined. I have some classes like

class Foo:
     def __init__(self, x, y):
          self.x = x, self.y = y

     def toDict(self):
          return dict(Foo = dict(x = self.x, y = self.y))

then a class that can contain multiple Foos, such as:

class Bar:
     def __init__(self):
          self.foos = [Foo(a, b), Foo(1, 2)]

While that’s a gross oversimplification of the real structure (it gets much, much more nested than that), that’s a pretty decent overview. The actual data for this is coming from a pseudo-XML file without any actual structure, so I wrote a parser according to the spec given to me, so I now have all the data in a series of classes I’ve defined, with actual structure.

What I’m wanting to do is take this data I have and spit it out into JSON, but I really don’t see a good way (I’m new to Python, this is my first real project with it).

I’ve defined a method in Foo, toDict(), that creates a dictionary out of the information, but that obviously isn’t going to work out like I hope when I try to serialize Bar, with the multiple Foos.

Does anyone have a great way of doing this? This has been a pretty much non-stop learning/codefest the past few days and I’m out of ideas for this, which is the last part of the project. I know about the JSON module for Python, but that doesn’t help me with this particular problem of getting my data into a dictionary (or something similar) that I can pass to json.dump().

Let me know if I can clarify in any way.

Thanks,
T.J.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:34:43+00:00Added an answer on May 17, 2026 at 11:34 pm

    Several comments. First:

    • xml.dom.minidom is a built-in Python DOM implementation. Obviously if the file isn’t actually XML you won’t be able to use it’s builtin parsing functions, but it looks like you’re building a tree-like structure out of the file anyway, in which case you might as well use a minidom.

    OK, henceforth I’ll assume that you have a good reason for writing your own tree-style structure instead of using the builtins.

    • Are you sure the nodes should be classes? That seems like an awful lot of structure when all you really seem to need is a bunch of nested dicts:

      root = {
          "foo1": { "bar1": "spam", "bar2": "ham"},
          "foo2": { "baz1": "spam", "baz2": "ham"},
      }
      

      You get the idea.

    OK, maybe you’re sure that you need the individual nodes to be classes. In that case, they should all inherit from some BaseNode class, right? After all, they are fundamentally similar things.

    • In that case, define a BaseNode.serialise method which effectively prints some information about itself and then calls serialise on all of its children. This is a recursive problem; you might as well use a recursive solution unless your tree is really really really nested.

      The json library allows you to subclass the JSONEncoder to do this.

      >>> import json
      >>> class ComplexEncoder(json.JSONEncoder):
      ...     def default(self, obj):
      ...         if isinstance(obj, complex):
      ...             return [obj.real, obj.imag]
      ...         return json.JSONEncoder.default(self, obj)
      ...
      >>> dumps(2 + 1j, cls=ComplexEncoder)
      '[2.0, 1.0]'
      >>> ComplexEncoder().encode(2 + 1j)
      '[2.0, 1.0]'
      >>> list(ComplexEncoder().iterencode(2 + 1j))
      ['[', '2.0', ', ', '1.0', ']']
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

got a question regarding printing out the 128 first characters from the ascii table.
I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and
This is going probably be a general question regarding checksum used for error detection.
Howdie stackoverflow people! So I've been doing some digging regarding these NoSQL databases, MongoDB,
Basically, i have purchased HTC Hero (Android sdk 1.5) , but i just got
This subject has been touched on before, but there's been some time since the
I would like to know whether its possible to open Open word 2003 (doc)
Ok, I hope this will be my last question in a series of Q's
Possible Duplicates: Benefits of using the conditional ?: (ternary) operator Is the conditional operator
I know this topic is bit old, but i did surf the web and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.