Skip to content

Commit 5eba279

Browse files
committed
feat: add JSON envelope wrapper for -J nested output
Emits {"lsof_version":"...","processes":[...]} around process objects. Handles empty results and suppresses -F marker output in JSON modes during repeat cycles.
1 parent cf0b102 commit 5eba279

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "common.h"
3434
#include "cli.h"
35+
#include "version.h"
3536

3637
/*
3738
* Local definitions
@@ -1390,6 +1391,10 @@ int main(int argc, char *argv[]) {
13901391
(void)qsort((QSORT_P *)slp, (size_t)Nlproc,
13911392
(size_t)sizeof(struct lproc *), comppid);
13921393
}
1394+
if (Fjson) {
1395+
printf("{\"lsof_version\":\"%s\",\"processes\":[", LSOF_VERSION);
1396+
Fjson_first_proc = 1;
1397+
}
13931398
if ((n = Nlproc)) {
13941399

13951400
#if defined(HASNCACHE)
@@ -1546,6 +1551,11 @@ int main(int argc, char *argv[]) {
15461551
}
15471552
Lf = lf;
15481553
}
1554+
if (Fjson) {
1555+
printf("]}\n");
1556+
} else if (Fjsonl && RptTm) {
1557+
putchar('\n');
1558+
}
15491559
/*
15501560
* If a repeat time is set, sleep for the specified time.
15511561
*
@@ -1593,7 +1603,9 @@ int main(int argc, char *argv[]) {
15931603
}
15941604
#endif /* defined(HAS_STRFTIME) */
15951605

1596-
if (Ffield) {
1606+
if (Fjson || Fjsonl) {
1607+
/* JSON modes handle their own cycle separation */
1608+
} else if (Ffield) {
15971609
putchar(LSOF_FID_MARK);
15981610

15991611
#if defined(HAS_STRFTIME)

0 commit comments

Comments
 (0)