/* * Copyright 2015 Ludwig Andersson * * This file is part of Thermospy-android. * * Thermospy-android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Thermospy-android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thermospy-android. If not, see <http://www.gnu.org/licenses/>. */ package com.luan.thermospy.android.core.pojo; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonIOException; import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.Type; import java.util.Date; /** * LogSession POJO */ public class LogSession { public static JSONObject toJson(LogSession logSession) throws JSONException { // Creates the json object which will manage the information received GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { @Override public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(src.getTime()); } }); Gson gson = builder.create(); try { return new JSONObject(gson.toJson(logSession, LogSession.class)); } catch (JSONException | JsonIOException e) { throw new JSONException(e.getMessage()); } } public static LogSession fromJson(String jsonString) throws JsonSyntaxException { GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); } }); Gson gson = builder.create(); LogSession logSession =null; try { logSession = gson.fromJson(jsonString, LogSession.class); } catch (JsonSyntaxException ex) { throw ex; } return logSession; } public Boolean isOpen() { return isopen; } public void setIsOpen(Boolean isOpen) { this.isopen = isOpen; } public Integer getTargetTemperature() { return targetTemperature; } public void setTargetTemperature(Integer targetTemperature) { this.targetTemperature = targetTemperature; } int id; private Integer targetTemperature; private Boolean isopen; private String name; private Date startTimestamp; private Date endTimestamp; private String comment; public LogSession() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getStartTimestamp() { return startTimestamp; } public void setStartTimestamp(Date startTimestamp) { this.startTimestamp = startTimestamp; } public Date getEndTimestamp() { return endTimestamp; } public void setEndTimestamp(Date endTimestamp) { this.endTimestamp = endTimestamp; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } @Override public String toString() { return name; } }