diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-04-25 11:02:26 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-04-25 11:02:26 +1200 |
commit | 9ea2dbe8400bd1ab30451e3dc2b85aa7a583a8e1 (patch) | |
tree | b9d71935821a7c895c11f87b1eb1fe3579f5289b /src | |
parent | 5859529fa396cf74bdddc714ea1936c972d01579 (diff) | |
download | traccar-server-9ea2dbe8400bd1ab30451e3dc2b85aa7a583a8e1.tar.gz traccar-server-9ea2dbe8400bd1ab30451e3dc2b85aa7a583a8e1.tar.bz2 traccar-server-9ea2dbe8400bd1ab30451e3dc2b85aa7a583a8e1.zip |
Use Java standard Json library
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 4 | ||||
-rw-r--r-- | src/org/traccar/database/ResultSetConverter.java | 42 | ||||
-rw-r--r-- | src/org/traccar/http/MainServlet.java | 17 |
3 files changed, 34 insertions, 29 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 172fbf763..2f029e74c 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -22,11 +22,11 @@ import java.net.URL; import java.net.URLClassLoader; import java.sql.*; import java.util.*; +import javax.json.JsonArray; import javax.sql.DataSource; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; -import org.json.JSONArray; import org.traccar.helper.DriverDelegate; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -362,7 +362,7 @@ public class DataManager { } } - public JSONArray getDevices(long userId) throws SQLException { + public JsonArray getDevices(long userId) throws SQLException { Connection connection = dataSource.getConnection(); try { diff --git a/src/org/traccar/database/ResultSetConverter.java b/src/org/traccar/database/ResultSetConverter.java index 32289f756..e4aec956c 100644 --- a/src/org/traccar/database/ResultSetConverter.java +++ b/src/org/traccar/database/ResultSetConverter.java @@ -15,24 +15,29 @@ */ package org.traccar.database; -import org.json.JSONArray; -import org.json.JSONObject; - -import java.sql.SQLException; import java.sql.ResultSet; import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObjectBuilder; public class ResultSetConverter { + + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); - public static JSONArray convert(ResultSet rs) throws SQLException { + public static JsonArray convert(ResultSet rs) throws SQLException { - JSONArray json = new JSONArray(); + JsonArrayBuilder json = Json.createArrayBuilder(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { int numColumns = rsmd.getColumnCount(); - JSONObject obj = new JSONObject(); + JsonObjectBuilder obj = Json.createObjectBuilder(); for (int i = 1; i <= numColumns; i++) { @@ -40,41 +45,40 @@ public class ResultSetConverter { switch (rsmd.getColumnType(i)) { case java.sql.Types.BIGINT: - obj.put(columnName, rs.getInt(columnName)); + obj.add(columnName, rs.getInt(columnName)); break; case java.sql.Types.BOOLEAN: - obj.put(columnName, rs.getBoolean(columnName)); + obj.add(columnName, rs.getBoolean(columnName)); break; case java.sql.Types.DOUBLE: - obj.put(columnName, rs.getDouble(columnName)); + obj.add(columnName, rs.getDouble(columnName)); break; case java.sql.Types.FLOAT: - obj.put(columnName, rs.getFloat(columnName)); + obj.add(columnName, rs.getFloat(columnName)); break; case java.sql.Types.INTEGER: - obj.put(columnName, rs.getInt(columnName)); + obj.add(columnName, rs.getInt(columnName)); break; case java.sql.Types.NVARCHAR: - obj.put(columnName, rs.getNString(columnName)); + obj.add(columnName, rs.getNString(columnName)); break; case java.sql.Types.VARCHAR: - obj.put(columnName, rs.getString(columnName)); + obj.add(columnName, rs.getString(columnName)); break; case java.sql.Types.DATE: - obj.put(columnName, rs.getDate(columnName)); + obj.add(columnName, dateFormat.format(rs.getDate(columnName))); break; case java.sql.Types.TIMESTAMP: - obj.put(columnName, rs.getTimestamp(columnName)); + obj.add(columnName, dateFormat.format(rs.getTimestamp(columnName))); break; default: - obj.put(columnName, rs.getObject(columnName)); break; } } - json.put(obj); + json.add(obj.build()); } - return json; + return json.build(); } } diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index e569b4892..7483b22b4 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -17,13 +17,14 @@ package org.traccar.http; import java.io.IOException; import java.sql.SQLException; +import javax.json.Json; +import javax.json.JsonObjectBuilder; import javax.servlet.AsyncContext; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONObject; import org.traccar.Context; import org.traccar.helper.Log; @@ -72,19 +73,19 @@ public class MainServlet extends HttpServlet { private void device(HttpServletRequest req, HttpServletResponse resp) throws IOException { - long userId = 1;//(Long) req.getSession().getAttribute(USER_ID); + long userId = (Long) req.getSession().getAttribute(USER_ID); - JSONObject result = new JSONObject(); + JsonObjectBuilder result = Json.createObjectBuilder(); try { - result.put("success", true); - result.put("data", Context.getDataManager().getDevices(userId)); + result.add("success", true); + result.add("data", Context.getDataManager().getDevices(userId)); } catch(SQLException error) { - result.put("success", false); - result.put("error", error.getMessage()); + result.add("success", false); + result.add("error", error.getMessage()); } - resp.getWriter().println(result.toString()); + resp.getWriter().println(result.build().toString()); } private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |