By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

Complex JSON obj, best strategy?

P: n/a
dd
I'm writing something in JS using the latest OO and JSON
and I'm looking for a bit of guidance. I'm going to have this
large object which has many top-level properties and some
top-level functions. That part I have no problem with, I'll
define it in JSON style (my prototype is working just fine).

What I'll also have though, is some arrays at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.

What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.

Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:

for(var i=0;i<mainobj.subobjarray.length;i++)
mainobj.subobjarray[i].somefunc=function(){bla();}

Obviously that func definition wouldn't be a one-liner.

Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:

mainobj = {
id:123,
name:"fred",
type:"whatever",
toplevelfunc:function(){do_stuff();},
toplevel2:function(){do_more_stuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property:"the_end",
final_func:function(){alert("really the end");
};

//add functions into the arrays of sub-obj's here.

Jun 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
dd
On Jun 20, 5:05 pm, dd <dd4...@gmail.comwrote:
],
final_property:"the_end",
final_func:function(){alert("really the end");

};
Yeah I know my closing braces got screwed
up at the end, I think it's google groups
that did that.

Jun 20 '07 #2

P: n/a
On Jun 20, 11:09 am, dd <dd4...@gmail.comwrote:
On Jun 20, 5:05 pm, dd <dd4...@gmail.comwrote:
],
final_property:"the_end",
final_func:function(){alert("really the end");
};

Yeah I know my closing braces got screwed
up at the end, I think it's google groups
that did that.
I think you should break thinks apart into different functions and use
new to construct the objects. Or you can have the functions return an
object. Whichever style you prefer.

function MySubObj(num) {
this.num = num;
this.name = 'hello';
this.foo = 'bar';

this.afunction = function() {
// much easier to define here.
}

// and so on
}

function MyObj() {
this.id = 123;
this.name = 'fred';

this.subObjarray = [new MySubObj(1), new MySubObj(2)];

// and so on.
}

var mainobj = new MyObj();

Jun 21 '07 #3

P: n/a
dd
On Jun 21, 2:40 pm, Jang <jangc...@gmail.comwrote:
I think you should break thinks apart into different
functions and use new to construct the objects.
Thanks for the suggestion. That would work nicely
but my sub-objects will have 30+ properties and I
don't want to pass them all in via the constructor
call. I tried to avoid showing that much detail in
the example. In the sub-sub-object cases where
there's not too much data I'd need to pass in thru
the constructor I will do that though :)

Jun 21 '07 #4

P: n/a
dd wrote:
I'm writing something in JS using the latest OO and JSON
and I'm looking for a bit of guidance. I'm going to have this
large object which has many top-level properties and some
top-level functions. That part I have no problem with, I'll
define it in JSON style (my prototype is working just fine).
JSON is a data transmission/exchange format based upon a subset of
javascript's object literal notation. JSON explicitly forbids the use of
functions as part of the data.
What I'll also have though, is some arrays at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.

What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.

Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:

for(var i=0;i<mainobj.subobjarray.length;i++)
mainobj.subobjarray[i].somefunc=function(){bla();}
In terms of what the code did (particularly the number of function
objects created) that would be no more efficient than defining the
functions directly in the object literal notation. All you have done is
reduce the amount of source code.
Obviously that func definition wouldn't be a one-liner.
that is perhaps a pity as if all the function did was call another
function with no arguments you could write:-

mainobj.subobjarray[i].somefunc = bla;

- and have all these objects share the same reference to a single
function object.
Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:
They are not JSON if they include functions, they are object literals.
mainobj = {
id:123,
name:"fred",
type:"whatever",
toplevelfunc:function(){do_stuff();},
toplevel2:function(){do_more_stuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property:"the_end",
final_func:function(){alert("really the end");
};

//add functions into the arrays of sub-obj's here.


var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
)
],
"the_end",
);

function MainObject(name, type, subobjarray, final_property){
this.name = name;
this.type = type;
this.subobjarray = subobjarray;
this.final_property = final_property;
};
MainObject.prototype.toplevelfunc = function(){do_stuff();};
MainObject.prototype.toplevel2 = function(){do_more_stuff();};
MainObject.prototype.final_func = function(){alert("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.prototype.func_here = function(){is_a_bad_idea();};

function SubSubObject(bla,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.prototype.somefunc = function(){bla();};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.

Jul 2 '07 #5

P: n/a
d d
Richard Cornford wrote:
dd wrote:
They are not JSON if they include functions, they are object literals.
Yeah, I seem to be using the term JSON wrongly. There's
no interchange going on between languages, via XML or
in any kind of server dialog. Really I just mean declaring
my objects/functions using the object notational style.
var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
)
],
"the_end",
);

function MainObject(name, type, subobjarray, final_property){
this.name = name;
this.type = type;
this.subobjarray = subobjarray;
this.final_property = final_property;
};
MainObject.prototype.toplevelfunc = function(){do_stuff();};
MainObject.prototype.toplevel2 = function(){do_more_stuff();};
MainObject.prototype.final_func = function(){alert("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.prototype.func_here = function(){is_a_bad_idea();};

function SubSubObject(bla,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.prototype.somefunc = function(){bla();};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.
One of the major reasons I want to do this, is because
these sub objects have a lot of properties. I tried to
scale it down to an easily read example. The trouble is,
doing that made it seem that certain methods (like using
the new SubObject would be the best method.

As I haven't yet decided on how many methods and how
many data elements will be in the objects, I'll bear
in mind the benefits you outlined above to ensure I get
the best mix of inline definitions and pre-defined
objects.

Thanks !!

~dd
Jul 2 '07 #6

P: n/a
d d wrote:
Richard Cornford wrote:
>dd wrote:
They are not JSON if they include functions, they are object
literals.

Yeah, I seem to be using the term JSON wrongly. There's
no interchange going on between languages, via XML or
in any kind of server dialog. Really I just mean declaring
my objects/functions using the object notational style.
>var mainobj = new MainObject(
123,
"fred",
"whatever",
[
new SubObject(
0,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
),
new SubObject(
1,
"hello",
"bar",
[
new SubSubObject("bla","whateva!"),
new SubSubObject("what?","hi")
]
)
],
"the_end",
);

function MainObject(name, type, subobjarray, final_property){
this.name = name;
this.type = type;
this.subobjarray = subobjarray;
this.final_property = final_property;
};
MainObject.prototype.toplevelfunc = function(){do_stuff();};
MainObject.prototype.toplevel2 = function(){do_more_stuff();};
MainObject.prototype.final_func = function(){alert("really the end");
function SubObject(num, name, foo, otherarray){
var c, len;
this.num = num;
this.name = name;
this.foo = foo;
this.otherarray = otherarray;
}
SubObject.prototype.func_here = function(){is_a_bad_idea();};

function SubSubObject(bla,like ){
this.fbla = bla;
this.flike = like
}
SubSubObject.prototype.somefunc = function(){bla();};

And the - mainobj - structure is still not JSON, as - new - is not
allowed in addition to functions.

Richard.

One of the major reasons I want to do this, is because
these sub objects have a lot of properties. I tried to
scale it down to an easily read example. The trouble is,
doing that made it seem that certain methods (like using
the new SubObject would be the best method.
<snip>

I don't think that really matters, more properties means more values -
more data. That would seem to favour the above as it increases the ratio
of date to mechanism code details, and keeps to two isolated from each
other. The only significant disadvantage is that the data is not
labelled in the context where it appears. Traded against the advantages
of having the mechanism defined once and in one place and inherited as a
side effect of creating the objects.

Richard.

Jul 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.