You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Ben Atkin <be...@benatkin.com> on 2012/09/04 11:52:05 UTC

ANN: js2json and json2js (for CouchApps)

I made a couple of npm (node.js) modules for editing CouchDB design
documents that involves fewer files than python CouchApp, but like
python CouchApp supports two-way sync. The function source is left
intact when converting between JavaScript source and JSON. The
JavaScript source version just shows it in an embedded function
expression, which makes it syntax highlightable.

http://benatkin.com/2012/09/04/js2json-and-json2js/
https://github.com/benatkin/js2json
https://github.com/benatkin/json2js

Here's a quick example. If I stick this in books.js:

module.exports = {
  "views": {
    "author": {
      "map": function(doc) {
        if (doc.type == 'book')
          emit(doc.author, null);
      }
    },
    "title": {
      "map": function(doc) {
        if (doc.type == 'book')
          emit(doc.title, null);
      }
    }
  }
}

...and run this (after npm install json2js js2json):

var js2json = require('js2json');
var json2js = require('json2js');
var fs = require('fs');

var jsSource = fs.readFileSync('books.js', 'utf8');
var jsonValue = js2json.convert(jsSource);
fs.writeFileSync('books.json', JSON.stringify(jsonValue, null, 2) + "\n");
var jsSourceFromJson = json2js.convert(jsonValue);
fs.writeFileSync('books-from-json.js', jsSourceFromJson + "\n");

...I get the following in books.json:

{
  "views": {
    "author": {
      "map": "function(doc) {\n  if (doc.type == 'book')\n
emit(doc.author, null);\n}"
    },
    "title": {
      "map": "function(doc) {\n  if (doc.type == 'book')\n
emit(doc.title, null);\n}"
    }
  }
}

...and books-from-json.js is exactly the same as books.js.

I explain it more in my blog post (linked at the top of this message).
I need to add a cli tool that syncs, a way to handle attachments, and
a way to handle embedded multiline strings for it to be a
full-featured design doc editor. I have much bigger plans for this,
though: I want to break up CouchApps into a bunch of smaller
documents! The source and tests for these two modules is programmed in
the same style. I think storing functions in JSON makes CouchDB just a
little bit like Smalltalk, with a much more familiar language.

Thanks for reading. Feedback welcome and appreciated.

Ben

Re: ANN: js2json and json2js (for CouchApps)

Posted by Ben Atkin <be...@benatkin.com>.
Jim,

Yes, but I haven't taken a look in a while. Thank you for reminding me
of it! I'm going to use it for my first CouchApp example with json2js.

It saves me a ton of work. :)

Ben

On Tue, Sep 4, 2012 at 6:55 AM, Jim Klo <ji...@sri.com> wrote:
> Hi Ben,
>
> Granted this is much less framework. But have you seen Kanso? http://kan.so
>
> It would be nice if your solution used a command like tool rather than having to script each serialization.
>
> - Jim
>
> On Sep 4, 2012, at 2:52 AM, "Ben Atkin" <be...@benatkin.com> wrote:
>
>> I made a couple of npm (node.js) modules for editing CouchDB design
>> documents that involves fewer files than python CouchApp, but like
>> python CouchApp supports two-way sync. The function source is left
>> intact when converting between JavaScript source and JSON. The
>> JavaScript source version just shows it in an embedded function
>> expression, which makes it syntax highlightable.
>>
>> http://benatkin.com/2012/09/04/js2json-and-json2js/
>> https://github.com/benatkin/js2json
>> https://github.com/benatkin/json2js
>>
>> Here's a quick example. If I stick this in books.js:
>>
>> module.exports = {
>>  "views": {
>>    "author": {
>>      "map": function(doc) {
>>        if (doc.type == 'book')
>>          emit(doc.author, null);
>>      }
>>    },
>>    "title": {
>>      "map": function(doc) {
>>        if (doc.type == 'book')
>>          emit(doc.title, null);
>>      }
>>    }
>>  }
>> }
>>
>> ...and run this (after npm install json2js js2json):
>>
>> var js2json = require('js2json');
>> var json2js = require('json2js');
>> var fs = require('fs');
>>
>> var jsSource = fs.readFileSync('books.js', 'utf8');
>> var jsonValue = js2json.convert(jsSource);
>> fs.writeFileSync('books.json', JSON.stringify(jsonValue, null, 2) + "\n");
>> var jsSourceFromJson = json2js.convert(jsonValue);
>> fs.writeFileSync('books-from-json.js', jsSourceFromJson + "\n");
>>
>> ...I get the following in books.json:
>>
>> {
>>  "views": {
>>    "author": {
>>      "map": "function(doc) {\n  if (doc.type == 'book')\n
>> emit(doc.author, null);\n}"
>>    },
>>    "title": {
>>      "map": "function(doc) {\n  if (doc.type == 'book')\n
>> emit(doc.title, null);\n}"
>>    }
>>  }
>> }
>>
>> ...and books-from-json.js is exactly the same as books.js.
>>
>> I explain it more in my blog post (linked at the top of this message).
>> I need to add a cli tool that syncs, a way to handle attachments, and
>> a way to handle embedded multiline strings for it to be a
>> full-featured design doc editor. I have much bigger plans for this,
>> though: I want to break up CouchApps into a bunch of smaller
>> documents! The source and tests for these two modules is programmed in
>> the same style. I think storing functions in JSON makes CouchDB just a
>> little bit like Smalltalk, with a much more familiar language.
>>
>> Thanks for reading. Feedback welcome and appreciated.
>>
>> Ben

Re: ANN: js2json and json2js (for CouchApps)

Posted by Jim Klo <ji...@sri.com>.
Hi Ben,

Granted this is much less framework. But have you seen Kanso? http://kan.so

It would be nice if your solution used a command like tool rather than having to script each serialization. 

- Jim

On Sep 4, 2012, at 2:52 AM, "Ben Atkin" <be...@benatkin.com> wrote:

> I made a couple of npm (node.js) modules for editing CouchDB design
> documents that involves fewer files than python CouchApp, but like
> python CouchApp supports two-way sync. The function source is left
> intact when converting between JavaScript source and JSON. The
> JavaScript source version just shows it in an embedded function
> expression, which makes it syntax highlightable.
> 
> http://benatkin.com/2012/09/04/js2json-and-json2js/
> https://github.com/benatkin/js2json
> https://github.com/benatkin/json2js
> 
> Here's a quick example. If I stick this in books.js:
> 
> module.exports = {
>  "views": {
>    "author": {
>      "map": function(doc) {
>        if (doc.type == 'book')
>          emit(doc.author, null);
>      }
>    },
>    "title": {
>      "map": function(doc) {
>        if (doc.type == 'book')
>          emit(doc.title, null);
>      }
>    }
>  }
> }
> 
> ...and run this (after npm install json2js js2json):
> 
> var js2json = require('js2json');
> var json2js = require('json2js');
> var fs = require('fs');
> 
> var jsSource = fs.readFileSync('books.js', 'utf8');
> var jsonValue = js2json.convert(jsSource);
> fs.writeFileSync('books.json', JSON.stringify(jsonValue, null, 2) + "\n");
> var jsSourceFromJson = json2js.convert(jsonValue);
> fs.writeFileSync('books-from-json.js', jsSourceFromJson + "\n");
> 
> ...I get the following in books.json:
> 
> {
>  "views": {
>    "author": {
>      "map": "function(doc) {\n  if (doc.type == 'book')\n
> emit(doc.author, null);\n}"
>    },
>    "title": {
>      "map": "function(doc) {\n  if (doc.type == 'book')\n
> emit(doc.title, null);\n}"
>    }
>  }
> }
> 
> ...and books-from-json.js is exactly the same as books.js.
> 
> I explain it more in my blog post (linked at the top of this message).
> I need to add a cli tool that syncs, a way to handle attachments, and
> a way to handle embedded multiline strings for it to be a
> full-featured design doc editor. I have much bigger plans for this,
> though: I want to break up CouchApps into a bunch of smaller
> documents! The source and tests for these two modules is programmed in
> the same style. I think storing functions in JSON makes CouchDB just a
> little bit like Smalltalk, with a much more familiar language.
> 
> Thanks for reading. Feedback welcome and appreciated.
> 
> Ben