views:

229

answers:

1

My goal is to serialize JavaScript native datatypes for storage into an SQLite database. I'm doing this within a Firefox extension, and so I have access to the Mozilla platform XPCOM api. Initially I was thinking that the easiest thing to do would be to just store JSON strings.

However, if there is a way to serialize native datatypes within the Mozilla platform, I'd like to know where I can find it. And if this is possible, what storage space savings can I expect for the extra work (if any) and would there be a performance cost vs JSON?

Of course, the gains of storing data in a binary format may not outweigh the ease of simply using JSON. Any suggestions?

+1  A: 

Firefox 3.5+ has native JSON encoding/decoding, which is faster than Javascript-engine decoding of JSON, according to Wikipedia.

Given the implication of rapid (de)serialization in FF3.5+, I'd say that you may as well stick with JSON strings. That way will also be virtually cross-platform compatible if you ever need to inspect the DB from another language/platform.

As far as string overhead goes, if you strip out unnecessary whitespace, you're looking at an overhead of a few braces, a few quotation marks, a few commas/colons, and the cost of storing a number as a string instead of a native format. Unless that proves to be a bottleneck in your application, the added simplicity of using simple JSON strings seems worth it.

Mark Rushakoff
Yes, JSON seems to be the natural choice. It could be a good idea to "encode" JSON data before sending to SQL, withsome compression or encoding scheme of sorts. At the cost of a bit of CPU you'll save time and space on the wire and in the database, and maybe more importantly (if the volume of data is generally small anyway) this will also introduce a de facto "encryption" of the data, would sensitive info (name/passowrds/personal details...) be eventually included in the javascript objects. (No NSA grade cipher, but a decent enough deterent against basic hacks).
mjv
If DB space is a major concern, compression is certainly a good idea. Mozilla does have zlib available, which means you definitely could compress strings before storage. Of course, that has the expense of less cross-compatibility, which is admittedly probably not a large concern.
Mark Rushakoff