Skip to content

Commit 0664bf8

Browse files
author
Meulemans
committed
Started on geojson reader
1 parent eefb207 commit 0664bf8

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

src/nl/tue/geometrycore/io/json/GeoJSONReader.java

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
* GeometryCore library
43
* Copyright (C) 2025 Wouter Meulemans ([email protected])
@@ -25,7 +24,10 @@
2524
import nl.tue.geometrycore.util.ClipboardUtil;
2625

2726
/**
28-
* This class provides a reader for the GeoJSON format.
27+
* This class provides a reader for the GeoJSON format. Note that all properties
28+
* of the elements are stored purely as strings. For quoted strings, the quotes
29+
* are removed. Any other values are parsed strictly as is, to be further parsed
30+
* if necessary.
2931
*
3032
* @author Wouter Meulemans ([email protected])
3133
*/
@@ -45,9 +47,9 @@ private GeoJSONReader(BufferedReader source) {
4547
}
4648

4749
/**
48-
* Constructs the reader of a file containing the IPE XML code.
50+
* Constructs the reader of a GeoJson file.
4951
*
50-
* @param file the ipe file to be read
52+
* @param file the GeoJson file to be read
5153
* @return new reader for the provided file
5254
* @throws FileNotFoundException
5355
*/
@@ -56,21 +58,21 @@ public static GeoJSONReader fileReader(File file) throws FileNotFoundException {
5658
}
5759

5860
/**
59-
* Constructs a reader for the provided string containing IPE XML code.
61+
* Constructs a reader for the provided string containing GeoJson.
6062
*
61-
* @param string IPE XML code
62-
* @return new reader for the code
63+
* @param string GeoJson text
64+
* @return new reader for the text
6365
*/
6466
public static GeoJSONReader stringReader(String string) {
6567
return new GeoJSONReader(new BufferedReader(new StringReader(string)));
6668
}
6769

6870
/**
6971
* Constructs a custom reader using some buffered reader that provides the
70-
* IPE XML code line by line.
72+
* GeoJson.
7173
*
72-
* @param reader buffered reader providing the IPE XML code
73-
* @return new reader for the code
74+
* @param reader buffered reader providing the GeoJson text
75+
* @return new reader for the text
7476
*/
7577
public static GeoJSONReader customReader(BufferedReader reader) {
7678
return new GeoJSONReader(reader);
@@ -108,6 +110,15 @@ public void read(List<ReadItem> items) throws IOException {
108110
}
109111
}
110112

113+
/**
114+
* By default, all properties are read. For very large files, this may be
115+
* very inefficient. This methods allows specifying precisely which
116+
* properties are to be read. Set to null to read all properties.
117+
*
118+
* Note that property names are case sensitive.
119+
*
120+
* @param props Properties to be read
121+
*/
111122
public void setPropertiesOfInterest(String... props) {
112123
_propsToKeep = props;
113124
}
@@ -149,7 +160,18 @@ private void readProperties(ReadItem ri) throws IOException {
149160
break;
150161
}
151162
skipToCharacter(':');
152-
StringBuilder value = new StringBuilder();
163+
164+
boolean interest;
165+
if (_propsToKeep == null) {
166+
interest = true;
167+
} else {
168+
interest = false;
169+
for (String p : _propsToKeep) {
170+
interest = interest || p.equals(name);
171+
}
172+
}
173+
174+
StringBuilder value = interest ? new StringBuilder() : null;
153175
boolean instring = false;
154176
int accolades = 0;
155177
int brackets = 0;
@@ -163,7 +185,9 @@ private void readProperties(ReadItem ri) throws IOException {
163185
if (!instring) {
164186
accolades++;
165187
}
166-
value.append(ch);
188+
if (interest) {
189+
value.append(ch);
190+
}
167191
break;
168192
case '}':
169193
if (!instring) {
@@ -174,57 +198,59 @@ private void readProperties(ReadItem ri) throws IOException {
174198
done = true;
175199
break charloop;
176200
} else {
177-
value.append(ch);
201+
if (interest) {
202+
value.append(ch);
203+
}
178204
}
179205
break;
180206
case '[':
181207
if (!instring) {
182208
brackets++;
183209
}
184-
value.append(ch);
210+
if (interest) {
211+
value.append(ch);
212+
}
185213
break;
186214
case ']':
187215
if (!instring) {
188216
brackets--;
189217
}
190-
value.append(ch);
218+
if (interest) {
219+
value.append(ch);
220+
}
191221
break;
192222
case '"':
193223
instring = !instring;
194-
value.append(ch);
224+
if (interest) {
225+
value.append(ch);
226+
}
195227
break;
196228
case ',':
197229
if (!instring && brackets == 0 && accolades == 0) {
198230
// end of the property
199231
break charloop;
200232
} else {
201-
value.append(ch);
233+
if (interest) {
234+
value.append(ch);
235+
}
202236
}
203237
break;
204238
default:
205-
value.append(ch);
239+
if (interest) {
240+
value.append(ch);
241+
}
206242
break;
207243
}
208244
next = _source.read();
209245
}
210246

211-
boolean interest;
212-
if (_propsToKeep == null) {
213-
interest = true;
214-
} else {
215-
interest = false;
216-
for (String p : _propsToKeep) {
217-
interest = interest || p.equals(name);
218-
}
219-
}
220-
221247
if (interest) {
222248
String val = value.toString().trim();
223249
if (val.charAt(0) == '"') {
224250
val = val.substring(1, val.length() - 1);
225251
}
226252
props.put(name, val);
227-
}
253+
}
228254
}
229255
}
230256

0 commit comments

Comments
 (0)