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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:20:37+00:00 2026-06-07T22:20:37+00:00

I have a JSONEncoder and JSONDecoder: class SimpleTargetJSONEncoder(json.JSONEncoder): converts a SimpleTarget to a Dict

  • 0

I have a JSONEncoder and JSONDecoder:

class SimpleTargetJSONEncoder(json.JSONEncoder):
    """
    converts a SimpleTarget to a Dict so it can be JSONified
    """

    def default(self, o):
        if isinstance(o, SimpleTargetItem):
            return {
                'x': o.x(),
                'y': o.y(),
                'status': o.status,
                'imageSet': o.imageSet}


class SimpleTargetJSONDecoder(json.JSONDecoder):
    def __init__(self):
        json.JSONDecoder.__init__(self, object_hook=self.dict_to_object)

    def dict_to_object(self, d):
        t = SimpleTargetItem(imageSet=d['imageSet'])
        t.setX(d['x'])
        t.setY(d['y'])
        return t

The encoder writes out a file like this:

[
    {
        "y": 2514.0, 
        "x": 2399.0, 
        "status": "default", 
        "imageSet": {
            "default": ":/images/blue-circle.png", 
            "active": ":/images/green-circle.png", 
            "removed": ":/images/black-circle.png", 
        }
    }, 
    {
        "y": 2360.0, 
        "x": 2404.0, 
        "status": "default", 
        "imageSet": {
            "default": ":/images/blue-square.png", 
            "active": ":/images/green-square.png", 
            "removed": ":/images/black-square.png", 
        }
    }
]

But when I call the decoder, I get:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/dmd/Documents/inmotion/py/targetlayoutdesigner/designer.py
Traceback (most recent call last):
  File "/Users/dmd/Documents/inmotion/py/targetlayoutdesigner/targetlayoutwindow.py", line 131, in loadLayout
    for t in SimpleTargetJSONDecoder().decode(open(fname).read()):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/Users/dmd/Documents/inmotion/py/targetlayoutdesigner/targetitem.py", line 113, in dict_to_object
    t = SimpleTargetItem(imageSet=d['imageSet'])
KeyError: 'imageSet'

And if I look at d at that point, d is just this:

{u'active': u':/images/green-circle.png',
 u'default': u':/images/blue-circle.png',
 u'removed': u':/images/black-circle.png'}

i.e., the inner dict.

What am I doing wrong?

  • 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-06-07T22:20:39+00:00Added an answer on June 7, 2026 at 10:20 pm

    You’re assuming that you’ll get only the dict you want, instead of all dicts.

    class SimpleTargetJSONDecoder(json.JSONDecoder):
        def __init__(self):
            json.JSONDecoder.__init__(self, object_hook=self.dict_to_object)
    
        def dict_to_object(self, d):
            if 'x' not in d or 'y' not in d:
                return d
            t = SimpleTargetItem(imageSet=d['imageSet'])
            t.setX(d['x'])
            t.setY(d['y'])
            return t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have a WCF service which I would like to product XML and JSON
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
i have encountered a very strange issue: i use json.dump to write a file
I have these models: class Projects(models.Model): projectName =models.CharField(max_length = 100,unique=True,db_index=True) projectManager = EmbeddedModelField('Users') class
i have an element of type Radio in zend form . how can i
I have the following json data in a Javascript variable result : [ Object
Have one Doubt In MVC Architecture we can able to pass data from Controller
I have a php script that outputs a json-encoded object with large numbers (greater
Have an app that can use tts to read text messages. It can also

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.