Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
ANN: js2json and json2js (for CouchApps)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ben Atkin  
View profile  
 More options Sep 4 2012, 5:52 am
From: Ben Atkin <b...@benatkin.com>
Date: Tue, 4 Sep 2012 02:52:05 -0700
Local: Tues, Sep 4 2012 5:52 am
Subject: 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Klo  
View profile  
 More options Sep 4 2012, 9:55 am
From: Jim Klo <jim....@sri.com>
Date: Tue, 4 Sep 2012 13:55:09 +0000
Local: Tues, Sep 4 2012 9:55 am
Subject: Re: ANN: js2json and json2js (for CouchApps)
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" <b...@benatkin.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Atkin  
View profile  
 More options Sep 4 2012, 11:16 am
From: Ben Atkin <b...@benatkin.com>
Date: Tue, 4 Sep 2012 08:15:29 -0700
Local: Tues, Sep 4 2012 11:15 am
Subject: Re: ANN: js2json and json2js (for CouchApps)
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »