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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:29:39+00:00 2026-05-19T09:29:39+00:00

In the following Android sample, I have a set of JSON objects(referred to as

  • 0

In the following Android sample, I have a set of JSON objects(referred to as templates) and I’m doing a sort of inheritance processing on them. If a JSON object has a key “extends”, the corresponding value will refer to another JSON object. I just copy the mappings in the referred JSON object to the current one. Each property (referred to as component in the code) will have sub properties, so each sub property is copied individually.
All the JSON objects are stored in a HashMap.
EDIT: Posting sample code which demonstrates the issue.

package com.trial;

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;

public class Trial extends Activity {

    HashMap<String, JSONObject> templates = new HashMap<String, JSONObject>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            templates.put("entity", new JSONObject("{\"Health\":{\"health\":50,\"maxHealth\":100}}"));
            templates.put("unit", new JSONObject("{\"Health\":{\"health\":70},\"extends\":\"entity\"}"));
            Iterator<String> templatesIterator = templates.keySet().iterator();
            while (templatesIterator.hasNext()) {
                String templateName = templatesIterator.next();
                JSONObject template = templates.get(templateName); 
                if (template.has("extends"))
                    templates.put(templateName, processInheritance(template));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private JSONObject processInheritance(JSONObject template) throws JSONException {
        if (template.has("extends")) {
            //If current template extends from another template
            String parentTemplateName = template.getString("extends");
            template.remove("extends");
            JSONObject parentTemplate = processInheritance(templates.get(parentTemplateName));
            //Create a clone of the parent to which child properties/components can be added
            JSONObject processedTemplate = new JSONObject(parentTemplate.toString());
            Iterator<String> it = template.keys();
            while (it.hasNext()) {
                String componentName = it.next();
                if (processedTemplate.has(componentName)) {
                    //Component is already present. Loop through Component properties and make
                    //specific replacements
                    JSONObject componentData = template.getJSONObject(componentName);
                    Iterator<String> it2 = componentData.keys();
                    JSONObject processedComponentData = processedTemplate.getJSONObject(componentName); 
                    while (it2.hasNext()) {
                        String propertyName = it2.next();
                        processedComponentData.put(propertyName, componentData.get(propertyName));
                    }
                    processedTemplate.put(componentName, processedComponentData);
                } else {
                    //Component is not already present. Simply copy
                    processedTemplate.put(componentName, template.get(componentName));
                }
            }
            return processedTemplate;
        } else {
            return template;
        }
    }
}

On debugging I noticed that the code executes fine until after the while loop. Soon after the while loop, it jumps across the if/else and returns “template” instead of “processedTemplate”. Even the resulting object indicates that “template” is being returned. I’ve tried clearing the binaries, recreating the emulator, and executing it on the actual device. The weird behavior persists. Could you someone tell me why this happens?

EDIT: The correct object IS being returned. My earlier comment that “template” was being returned was because of a bug where I was not saving the returned process object back into the HashMap.
I was simply doing

if (template.has("extends"))
    processInheritance(template));

However the control still seems to jump across. An problem with Eclipse, I suppose.

  • 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-19T09:29:40+00:00Added an answer on May 19, 2026 at 9:29 am

    Changing the code as below seems to have fixed the issue, but the problem with the original code snippet still remains.

    private JSONObject processInheritance(JSONObject template) throws AssetLoadingException {
            try {
                if (!template.has("extends")) {
                    return template;
                } else {
                    //Everything else
                }
            } catch (JSONException ex) {
                throw new AssetLoadingException(ex);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to execute the following query in Android SQLite UPDATE player SET rank=rank+1
I have the following drawable set as a background: <?xml version=1.0 encoding=utf-8?> <layer-list xmlns:android=http://schemas.android.com/apk/res/android>
I have the following scenario: android up, compatible starting with android 1.6 and up.
I have the following code for Android which works fine to play a sound
I have created following thing in android using android compatibility support package Basically i
I have the following TextView defined: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtCredits" android:autoLink="web" android:id="@+id/infoTxtCredits" android:layout_centerInParent="true" android:linksClickable="true"/>
Hi. I have create a background service application following this tutorial http://androidsourcecode.blogspot.com/2010/10/basic-android-background-service.html . But
I get the following errors when I try to compile any Android RenderScript sample
I have a doubt in android ImageView . I'm making a sample application to
I am following the steps on http://developer.android.com/sdk/ndk/overview.html to build the hello-jni sample, however when

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.