Skip to content

Commit 2ebf751

Browse files
committed
1.1.0 -- json unescaping, csv output mode
1 parent 05eb966 commit 2ebf751

File tree

6 files changed

+125
-5
lines changed

6 files changed

+125
-5
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: all clean docs install dist
22

3-
VERSION = 1.0.1
3+
VERSION = 1.1.0
44
CFLAGS = -O3
55
LDFLAGS = -static
66
PREFIX = /usr/local

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ elb-2 i-b910a256
3434
Linux users can install prebuilt binaries from the release tarball:
3535

3636
```
37-
sudo bash -c "cd /usr/local && wget -O - https://github.com/micha/json-table/releases/download/1.0.1/jt-1.0.1.tar.gz | tar xzvf -"
37+
sudo bash -c "cd /usr/local && wget -O - https://github.com/micha/json-table/releases/download/1.1.0/jt-1.1.0.tar.gz | tar xzvf -"
3838
```
3939

4040
Otherwise, to build from source:
4141

4242
```
43-
git checkout 1.0.1 && make && sudo make install
43+
git checkout 1.1.0 && make && sudo make install
4444
```
4545

4646
## Documentation

jt.1

+37
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,43 @@ If the item at the top of the data stack is not an object or if the object has n
115115
.IP
116116
If the \fIKEY\fR property of the object is an array subsequent commands will operate on one of the items in the array, chosen automatically by \fBjt\fR\. The array index will be available to subsequent commands via the index stack\.
117117
.
118+
.SH "JSON UNESCAPING AND CSV OUTPUT"
119+
Strings in JSON data must not contain control characters (e\.g\., \fBtab\fR, \fBnewline\fR, etc\.) These characters \fImust\fR be escaped with a backslash\. Additionally, any character \fImay\fR be escaped with a backslash\. The JSON specification also allows escaping of unicode characters with \fB\eu\fR escape, for example the copyright symbol © can be encoded as \fB\eu00A9\fR, and the G\-clef character 𝄞 as \fB\euD834\euDD1E\fR\.
120+
.
121+
.P
122+
Numbers may be expressed in a number of ways in JSON data, and there is a single \fBNumber\fR type that encompasses both integer and floating point\. Both decimal and exponential notation are valid in JSON\.
123+
.
124+
.SS "Strings"
125+
\fBJt\fR does not unescape string values by default, in case they contain tab or newline characters that would break the tabular output format\. If unescaped values are needed this can be achieved by invoking \fBjt\fR with the \fB\-u\fR option in post processing\. For example:
126+
.
127+
.IP "" 4
128+
.
129+
.nf
130+
131+
$ jt \-u \'i love music \eu266A\'
132+
i love music ♪
133+
.
134+
.fi
135+
.
136+
.IP "" 0
137+
.
138+
.SS "Numbers"
139+
\fBJt\fR does not process numbers in any way \(em they are printed in the output verbatim, as they appear in the JSON input\. If special processing is required the \fBprintf\fR program in coreutils is your friend:
140+
.
141+
.IP "" 4
142+
.
143+
.nf
144+
145+
$ printf %\.0f 2\.99792458e9
146+
2997924580
147+
.
148+
.fi
149+
.
150+
.IP "" 0
151+
.
152+
.SS "CSV Output"
153+
The CSV format uses quoted values, which avoids the problems associated with values that contain tab and newline characters\. The \fB\-c\fR option puts \fBjt\fR into CSV output mode\. In this mode JSON strings are unescaped by default\. The \fBcsvtool\fR program and \fBcsvkit\fR suite of tools facilitate processing of CSV data in the shell\.
154+
.
118155
.SH "EXAMPLES"
119156
We will use the following JSON input for the examples:
120157
.

jt.1.html

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jt.1.ronn

+40
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,46 @@ The following commands are available:
115115
The array index will be available to subsequent commands via the index
116116
stack.
117117

118+
## JSON UNESCAPING AND CSV OUTPUT
119+
120+
Strings in JSON data must not contain control characters (e.g., `tab`,
121+
`newline`, etc.) These characters _must_ be escaped with a backslash.
122+
Additionally, any character _may_ be escaped with a backslash. The JSON
123+
specification also allows escaping of unicode characters with `\u` escape,
124+
for example the copyright symbol © can be encoded as `\u00A9`, and the G-clef
125+
character 𝄞 as `\uD834\uDD1E`.
126+
127+
Numbers may be expressed in a number of ways in JSON data, and there is a
128+
single `Number` type that encompasses both integer and floating point. Both
129+
decimal and exponential notation are valid in JSON.
130+
131+
### Strings
132+
133+
**Jt** does not unescape string values by default, in case they contain
134+
tab or newline characters that would break the tabular output format. If
135+
unescaped values are needed this can be achieved by invoking **jt** with the
136+
`-u` option in post processing. For example:
137+
138+
$ jt -u 'i love music \u266A'
139+
i love music ♪
140+
141+
### Numbers
142+
143+
**Jt** does not process numbers in any way — they are printed in the
144+
output verbatim, as they appear in the JSON input. If special processing is
145+
required the `printf` program in coreutils is your friend:
146+
147+
$ printf %.0f 2.99792458e9
148+
2997924580
149+
150+
### CSV Output
151+
152+
The CSV format uses quoted values, which avoids the problems associated with
153+
values that contain tab and newline characters. The `-c` option puts **jt**
154+
into CSV output mode. In this mode JSON strings are unescaped by default. The
155+
`csvtool` program and `csvkit` suite of tools facilitate processing of CSV
156+
data in the shell.
157+
118158
## EXAMPLES
119159

120160
We will use the following JSON input for the examples:

jt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define _GNU_SOURCE
22
#define JSMN_STRICT
33
#define JSMN_PARENT_LINKS
4-
#define JT_VERSION "1.0.1"
4+
#define JT_VERSION "1.1.0"
55

66
#include <stdarg.h>
77
#include <stdio.h>
@@ -219,7 +219,7 @@ unsigned long utf_tag[4] = { 0x00, 0xc0, 0xe0, 0xf0 };
219219

220220
void encode_u_escaped(char **in, char **out) {
221221
unsigned long p = read_code_point(in);
222-
int len = (p < 0x80) ? 1 : ((p < 0x800) ? 2 : ((p < 0x10000) ? 3 : 4));
222+
int len = (p < 0x80) ? 1 : (p < 0x800) ? 2 : (p < 0x10000) ? 3 : 4;
223223
*out += len;
224224
switch (len) {
225225
case 4: *--(*out) = ((p | 0x80) & 0xbf); p >>= 6;

0 commit comments

Comments
 (0)