JSON indentation in Scaffold

4 views
Skip to first unread message

Rintze Zelle

unread,
Apr 19, 2010, 3:06:43 PM4/19/10
to zotero-dev
As already noted on the Zotero forums, the modified version of
Scaffold for Zotero 2.0 (http://bitbucket.org/rmzelle/scaffold/)
doesn't indent the JSON metadata when writing the translator to file.
I traced the issue to the Zotero.Translators.save function, where
nsIJSON is used to encode the metadata (https://www.zotero.org/trac/
browser/extension/trunk/chrome/content/zotero/xpcom/translate.js?
rev=5895#L181). Apparently nsIJSON doesn't indent JSON: one-line JSON
strings can also be found in translators pushed to Zotero clients via
auto-update (you can check this for the recently updated NCBI
PubMed.js translator: the copy in the SVN repository has indented
JSON, while the copy in my local Zotero folder has a one-line JSON
string).

Firefox 3.5.4 introduced JSON.stringify(), which has indentation
support (via the 'space' parameter, see https://developer.mozilla.org/en/Using_native_JSON).
But using this function is problematic since Zotero 2.0.2 is
compatible with Firefox 3.0, right?

What would be the best thing to do here? Maybe we could add an
optional boolean parameter to the Zotero.Translators.save function,
'embeddedMetadata'? Then Scaffold could use JSON.stringify() to
convert the 'metadata' JavaScript object to an indented JSON string
(requiring Firefox 3.5.4 for Scaffold 2.0 obviously wouldn't be a
problem), prepend the indented string to the 'code' string, and
deliver the 'metadata' JavaScript object and 'code' string (with
embedded metadata JSON string) to Zotero.Translators.save, e.g. via
Zotero.Translators.save(metadata, code, "true"). With a value of
"true", Zotero.Translators.save should then skip the internal JSON
encoding of the metadata object with nsIJSON.

Rintze

--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To post to this group, send email to zoter...@googlegroups.com.
To unsubscribe from this group, send email to zotero-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/zotero-dev?hl=en.

Dan Stillman

unread,
Apr 19, 2010, 9:17:23 PM4/19/10
to zoter...@googlegroups.com
On 4/19/10 3:06 PM, Rintze Zelle wrote:
> What would be the best thing to do here? Maybe we could add an
> optional boolean parameter to the Zotero.Translators.save function,
> 'embeddedMetadata'? Then Scaffold could use JSON.stringify() to
> convert the 'metadata' JavaScript object to an indented JSON string
> (requiring Firefox 3.5.4 for Scaffold 2.0 obviously wouldn't be a
> problem), prepend the indented string to the 'code' string, and
> deliver the 'metadata' JavaScript object and 'code' string (with
> embedded metadata JSON string) to Zotero.Translators.save, e.g. via
> Zotero.Translators.save(metadata, code, "true"). With a value of
> "true", Zotero.Translators.save should then skip the internal JSON
> encoding of the metadata object with nsIJSON.

We can just use stringify() in Zotero.Translators.save() if it's
available (i.e., if the function exists), and make Scaffold 2.0 require
>=3.5 to help ensure that contributed translators use indented JSON.

Rintze Zelle

unread,
Apr 20, 2010, 1:22:55 AM4/20/10
to zotero-dev
On Apr 20, 3:17 am, Dan Stillman <dstill...@zotero.org> wrote:
> We can just use stringify() in Zotero.Translators.save() if it's
> available (i.e., if the function exists), and make Scaffold 2.0 require
>  >=3.5 to help ensure that contributed translators use indented JSON.

That makes more sense. I'll submit a Zotero patch for this once I get
it working.

Rintze

Rintze Zelle

unread,
Apr 20, 2010, 4:29:31 PM4/20/10
to zotero-dev
The following patch works for me (tested in Firefox 3.0.19, where it
gives one-line JSON lines, and Firefox 3.5.9, where it gives indented
JSON):

http://groups.google.com/group/zotero-dev/web/IndentJSON.patch

Rintze
Reply all
Reply to author
Forward
0 new messages