diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-04-27 12:52:33 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-04-27 12:52:33 +1200 |
commit | 3083db3929e27933d3ab5eab9fa0c1e221e94eec (patch) | |
tree | 15db7705ecb90dabafb8719c9b93861982a5d91b /src/org/traccar/model/Position.java | |
parent | 27055e0bf5f544552c227da10badff34a71cfb70 (diff) | |
download | trackermap-server-3083db3929e27933d3ab5eab9fa0c1e221e94eec.tar.gz trackermap-server-3083db3929e27933d3ab5eab9fa0c1e221e94eec.tar.bz2 trackermap-server-3083db3929e27933d3ab5eab9fa0c1e221e94eec.zip |
Implement JSON conversion
Diffstat (limited to 'src/org/traccar/model/Position.java')
-rw-r--r-- | src/org/traccar/model/Position.java | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 14f758b4a..7f042c409 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -15,9 +15,15 @@ */ package org.traccar.model; +import java.text.ParseException; import java.util.Date; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import org.traccar.database.JsonConvertable; +import static org.traccar.database.JsonConvertable.dateFormat; -public class Position { +public class Position implements JsonConvertable { private long id; public long getId() { return id; } @@ -76,4 +82,40 @@ public class Position { public String getExtendedInfo() { return extendedInfo; } public void setExtendedInfo(String extendedInfo) { this.extendedInfo = extendedInfo; } + @Override + public JsonObject toJson() { + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add("id", id); + json.add("deviceId", deviceId); + json.add("serverTime", dateFormat.format(serverTime)); + json.add("deviceTime", dateFormat.format(deviceTime)); + json.add("fixTime", dateFormat.format(fixTime)); + json.add("valid", valid); + json.add("latitude", latitude); + json.add("longitude", longitude); + json.add("altitude", altitude); + json.add("speed", speed); + json.add("course", course); + json.add("address", address); + //json.add("extendedInfo", extendedInfo); + return json.build(); + } + + @Override + public void fromJson(JsonObject json) throws ParseException { + id = json.getJsonNumber("id").longValue(); + deviceId = json.getJsonNumber("deviceId").longValue(); + serverTime = dateFormat.parse(json.getString("serverTime")); + deviceTime = dateFormat.parse(json.getString("deviceTime")); + fixTime = dateFormat.parse(json.getString("fixTime")); + valid = json.getBoolean("valid"); + latitude = json.getJsonNumber("latitude").doubleValue(); + longitude = json.getJsonNumber("longitude").doubleValue(); + altitude = json.getJsonNumber("altitude").doubleValue(); + speed = json.getJsonNumber("speed").doubleValue(); + course = json.getJsonNumber("course").doubleValue(); + address = json.getString("address"); + //extendedInfo = json.getString("extendedInfo"); + } + } |