Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a properties file I'm using for GIS software and currently you have a feature like road, then properties under it like so:

{
    "road": {
        "colour": "rgb(0,0,0)"
     },
     "railway": {
        "colour": "rgb(100,100,100)"
     }
}

Now I need to group these into categories, for example Transportation. The first thing I thought of was:

{
    "transportation":
    {
        "road": {
            "colour": "rgb(0,0,0)"
        },
        "railway": {
            "colour": "rgb(100,100,100)"
         }
    }
 }

This makes things awkward when I want to access "road" (for example), because now I need to know it is under "transportation" (mind you I could always write a helper function to eliminate this). Further, "road" could be under multiple groups, perhaps "transportation" and "public works", now I would need to duplicate the properties in both instances.

Would I be wiser to use the following format, or is there a better way to do this?

{
    "road": {
        "colour": "rgb(0,0,0)",
        "group": ["transportation"]
     },
     "railway": {
        "colour": "rgb(100,100,100)",
        "group": ["transportation"]
     }
}

This is my first time using Code Review, I look forward to the feedback =). Cheers.

share|improve this question
    
This has nothing at all to do with php. –  Theodore R. Smith Feb 5 '12 at 0:26
    
It has little to do with PHP. The PHP portion is stating what language I am using to work with the data. People often ask stuff like "In what application are you trying to do this in?" Part of that question is answered by having the php tag. –  user1272 Feb 8 '12 at 20:51

1 Answer 1

up vote 2 down vote accepted

Assuming you're using JavaScript to manipulate the object, you could also split it up into 2 different structures, one for maintaining the hierarchy and another for easy access:

var hierarchy = {
    transportation: {
        road: {
            colour: "rgb(0,0,0)"
        },
        railway: {
            colour: "rgb(100,100,100)"
        }
    }
};
var features = {
    road: hierarchy.transportation.road,
    railway: hierarchy.transportation.railway
};
share|improve this answer
    
I am not using javascript here, but rather straight JSON in text files. Still, this is the way I ended up leaning to. I have my styles in one JSON file, while completely ignoring the hierarchy. Then another file states the nesting hierarchy of the features. –  user1272 Feb 8 '12 at 20:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.