Permalink
Browse files

jsonquery workaround in place, multiple queries, before, after script…

…s supported
  • Loading branch information...
1 parent e5e73dd commit 53f1c49b3205ff817fcbf15476fab5777f0fa81a @micha committed Jun 4, 2009
Showing with 13 additions and 12 deletions.
  1. +6 −1 README.markdown
  2. +7 −11 jsawk
View
@@ -70,7 +70,9 @@ Usage
is a spidermonkey issue.) Default delimiter is a single period.
-q <query>
- Filter JSON through the specified JSONQuery query.
+ Filter JSON through the specified JSONQuery query. If multiple
+ '-q' options are specified then each query will be performed in
+ turn, in the order in which they appeared on the command line.
-f <file>
Load and run the specified JavaScript file prior to processing
@@ -82,6 +84,9 @@ Usage
processing JSON input. The `this` object is set to the whole JSON
array or object. This is used to preprocess (-b) or postprocess
(-a) the JSON array before or after the main script is applied.
+ This option can be specified multiple times to define multiple
+ before/after scripts, which will be applied in the order they
+ appeared on the command line.
SCRIPT
------
View
18 jsawk
@@ -184,15 +184,9 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2
for(var i=0,l=array.length; i<l; ++i){
var value = array[i];
if(callback(value, i, array)){
- if((typeof value == 'object') && value) {
- // with objects we prevent duplicates with a marker property
- if(!value.__included){
- value.__included = true;
- outArr.push(value);
- }
- }else if(!primitives[value + typeof value]){
+ if(!primitives[JSON.stringify(value)]){
// with primitives we prevent duplicates by putting it in a map
- primitives[value + typeof value] = true;
+ primitives[JSON.stringify(value)] = true;
outArr.push(value);
}
}
@@ -909,6 +903,7 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
args.unshift("ERR:");
doJsonPrint.apply(window, args);
};
+ window.alert = p;
window.doJson = function(input) {
try {
return eval("("+input+")");
@@ -959,12 +954,12 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
quit(1);
};
- var pat = "[*]";
var fun = "";
var noprint = false;
var end = ".";
var befores = [];
var afters = [];
+ var queries = [];
var i,j,k,l,m,n;
var arg;
@@ -983,7 +978,7 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
break;
case "-q":
if (argv.length < 1) usage();
- pat = argv.shift();
+ queries.push(argv.shift());
break;
case "-f":
if (argv.length < 1) usage();
@@ -1030,7 +1025,8 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
var f = makeFilter(fun);
- IS = Q(pat, IS);
+ for (i in queries)
+ IS = Q(queries[i], IS);
$_ = -1;
$$ = IS;

0 comments on commit 53f1c49

Please sign in to comment.