From 54e738d3281a463a0ea3fcf2026c2464529a9b58 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 19 Dec 2016 08:01:20 +1300 Subject: Remove own JSON converter --- src/org/traccar/database/QueryBuilder.java | 41 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index 201240f2f..127229700 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -15,15 +15,13 @@ */ package org.traccar.database; +import com.fasterxml.jackson.core.JsonProcessingException; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.MiscFormatter; -import javax.json.Json; -import javax.json.JsonReader; -import javax.json.stream.JsonParsingException; import javax.sql.DataSource; -import java.io.StringReader; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Connection; @@ -279,14 +277,15 @@ public final class QueryBuilder { setDate(name, (Date) method.invoke(object)); } else if (method.getReturnType().equals(byte[].class)) { setBlob(name, (byte[]) method.invoke(object)); - } else if (method.getReturnType().equals(Map.class)) { - if (Context.getConfig().getBoolean("database.xml")) { + } else { + if (method.getReturnType().equals(Map.class) + && Context.getConfig().getBoolean("database.xml")) { setString(name, MiscFormatter.toXmlString((Map) method.invoke(object))); } else { - setString(name, MiscFormatter.toJsonString((Map) method.invoke(object))); + setString(name, Context.getObjectMapper().writeValueAsString(method.invoke(object))); } } - } catch (IllegalAccessException | InvocationTargetException error) { + } catch (IllegalAccessException | InvocationTargetException | JsonProcessingException error) { Log.warning(error); } } @@ -380,28 +379,28 @@ public final class QueryBuilder { } } }); - } else if (parameterType.equals(Map.class)) { + } else if (parameterType.equals(byte[].class)) { processors.add(new ResultSetProcessor() { @Override public void process(T object, ResultSet resultSet) throws SQLException { - String value = resultSet.getString(name); - if (value != null) { - try (JsonReader reader = Json.createReader(new StringReader(value))) { - method.invoke(object, MiscFormatter.fromJson(reader.readObject())); - } catch (IllegalAccessException | InvocationTargetException | JsonParsingException error) { - Log.warning(error); - } + try { + method.invoke(object, resultSet.getBytes(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + Log.warning(error); } } }); - } else if (parameterType.equals(byte[].class)) { + } else { processors.add(new ResultSetProcessor() { @Override public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getBytes(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - Log.warning(error); + String value = resultSet.getString(name); + if (value != null) { + try { + method.invoke(object, Context.getObjectMapper().readValue(value, Map.class)); + } catch (InvocationTargetException | IllegalAccessException | IOException error) { + Log.warning(error); + } } } }); -- cgit v1.2.3