Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DC3D3DA06 for ; Tue, 4 Sep 2012 15:16:17 +0000 (UTC) Received: (qmail 55516 invoked by uid 500); 4 Sep 2012 15:16:16 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 55486 invoked by uid 500); 4 Sep 2012 15:16:16 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 55477 invoked by uid 99); 4 Sep 2012 15:16:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Sep 2012 15:16:16 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=FSL_RCVD_USER,RCVD_IN_DNSWL_LOW,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [209.85.214.52] (HELO mail-bk0-f52.google.com) (209.85.214.52) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Sep 2012 15:16:10 +0000 Received: by bkcjf3 with SMTP id jf3so2541025bkc.11 for ; Tue, 04 Sep 2012 08:15:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:x-gm-message-state; bh=xM0yiSx9YWD3miRabHaXongUhoX9OP3lXNnqv1hfIPM=; b=ooWTHOBZpckCvDmF74VEr2E5LJvSkTbtLYp9t6iXXlush0p5ggUOruPohXnvp7LJ9j xof/xK+JX/nDJIpst3XY/xsGfQaVQX5RF/R3Osm/XsdKijWTT3MzjdTDpUblAZUm0Nbl YziefSOZk9U3Vh1NqMIaHKNuIKsrILxR5uob0oMUWV7FeqLET/D3ZAtSpAHq90FGn4Tv exO07yH7bhc0tqcFW31tIM1YUzFaO1SFhzZAeYKayO30smHSeyaAn9mVpBYNZaMdvQO0 HppeIXEmtT7+8HWm0tL35QCr/Gt8MWXpd8ZSzgpW/bBtrujK3fyKEgwuFs50/wzUdTYA bOCA== Received: by 10.204.154.211 with SMTP id p19mr8524546bkw.12.1346771749522; Tue, 04 Sep 2012 08:15:49 -0700 (PDT) MIME-Version: 1.0 Received: by 10.204.127.66 with HTTP; Tue, 4 Sep 2012 08:15:29 -0700 (PDT) In-Reply-To: <90B10DC3-D27E-40CE-BE0C-3B692A7C3DBE@sri.com> References: <90B10DC3-D27E-40CE-BE0C-3B692A7C3DBE@sri.com> From: Ben Atkin Date: Tue, 4 Sep 2012 08:15:29 -0700 Message-ID: Subject: Re: ANN: js2json and json2js (for CouchApps) To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 X-Gm-Message-State: ALoCoQkX5G63xuqrJorL/64IjaL7KGyaag7oCXZk0CUEdAChiERY9AnNKJmF4wqs3ax5MsRFMOve 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 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" 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