Complex JSON obj, best strategy?

D

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.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.
 
D

dd

],
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.
 
J

Jang

],
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();
 
D

dd

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 :)
 
R

Richard Cornford

dd said:
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.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.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.
 
D

d d

Richard said:
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
 
R

Richard Cornford

d said:
Richard said:
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.
 
R

Richard Cornford

-Lost wrote:
Mr. Cornford, does this qualify as strictly JSON?:

var constants = {
dom: !!(document.getElementById && document.createElement)
};
No.

In the original case where I saw this code, the developer's
suggested usage was:

var constants = {
dom: function() {
return !!(document.getElementById && document.createElement);
}()
};

I didn't quite understand the need for a function expression,
but it may be due to my lack of knowledge.
<snip>

The function expression is utterly pointless and useless in this
context.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top