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 6595641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:57:58+00:00 2026-05-25T17:57:58+00:00

I’m writing in python some json encoders for a dictionary derived class, the class

  • 0

I’m writing in python some json encoders for a dictionary derived class, the class EPPI_dictDB, the object of this class may be store as value some instances of the class parser.

BUT when I would encode something goes wrong and the instance of the class EPPI_dictDB passed to the specific encoder function becomes an instance of the class parser!

Let me explain better with an example.
If I execute this code

import json
import raw.data_input as data_input
p = data_input.parser()
class data():
    def __str__(self):
        return self.name

data1 = data()
data1.name = "data1"
data1.proteins = ['acc1','acc2']
data1.peptides = [('acc1',['SEQTWO']),
                      ('acc2',['SEQONE'])]
p.parse(data1)
s = EPPI_dbopen('tmp.prj', flag='n')
s["parser"] = p

print s.__class__
print isinstance(s, EPPI_DictDB)
print isinstance(s, data_input.parser)

I obtain these results:

<class '__main__.EPPI_DictDB'>
True
False

after I call the encoder:

print json.dumps(obj=s, cls=projectEncoder)

Inside the code of the encode I put some ‘prints’ to determine the nature of the passed object:

class projectEncoder(json.JSONEncoder):
    def default(self, obj):

        print obj.__class__
        print isinstance(obj, EPPI_DictDB)
        print isinstance(obj, data_input.parser)

        if isinstance(obj, EPPI_DictDB):
            result = obj.__dict__
            if "parser" in result.keys():
                pars = result["parser"]
                result["parser"] = parserEncoder().encode(pars)
            if "selected" in results.keys():
                sel = result["selected"]
                result["selected"] = parserEncoder().encode(sel)
            return result
        return json.JSONEncoder.default(self, obj)

The results of inner prints are:

<class 'raw.data_input.parser'>
False
True

Could anyone explain me what happen?

  • 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-25T17:57:58+00:00Added an answer on May 25, 2026 at 5:57 pm

    Guessing a bit:

    JSON encoders already know how to handle dicts, so it treats your dict subclass the same way as a dict. However, when it reaches s[“parser”], it doesn’t know what to do with the object, so it calls your default method on it. I.e. from your code, it’s seeing p, not s.

    This seems to be what the docs say.

    EDIT

    Looking at what you’re actually doing, I think there should be an easier way to achieve this:

    def default(self, obj):
        if isinstance(obj, data_input.parser):
            return parserEncoder().encode(obj)
        return super(projectEncoder, self).default(obj)
    

    If the "selected" field is another type, then just change the second line to:

    if isinstance(obj, (data_input.parser, type_of_selected)):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I am writing an app with both english and french support. The app requests

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.