Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 966388b

Browse files
committed
handle multiple versions with same parser
1 parent 95e769d commit 966388b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/java/de/taimos/gpsd4java/backend/LegacyResultParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
/**
3232
* This class is used to parse responses from GPSd<br>
33+
* @deprecated use ResultParser; it handles old fields correctly
3334
*
3435
* @author thoeger
3536
*/
37+
@Deprecated
3638
public class LegacyResultParser extends ResultParser {
3739

3840
@Override

src/main/java/de/taimos/gpsd4java/backend/ResultParser.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,29 @@ protected IGPSObject parsePOLL(final JSONObject json) throws ParseException {
256256
IGPSObject gps;
257257
// for gpsd version > 3.5
258258
final PollObject poll = new PollObject();
259-
poll.setTimestamp(this.parseTimestamp(json, "time"));
259+
if (json.has("time")) {
260+
poll.setTimestamp(this.parseTimestamp(json, "time"));
261+
} else if (json.has("timestamp")) {
262+
poll.setTimestamp(json.optDouble("timestamp", Double.NaN));
263+
}
264+
260265
poll.setActive(json.optInt("active", 0));
261-
poll.setFixes(this.parseObjectArray(json.optJSONArray("tpv"), TPVObject.class));
262-
poll.setSkyviews(this.parseObjectArray(json.optJSONArray("sky"), SKYObject.class));
263-
poll.setGst(this.parseObjectArray(json.optJSONArray("gst"), GSTObject.class));
266+
267+
if (json.has("tpv")) {
268+
poll.setFixes(this.parseObjectArray(json.optJSONArray("tpv"), TPVObject.class));
269+
} else if (json.has("fixes")) {
270+
poll.setFixes(this.parseObjectArray(json.optJSONArray("fixes"), TPVObject.class));
271+
}
272+
273+
if (json.has("sky")) {
274+
poll.setSkyviews(this.parseObjectArray(json.optJSONArray("sky"), SKYObject.class));
275+
} else if (json.has("skyviews")) {
276+
poll.setSkyviews(this.parseObjectArray(json.optJSONArray("skyviews"), SKYObject.class));
277+
}
278+
279+
if (json.has("gst")) {
280+
poll.setGst(this.parseObjectArray(json.optJSONArray("gst"), GSTObject.class));
281+
}
264282
gps = poll;
265283
return gps;
266284
}

0 commit comments

Comments
 (0)