473,378 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Complex JSON obj, best strategy?

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
6 2141
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
5
by: Dominic Myers | last post by:
In the full and frank knowledge that someone will doubtless refer me to google and probably tell me to wipe my own arse I was wondering if someone could shed some light on the following problem...
13
by: trpost | last post by:
I am looking to make a small web app that will return the status of a website from the client browser. I tried this with AJAX and it worked great locally, but did not work for remote users...
4
by: VK | last post by:
Google Trends is an all new service (started May 10) and I have not responsability for proper query or data accuracy. Overall seems pretty close to what could be observed by the post history in...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
5
by: michal | last post by:
hi guys, i thought you might be interested in a nice JSON class which converts ASP datatypes (basic datatypes, dictionaries, recordsets, ...) into JSON so that javascript can easily understand it...
2
by: giloosh | last post by:
whats the best way to pass a json string to the server. if my jsonstring = {a:'1',b:'sds',c:'sdg'} could i send that to the server passing it as 1 variable like so: url =...
2
by: holtmann | last post by:
Hi, I got a question regarding JSON as datasource- I understand that eval on a JSON String creates the appropriate objects in JS. But I would like to use JSON to supply data to already defined...
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.