From 3083db3929e27933d3ab5eab9fa0c1e221e94eec Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 27 Apr 2015 12:52:33 +1200 Subject: Implement JSON conversion --- src/org/traccar/model/Data.java | 28 ++++++++++++++++++++++- src/org/traccar/model/Device.java | 31 ++++++++++++++++++++++++- src/org/traccar/model/Position.java | 44 +++++++++++++++++++++++++++++++++++- src/org/traccar/model/User.java | 45 ++++++++++++++++++++++++++++++++++--- 4 files changed, 142 insertions(+), 6 deletions(-) (limited to 'src/org/traccar/model') diff --git a/src/org/traccar/model/Data.java b/src/org/traccar/model/Data.java index ce23154c2..1cef813a3 100644 --- a/src/org/traccar/model/Data.java +++ b/src/org/traccar/model/Data.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 Data { +public class Data implements JsonConvertable { private long id; public long getId() { return id; } @@ -39,4 +45,24 @@ public class Data { 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("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")); + //extendedInfo = json.getString("extendedInfo"); + } + } diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 273faebcf..4e2f57690 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -15,9 +15,14 @@ */ 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; -public class Device { +public class Device implements JsonConvertable { private long id; public long getId() { return id; } @@ -39,4 +44,28 @@ public class Device { private long dataId; + @Override + public JsonObject toJson() { + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add("id", id); + json.add("name", name); + json.add("uniqueId", uniqueId); + json.add("status", status); + json.add("lastUpdate", dateFormat.format(lastUpdate)); + json.add("positionId", positionId); + json.add("dataId", dataId); + return json.build(); + } + + @Override + public void fromJson(JsonObject json) throws ParseException { + id = json.getJsonNumber("id").longValue(); + name = json.getString("name"); + uniqueId = json.getString("uniqueId"); + status = json.getString("status"); + lastUpdate = dateFormat.parse(json.getString("lastUpdate")); + positionId = json.getJsonNumber("positionId").longValue(); + dataId = json.getJsonNumber("dataId").longValue(); + } + } 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"); + } + } diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 93ce111ec..a3eaf5a10 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -15,7 +15,12 @@ */ package org.traccar.model; -public class User { +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import org.traccar.database.JsonConvertable; + +public class User implements JsonConvertable { private long id; @@ -31,14 +36,48 @@ public class User { private String language; - private String distance_unit; + private String distanceUnit; - private String speed_unit; + private String speedUnit; private double latitude; private double longitude; private int zoom; + + @Override + public JsonObject toJson() { + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add("id", id); + json.add("email", email); + json.add("password", password); + json.add("readonly", readonly); + json.add("admin", admin); + json.add("map", map); + json.add("language", language); + json.add("distanceUnit", distanceUnit); + json.add("speedUnit", speedUnit); + json.add("latitude", latitude); + json.add("longitude", longitude); + json.add("zoom", zoom); + return json.build(); + } + + @Override + public void fromJson(JsonObject json) { + id = json.getJsonNumber("id").longValue(); + email = json.getString("email"); + password = json.getString("password"); + readonly = json.getBoolean("readonly"); + admin = json.getBoolean("admin"); + map = json.getString("map"); + language = json.getString("language"); + distanceUnit = json.getString("distanceUnit"); + speedUnit = json.getString("speedUnit"); + latitude = json.getJsonNumber("latitude").doubleValue(); + longitude = json.getJsonNumber("longitude").doubleValue(); + zoom = json.getJsonNumber("zoom").intValue(); + } } -- cgit v1.2.3