Skip to content

Commit 3ce5a33

Browse files
committed
json: option DWG_OPTS_MINIMAL for minJSON (minimized)
no whitespace at run-time. We don't write minimal JSON as with minimal DXFs anymore ($ACADVER, $HANDSEED, and then ENTITIES only). With ideas from GH #886
1 parent aa2566e commit 3ce5a33

File tree

7 files changed

+152
-106
lines changed

7 files changed

+152
-106
lines changed

doc/LibreDWG.texi

+4-2
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ LibreDWG installs some binary programs to read or write DWG files.
13781378
@anchor{dwgread}
13791379

13801380
This reads a DWG file, and optionally converts its content to some
1381-
output formats: JSON, GeoJSON, DXF, DXFB (i.e. Binary DXF), SVG.
1381+
output formats: JSON, Minimal JSON, GeoJSON, DXF, DXFB (i.e. Binary DXF), SVG.
13821382

13831383
@code{dwgread [OPTION]... DWGFILE}
13841384

@@ -1388,7 +1388,7 @@ Options:
13881388
verbosity
13891389

13901390
-O fmt, --format fmt
1391-
fmt: JSON, DXF, DXFB, GeoJSON.
1391+
fmt: JSON, DXF, DXFB, minJSON, GeoJSON.
13921392

13931393
More planned formats: YAML, XML/OGR, GPX, SVG, PS.
13941394

@@ -1400,6 +1400,8 @@ Options:
14001400
--version
14011401
output version information and exit
14021402

1403+
minJSON is minimized JSON, without whitespace.
1404+
14031405
@item @file{dwgwrite}
14041406
@cindex dwgwrite
14051407
@anchor{dwgwrite}

programs/alive.test.in

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# alive.test
33
#
44
# Copyright (C) 2010 Thien-Thi Nguyen
5-
# Copyright (C) 2018-2023 Free Software Foundation, Inc.
5+
# Copyright (C) 2018-2024 Free Software Foundation, Inc.
66
#
77
# This program is free software, licensed under the terms of the GNU
88
# General Public License as published by the Free Software Foundation,
@@ -85,7 +85,11 @@ run ()
8585
;;
8686
dwgread) if [ -z "$DISABLE_JSON" ]; then
8787
if [ -n "$addarg" ]; then
88-
arg="-v2 $addarg -o $bdwg-$out.json $arg"
88+
if [ "$addarg" = "min" ]; then
89+
arg="-v2 -o $bdwg.$out.json $arg"
90+
else
91+
arg="-v2 $addarg -o $bdwg-$out.json $arg"
92+
fi
8993
else
9094
arg="-v2 -o $bdwg.json $arg"
9195
fi
@@ -253,17 +257,20 @@ clean ()
253257
rm "./$b.ps" 2>/dev/null
254258
rm "./$b.bmp" 2>/dev/null
255259
rm "./$b.json" 2>/dev/null
260+
rm "./$b.min.json" 2>/dev/null
256261
rm "./$b-rewrite.dwg" 2>/dev/null
257262
rm "./$b-dwgwrite.dwg" 2>/dev/null
258263
rm "./${b}_new.dwg" 2>/dev/null
259264
done
260265
rm "./entities*.dwg" 2>/dev/null
261266
rm "./entities*.dxf" 2>/dev/null
262267
rm "./entities*.json" 2>/dev/null
268+
rm "./entities*.min.json" 2>/dev/null
263269
rm "./entities.dwg" 2>/dev/null
264270
rm "./entities-3d.dwg" 2>/dev/null
265271
rm "./entities-3d_r11.dwg" 2>/dev/null
266272
rm "./entities-3d_r11.json" 2>/dev/null
273+
rm "./entities-3d_r11.min.json" 2>/dev/null
267274
rm "./ACEB10.dwg" 2>/dev/null
268275
rm "./ACEB10.dxf" 2>/dev/null
269276
rm "./ACEB10_*.dxf" 2>/dev/null
@@ -277,6 +284,9 @@ for prog in $PROGS ; do
277284
;;
278285
*) for dwg in $DATA; do
279286
run "$prog" "$dwg"
287+
if [ "$prog" = "dwgread@EXEEXT@" ]; then
288+
run "$prog" "$dwg" "min"
289+
fi
280290
done
281291
;;
282292
esac

programs/dwgread.1

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.49.3.
2-
.TH DWGREAD "1" "February 2024" "dwgread 0.13" "User Commands"
1+
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.48.5.
2+
.TH DWGREAD "1" "Februar 2024" "dwgread 0.13.1" "User Commands"
33
.SH NAME
4-
dwgread \- manual page for dwgread 0.13
4+
dwgread \- manual page for dwgread 0.13.1
55
.SH SYNOPSIS
66
.B dwgread
77
[\fI\,OPTION\/\fR]... \fI\,DWGFILE\/\fR
@@ -13,7 +13,7 @@ and prints error, success or verbose internal progress to stderr.
1313
verbosity
1414
.TP
1515
\fB\-O\fR fmt, \fB\-\-format\fR fmt
16-
fmt: DXF, DXFB, JSON, GeoJSON
16+
fmt: DXF, DXFB, JSON, minJSON, GeoJSON
1717
.TP
1818
Planned output formats:
1919
YAML, XML/OGR, GPX, SVG, PS

programs/dwgread.c

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*****************************************************************************/
22
/* LibreDWG - free implementation of the DWG file format */
33
/* */
4-
/* Copyright (C) 2009-2023 Free Software Foundation, Inc. */
4+
/* Copyright (C) 2009-2024 Free Software Foundation, Inc. */
55
/* Copyright (C) 2010 Thien-Thi Nguyen */
66
/* */
77
/* This library is free software, licensed under the terms of the GNU */
@@ -78,7 +78,7 @@ help (void)
7878
printf (" -v[0-9], --verbose [0-9] verbosity\n");
7979
# ifndef DISABLE_DXF
8080
# ifndef DISABLE_JSON
81-
printf (" -O fmt, --format fmt fmt: DXF, DXFB, JSON, GeoJSON\n");
81+
printf (" -O fmt, --format fmt fmt: DXF, DXFB, JSON, minJSON, GeoJSON\n");
8282
# else
8383
printf (" -O fmt, --format fmt fmt: DXF, DXFB\n");
8484
# endif
@@ -93,7 +93,7 @@ help (void)
9393
printf (" -v[0-9] verbosity\n");
9494
# ifndef DISABLE_DXF
9595
# ifndef DISABLE_JSON
96-
printf (" -O fmt fmt: DXF, DXFB, JSON, GeoJSON\n");
96+
printf (" -O fmt fmt: DXF, DXFB, JSON, MinJSON, GeoJSON\n");
9797
# else
9898
printf (" -O fmt fmt: DXF, DXFB\n");
9999
# endif
@@ -147,7 +147,7 @@ main (int argc, char *argv[])
147147
case ':': // missing arg
148148
if (optarg && !strcmp (optarg, "v"))
149149
{
150-
opts = 1;
150+
opts |= 1;
151151
has_v = 1;
152152
break;
153153
}
@@ -198,7 +198,13 @@ main (int argc, char *argv[])
198198
else
199199
#endif
200200
#ifndef DISABLE_JSON
201-
if (strstr (outfile, ".json") || strstr (outfile, ".JSON"))
201+
if (strstr (outfile, ".min.json")
202+
|| strstr (outfile, ".MIN.JSON"))
203+
{
204+
fmt = strdup ("json");
205+
opts |= DWG_OPTS_MINIMAL;
206+
}
207+
else if (strstr (outfile, ".json") || strstr (outfile, ".JSON"))
202208
fmt = strdup ("json");
203209
else if (strstr (outfile, ".geojson")
204210
|| strstr (outfile, ".GeoJSON"))
@@ -248,13 +254,13 @@ main (int argc, char *argv[])
248254

249255
if (optind != argc)
250256
{
251-
if (opts > 1)
257+
if ((opts & 0xf) > 1)
252258
fprintf (stderr, "Reading DWG file %s\n", argv[i]);
253259
error = dwg_read_file (argv[i], &dwg);
254260
}
255261
else
256262
{
257-
if (opts > 1)
263+
if ((opts & 0xf) > 1)
258264
fprintf (stderr, "Reading DWG from stdin\n");
259265
error = dwg_read_file ("-", &dwg); // i.e. from stdin
260266
}
@@ -272,34 +278,36 @@ main (int argc, char *argv[])
272278
fprintf (stderr, "\n");
273279
dat.version = dat.from_version = dwg.header.version;
274280
dat.codepage = dwg.header.codepage;
281+
dat.opts = opts;
275282
// TODO --as-rNNNN version? for now not.
276283
// we want the native dump, converters are separate.
277284
#ifndef DISABLE_DXF
278285
# ifndef DISABLE_JSON
279-
if (!strcasecmp (fmt, "json"))
286+
if (!strcasecmp (fmt, "json") || !strcasecmp (fmt, "minjson"))
280287
{
281-
if (opts > 1 && outfile)
282-
fprintf (stderr, "Writing JSON file %s\n", outfile);
288+
if ((opts & 0xf) > 1 && outfile)
289+
fprintf (stderr, "Writing %sJSON file %s\n",
290+
opts & DWG_OPTS_MINIMAL ? "minimal " : "", outfile);
283291
error = dwg_write_json (&dat, &dwg);
284292
}
285293
else
286294
# endif
287295
if (!strcasecmp (fmt, "dxfb"))
288296
{
289-
if (opts > 1 && outfile)
297+
if ((opts & 0xf) > 1 && outfile)
290298
fprintf (stderr, "Writing Binary DXF file %s\n", outfile);
291299
error = dwg_write_dxfb (&dat, &dwg);
292300
}
293301
else if (!strcasecmp (fmt, "dxf"))
294302
{
295-
if (opts > 1 && outfile)
303+
if ((opts & 0xf) > 1 && outfile)
296304
fprintf (stderr, "Writing Binary DXF file %s\n", outfile);
297305
error = dwg_write_dxf (&dat, &dwg);
298306
}
299307
# ifndef DISABLE_JSON
300308
else if (!strcasecmp (fmt, "geojson"))
301309
{
302-
if (opts > 1 && outfile)
310+
if ((opts & 0xf) > 1 && outfile)
303311
fprintf (stderr, "Writing GeoJSON file %s\n", outfile);
304312
error = dwg_write_geojson (&dat, &dwg);
305313
}
@@ -341,15 +349,15 @@ main (int argc, char *argv[])
341349
if (error >= DWG_ERR_CRITICAL)
342350
{
343351
fprintf (stderr, "ERROR 0x%x\n", error);
344-
if (error && opts > 2)
352+
if (error && (opts & 0xf) > 2)
345353
dwg_errstrings (error);
346354
}
347355
else
348356
{
349-
if (opts > 1)
357+
if ((opts & 0xf) > 1)
350358
{
351359
fprintf (stderr, "SUCCESS 0x%x\n", error);
352-
if (error && opts > 2)
360+
if (error && (opts & 0xf) > 2)
353361
dwg_errstrings (error);
354362
}
355363
else

programs/json.test.in

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ for dwg in $DATA $TODO; do
116116
i=$((i+1))
117117
fi
118118

119+
minjson="$(basename "$dwg" .dwg).min.json"
120+
echo "./dwgread${EXEEXT} -Ominjson -o$minjson $dwg && jq . $minjson"
121+
if $TESTPROG "./dwgread${EXEEXT}" -Ominjson -o"$minjson" "$dwg" 2>/dev/null && \
122+
@JSON_SED_NAN@ "$minjson" && \
123+
$JQ . "$minjson" >>"$json.log" 2>&1
124+
then
125+
rm "./$json.log" "./$minjson"
126+
else
127+
cat "$json.log"
128+
i=$((i+1))
129+
fi
130+
119131
geojson="$(basename "$dwg" .dwg).geojson"
120132
echo "./dwgread${EXEEXT}" -o"$geojson" "$dwg"
121133
if $TESTPROG "./dwgread${EXEEXT}" -Ogeojson -o"$geojson" "$dwg" 2>/dev/null && \

src/dwg.spec

+2-2
Original file line numberDiff line numberDiff line change
@@ -3161,7 +3161,7 @@ DWG_OBJECT (DICTIONARY)
31613161
{
31623162
FIRSTPREFIX
31633163
VALUE_T (_obj->texts[rcount1]);
3164-
fprintf (dat->fh, ": ");
3164+
fprintf (dat->fh, ":%s", JSON_SPC);
31653165
VALUE_HANDLE (_obj->itemhandles[rcount1], itemhandles, 2, 350);
31663166
}
31673167
}
@@ -3224,7 +3224,7 @@ DWG_OBJECT (DICTIONARYWDFLT)
32243224
{
32253225
FIRSTPREFIX
32263226
VALUE_T (_obj->texts[rcount1]);
3227-
fprintf (dat->fh, ": ");
3227+
fprintf (dat->fh, ":%s", JSON_SPC);
32283228
VALUE_HANDLE (_obj->itemhandles[rcount1], itemhandles, 2, 350);
32293229
}
32303230
}

0 commit comments

Comments
 (0)