/    Sign up×
Community /Pin to ProfileBookmark

How to serialize complicated object

Hi guys,

I need to write some object to SQLite database. In order to do that I need to serialize this object first.. It has many fields and some fields are objects with some fields by themselves.

I have read how to serialize with JSON but it doesn’t work for me.. ?
This is what I have done:

[CODE]expression = window.getSelection();
expressionRange = expression.getRangeAt(0);
expStartNode = expression.anchorNode;[/CODE]

I get the beginning of the selection I made in expStartNode.
At this point I have expStartNode having an object I want to serialize.

Now JSON claims to do this in order to serialize:

[CODE]p1 = JSON.stringify(expStartNode);[/CODE]

But what I get in p1 is empty brackets…
Anybody has an idea?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meFeb 06.2010 — you can't use elements in JSON: it's just for data.

you might actually be wanting to do this:[CODE]
p1 = JSON.stringify(window.getSelection().[COLOR="Red"]toString[/COLOR]());[/CODE]
Copy linkTweet thisAlerts:
@biancazzurriauthorFeb 06.2010 — No, I want to serialize the data structure in order to save it and open on next occasion.
Copy linkTweet thisAlerts:
@rnd_meFeb 06.2010 — No, I want to serialize the data structure in order to save it and open on next occasion.[/QUOTE]
hard to say what's wrong from what i see.

post the rest of your code and you'll probably get more suggestions.
Copy linkTweet thisAlerts:
@biancazzurriauthorFeb 06.2010 — I don't want to serialize the selection string' i want to serialize the selection - two different things..

window.getSelection()

returns me a selection, descibed by 4 parameters : 2 numbers and two data structures with enormous number of fields. I want to serialize these structures in order to be able to write them to DB.

I have come with some answer about serialization, but the function is stuck, I don't understand why:

[CODE]function serialize(_obj)
{
var str;

switch (typeof _obj)
{
case 'function':
case 'boolean':
case 'number':
return _obj;
break;

case 'string':
return "'" + _obj + "'";
break;

case 'object':
str = "{";
var key, field, count;
count = 0;
for(key in _obj){
field = eval("_obj." + key + ";");
console.log(key +":"+typeof field);
str += key + ":" + serialize(_obj[key]) + ",";
}
str += "}";
return str;
break;

default:
return "unknown";
break;
}
}[/CODE]


this function build me a string lets say the structure is

var i = {a:1, b:2}

serialize(i) would return string "{a:1,b:2}", something that can be saved, and in order to parse it back eval() function can be used.

So now the problem is with this function, the lack of debugging tools is a problem with javascript, I use firebug but it doesn't help much ?
×

Success!

Help @biancazzurri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...