[chronojump] Refactorized json



commit 4ee10109a5995a7823334d71c039bf9725d4d73a
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Jun 18 14:09:03 2019 +0200

    Refactorized json

 src/Makefile.am                    |   5 +-
 src/chronojumpImporter.cs          |   4 +-
 src/gui/chronojump.cs              |   2 +-
 src/gui/dialogPersonPopup.cs       |   2 +-
 src/gui/encoder.cs                 |   2 +-
 src/gui/networks.cs                |   6 +-
 src/{json.cs => json/compujump.cs} | 408 +------------------------------------
 src/json/exhibitions.cs            | 137 +++++++++++++
 src/json/utils.cs                  |  46 +++++
 src/sqlite/json.cs                 |   4 +-
 10 files changed, 200 insertions(+), 416 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 3609256d..04082e00 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -155,7 +155,10 @@ SOURCES = \
        forcePlatform.cs\
        forceSensor.cs\
        genericObjects.cs\
-       json.cs\
+       json/json.cs\
+       json/utils.cs\
+       json/compujump.cs\
+       json/exhibitions.cs\
        jump.cs\
        jumpsProfile.cs\
        jumpType.cs\
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
index 91c2d997..515956a3 100644
--- a/src/chronojumpImporter.cs
+++ b/src/chronojumpImporter.cs
@@ -241,7 +241,7 @@ class ChronojumpImporter
                        foreach(JsonValue session in json["sessions"])
                        {
                                if (session.ContainsKey ("uniqueID") && session ["uniqueID"] == sessionId) {
-                                       return JsonUtils.valueOrDefault (session, "name", "UNKNOWN");
+                                       return JsonUtils.ValueOrDefault (session, "name", "UNKNOWN");
                                }
                        }
                        LogB.Information ("Trying to import a session that we can't find the name. Output:" + 
information.output);
@@ -255,7 +255,7 @@ class ChronojumpImporter
 
                if (information.success) {
                        JsonValue json = JsonValue.Parse (information.output);
-                       return new Result (true, JsonUtils.valueOrDefault(json, "databaseVersion", "0"));
+                       return new Result (true, JsonUtils.ValueOrDefault(json, "databaseVersion", "0"));
                } else {
                        return information;
                }
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 1d1f0fb0..d385bc1d 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -4938,7 +4938,7 @@ public partial class ChronoJumpWindow
                                sprint.Positions, sprint.GetSplitTimesAsList(),
                                k, vmax, amax, fmax, pmax);
 
-               Json js = new Json();
+               JsonCompujump js = new JsonCompujump();
                if( ! js.UploadSprintData(usdo) )
                {
                        LogB.Error(js.ResultMessage);
diff --git a/src/gui/dialogPersonPopup.cs b/src/gui/dialogPersonPopup.cs
index 34b3f4da..18c04de0 100644
--- a/src/gui/dialogPersonPopup.cs
+++ b/src/gui/dialogPersonPopup.cs
@@ -252,7 +252,7 @@ public class DialogPersonPopup
                        {
                                LogB.Information("Clicked button done: " + count.ToString());
 
-                               Json json = new Json();
+                               JsonCompujump json = new JsonCompujump();
                                json.UpdateTask(list_buttons_done_id[count], 1);
 
                                //button.Sensitive = false;
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index bca23d33..61d0894a 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -7000,7 +7000,7 @@ public partial class ChronoJumpWindow
                                                         */
 
                                                        LogB.Information("calling Upload");
-                                                       Json js = new Json();
+                                                       JsonCompujump js = new JsonCompujump();
                                                        UploadEncoderDataFullObject uedfo = new 
UploadEncoderDataFullObject(
                                                                        -1, //uniqueID
                                                                        currentPerson.UniqueID,
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index ba90d5ee..2416b106 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -578,7 +578,7 @@ public partial class ChronoJumpWindow
 
                bool currentPersonWasNull = (currentPerson == null);
                bool pChangedShowTasks = false;
-               Json json = new Json();
+               JsonCompujump json = new JsonCompujump();
 
                //select person by RFID
                Person pLocal = SqlitePerson.SelectByRFID(capturedRFID);
@@ -746,7 +746,7 @@ public partial class ChronoJumpWindow
                }
        }
 
-       private void insertAndAssignPersonSessionIfNeeded(Json json)
+       private void insertAndAssignPersonSessionIfNeeded(JsonCompujump json)
        {
                PersonSession ps = SqlitePersonSession.Select(false, currentPerson.UniqueID, 
currentSession.UniqueID);
                if(ps.UniqueID == -1)
@@ -776,7 +776,7 @@ public partial class ChronoJumpWindow
        private void getTasksExercisesAndPopup()
        {
                //1) get tasks
-               Json json = new Json();
+               JsonCompujump json = new JsonCompujump();
                List<Task> tasks = json.GetTasks(currentPerson.UniqueID, configChronojump.CompujumpStationID);
 
                //2) get exercises and insert if needed (only on encoder)
diff --git a/src/json.cs b/src/json/compujump.cs
similarity index 72%
rename from src/json.cs
rename to src/json/compujump.cs
index ac25d531..1c943a49 100644
--- a/src/json.cs
+++ b/src/json/compujump.cs
@@ -29,231 +29,13 @@ using System.Collections.Generic; //Dictionary
 using Mono.Unix;
 
 
-public class Json
+public class JsonCompujump : Json
 {
-       private bool connected; //know if server is connected. Do it when there's a change on RFID (pulse)
-       public string ResultMessage;
-       static string serverUrl = "http://api.chronojump.org:8080";;
-       //string serverUrl = "http://192.168.200.1:8080";;
-
-       public static void ChangeServerUrl(string url)
-       {
-               serverUrl = url;
-       }
-
-       public Json()
+       public JsonCompujump()
        {
                ResultMessage = "";
        }
 
-       public bool PostCrashLog(string email, string comments) 
-       {
-               string filePath = UtilAll.GetLogFileOld();
-
-               if(! File.Exists(filePath)) {
-                       this.ResultMessage = Catalog.GetString("Could not send file.\nIt does not exist.");
-                       return false;
-               }
-
-               if(comments != null && comments != "")
-                       Util.InsertTextBeginningOfFile(
-                                       "----------\nUser comments:\n" + comments + "\n----------\n", 
filePath);
-
-               // Create a request using a URL that can receive a post. 
-               if (! createWebRequest(requestType.GENERIC, "/backtrace/" + 
UtilAll.ReadVersionFromBuildInfo() + "-" + email))
-                       return false;
-
-               // Set the Method property of the request to POST.
-               request.Method = "POST";
-
-               // Create POST data and convert it to a byte array.
-               byte[] byteArray = readFile(filePath);
-
-               // Set the ContentType property of the WebRequest.
-               request.ContentType = "application/x-www-form-urlencoded";
-
-               // Set the ContentLength property of the WebRequest.
-               request.ContentLength = byteArray.Length;
-
-               // Get the request stream.
-               Stream dataStream;
-               if(! getWebRequestStream (request, out dataStream, Catalog.GetString("Could not send file.")))
-                       return false;
-
-               // Write the data to the request stream.
-               dataStream.Write (byteArray, 0, byteArray.Length);
-
-               // Close the Stream object.
-               dataStream.Close ();
-        
-               // Get the response.
-               WebResponse response;
-               if(! getWebResponse (request, out response, Catalog.GetString("Could not send file.")))
-                       return false;
-
-
-               // Display the status.
-               LogB.Information(((HttpWebResponse)response).StatusDescription);
-
-               // Get the stream containing content returned by the server.
-               dataStream = response.GetResponseStream ();
-
-               // Open the stream using a StreamReader for easy access.
-               StreamReader reader = new StreamReader (dataStream);
-
-               // Read the content.
-               string responseFromServer = reader.ReadToEnd ();
-
-               // Display the content.
-               LogB.Information(responseFromServer);
-
-               // Clean up the streams.
-               reader.Close ();
-               dataStream.Close ();
-               response.Close ();    
-
-
-               JsonValue result = JsonValue.Parse(responseFromServer);
-               string crash_id = result["crash_id"];
-               LogB.Information("crash_id: ", crash_id);
-
-               this.ResultMessage = Catalog.GetString("Log sent. Thank you.");
-               return true;
-       }
-       
-       private byte[] readFile(string filePath)
-       {
-               return System.IO.File.ReadAllBytes(filePath); 
-       }
-
-
-       //public bool ChronojumpUpdated = true;
-       public bool GetLastVersion(string currentVersion) 
-       {
-               // Create a request using a URL that can receive a post. 
-               if (! createWebRequest(requestType.GENERIC, "/version"))
-                       return false;
-               
-               // Set the Method property of the request to GET.
-               request.Method = "GET";
-               
-               // Set the ContentType property of the WebRequest.
-               //request.ContentType = "application/x-www-form-urlencoded";
-               
-               HttpWebResponse response;
-               if(! getHttpWebResponse (request, out response, Catalog.GetString("Could not get last 
version.")))
-                       return false;
-
-               string responseFromServer;
-               using (var sr = new StreamReader(response.GetResponseStream()))
-               {
-                       responseFromServer = sr.ReadToEnd();
-               }
-
-               //this prints:
-               // {"stable": "1.4.9"}
-               //this.ResultMessage = "Last version published: " + responseFromServer;
-               
-               string [] strFull = responseFromServer.Split(new char[] {':'});
-               int startPos = strFull[1].IndexOf('"') +1;
-               int endPos = strFull[1].LastIndexOf('"') -2;
-
-               string lastVersionPublished = strFull[1].Substring(startPos,endPos); //1.4.9
-
-               /*
-               string updateStr = "";
-               if(currentVersion != lastVersionPublished)
-                       updateStr = "\n\n" + Catalog.GetString("Update software at ") + "www.chronojump.org";
-                       */
-
-               string str =
-                       Catalog.GetString("Installed version is: ") + currentVersion + "\n" + 
-                       Catalog.GetString("Last version published: ") + lastVersionPublished;
-
-               VersionCompare vCompare = new VersionCompare(
-                               new Version31(currentVersion),
-                               new Version31(lastVersionPublished));
-               str += "\n\n" + vCompare.ResultStr;
-
-               this.ResultMessage = str;
-               
-               //ChronojumpUpdated = (currentVersion == ResultMessage);
-
-               return true;
-       }
-
-       WebRequest request; //generic request (for all methods except ping)
-       /*
-        * if software just started, ping gets stuck by network problems, and user try to exit software,
-        * thread.Abort doesn't kill the thread properly
-        * just kill the webRequest
-        */
-       WebRequest requestPing;
-       bool requestPingAborting;
-
-       public void PingAbort()
-       {
-               requestPingAborting = true;
-               if(requestPing != null)
-                       requestPing.Abort(); //cancel an asynchronous request
-       }
-       public bool Ping(string osVersion, string cjVersion, string machineID) 
-       {
-               requestPingAborting = false;
-
-               if (! createWebRequest(requestType.PING, "/ping"))
-                       return false;
-
-               // Set the Method property of the request to POST.
-               requestPing.Method = "POST";
-
-               // Set the ContentType property of the WebRequest.
-               requestPing.ContentType = "application/json";
-
-               // Creates the json object
-               JsonObject json = new JsonObject();
-               json.Add("os_version", osVersion);
-               json.Add("cj_version", cjVersion);
-               json.Add("machine_id", machineID);
-
-               // Converts it to a String
-               String js = json.ToString();
-
-               // Writes the json object into the request dataStream
-               Stream dataStream;
-               if(! getWebRequestStream (requestPing, out dataStream, "Could not send ping (A)."))
-                       return false;
-
-               if(requestPingAborting) {
-                       LogB.Information("Aborted from PingAbort");
-                       return false;
-               }
-
-               dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
-
-               dataStream.Close ();
-
-               // Get the response.
-               WebResponse response;
-               if(! getWebResponse (requestPing, out response, "Could not send ping (B)."))
-                       return false;
-
-               if(requestPingAborting) {
-                       LogB.Information("Aborted from PingAbort");
-                       return false;
-               }
-
-               // Display the status (will be 201, CREATED)
-               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
-
-               // Clean up the streams.
-               dataStream.Close ();
-               response.Close ();
-               
-               this.ResultMessage = "Ping sent.";
-               return true;
-       }
-
        public Person GetPersonByRFID(string rfid)
        {
                connected = false;
@@ -309,7 +91,6 @@ public class Json
 
                connected = true;
                return person;
-
        }
 
        public double LastPersonByRFIDHeight = 0;
@@ -865,146 +646,7 @@ public class Json
                return true;
        }
 
-       enum requestType { GENERIC, PING };
-       bool createWebRequest(requestType rt, string webService)
-       {
-               try {
-                       if(rt == requestType.GENERIC)
-                               request = WebRequest.Create (serverUrl + webService);
-                       else //(rt == requestType.PING)
-                               requestPing = WebRequest.Create (serverUrl + webService);
-
-               } catch {
-                       this.ResultMessage =
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
-                                               serverUrl) + "\n\nMaybe proxy is not configured";
-                       //System.Net.WebRequest.GetSystemWebProxy System.NullReferenceException: Object 
reference not set to an instance of an object
-                       return false;
-               }
-
-               return true;
-       }
-
-       bool getWebRequestStream(WebRequest req, out Stream dataStream, string errMessage)
-       {
-               dataStream = null;
-
-               try {
-                       dataStream = req.GetRequestStream ();
-               } catch {
-                       this.ResultMessage = errMessage + "\n" +
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
-                               serverUrl);
-                       return false;
-               }
-
-               return true;
-       }
-
-       bool getWebResponse(WebRequest req, out WebResponse response, string errMessage)
-       {
-               response = null;
-
-               try {
-                       response = req.GetResponse ();
-               } catch {
-                       this.ResultMessage = errMessage + "\n" +
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
-                               serverUrl);
-                       return false;
-               }
-
-               return true;
-       }
-       bool getHttpWebResponse(WebRequest req, out HttpWebResponse response, string errMessage)
-       {
-               response = null;
-
-               try {
-                       response = (HttpWebResponse) req.GetResponse ();
-               } catch {
-                       this.ResultMessage = errMessage + "\n" +
-                               string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."),
-                               serverUrl);
-                       return false;
-               }
-
-               return true;
-       }
-
-       //table created with:
-       //CREATE TABLE exhibitionTest(dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, schoolID INT NOT NULL, groupID 
INT NOT NULL, personID INT NOT NULL, testType CHAR(10), result DOUBLE);
-       public bool UploadExhibitionTest(ExhibitionTest et)
-       {
-               // Create a request using a URL that can receive a post.
-               if (! createWebRequest(requestType.GENERIC, "/uploadExhibitionTestData"))
-                       return false;
-
-               // Set the Method property of the request to POST.
-               request.Method = "POST";
-
-               // Set the ContentType property of the WebRequest.
-               request.ContentType = "application/json; Charset=UTF-8"; //but this is not enough, see this 
line:
-               //exerciseName = Util.RemoveAccents(exerciseName);
-
-               // Creates the json object
-               JsonObject json = new JsonObject();
-
-               json.Add("schoolID", et.schoolID);
-               json.Add("groupID", et.groupID);
-               json.Add("personID", et.personID);
-               json.Add("testType", et.testType.ToString());
-               json.Add("result", et.resultToJson);
-
-               // Converts it to a String
-               String js = json.ToString();
-
-               // Writes the json object into the request dataStream
-               Stream dataStream;
-               if(! getWebRequestStream (request, out dataStream, "Could not upload exhibition test data 
(A)."))
-                       return false;
-
-               dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
-
-               dataStream.Close ();
-
-               // Get the response.
-               WebResponse response;
-               if(! getWebResponse (request, out response, "Could not upload exhibition test data (B)."))
-                       return false;
-
-               // Display the status (will be 202, CREATED)
-               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
-
-               // Clean up the streams.
-               dataStream.Close ();
-               response.Close ();
-
-               this.ResultMessage = "Exhibition test data sent.";
-               return true;
-       }
-
-
-       public bool Connected {
-               get { return connected; }
-       }
-
-       ~Json() {}
-}
-
-class JsonUtils
-{
-       public static JsonValue valueOrDefault(JsonValue jsonObject, string key, string defaultValue)
-       {
-               // Returns jsonObject[key] if it exists. If the key doesn't exist returns defaultValue and
-               // logs the anomaly into the Chronojump log.
-               if (jsonObject.ContainsKey (key)) {
-                       return jsonObject [key];
-               } else {
-                       LogB.Information ("JsonUtils::valueOrDefault: returning default (" + defaultValue + 
") from JSON: " + jsonObject.ToString ());
-                       return defaultValue;
-               }
-       }
+       ~JsonCompujump() {}
 }
 
 public class UploadSprintDataObject
@@ -1399,47 +1041,3 @@ public class StationCount
                return stationName + " (" + tasksCount.ToString() + ")";
        }
 }
-
-//eg. YOMO
-public class ExhibitionTest
-{
-       public int schoolID;
-       public int groupID;
-       public int personID;
-       public enum testTypes { JUMP, RUN, INERTIAL, FORCE_ROPE, FORCE_SHOT };
-       public testTypes testType;
-       public double result;
-       /* result is:
-        *      on jumps is height
-        *      on runs is maximum speed ?
-        *      on pull rope is maximum force
-        *      on shot is maximum force
-        *      on inertial is mean power of the maximum repetiton
-        */
-
-       public ExhibitionTest(int schoolID, int groupID, int personID, testTypes testType, double result)
-       {
-               this.schoolID = schoolID;
-               this.groupID = groupID;
-               this.personID = personID;
-               this.testType = testType;
-               this.result = result;
-       }
-
-       public string ToSQLTempInsertString()
-       {
-               return
-                       schoolID.ToString() + ", " +
-                       groupID.ToString() + ", " +
-                       personID.ToString() + ", \"" +
-                       testType.ToString() + "\", " +
-                       resultToJson;
-       }
-
-       //convert to decimal point and str
-       public string resultToJson {
-               get { return Util.ConvertToPoint(result); }
-       }
-
-       ~ExhibitionTest() {}
-}
diff --git a/src/json/exhibitions.cs b/src/json/exhibitions.cs
new file mode 100644
index 00000000..69bd8419
--- /dev/null
+++ b/src/json/exhibitions.cs
@@ -0,0 +1,137 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump 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 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump 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 this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016-2017 Carles Pina & Xavier de Blas
+ */
+
+using System;
+using System.Net;
+using System.Web;
+using System.IO;
+using System.Json;
+using System.Text;
+using System.Collections;
+using System.Collections.Generic; //Dictionary
+using Mono.Unix;
+
+
+public class JsonExhibitions : Json
+{
+       public JsonExhibitions()
+       {
+               ResultMessage = "";
+       }
+
+       //table created with:
+       //CREATE TABLE exhibitionTest(dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, schoolID INT NOT NULL, groupID 
INT NOT NULL, personID INT NOT NULL, testType CHAR(10), result DOUBLE);
+       public bool UploadExhibitionTest(ExhibitionTest et)
+       {
+               // Create a request using a URL that can receive a post.
+               if (! createWebRequest(requestType.GENERIC, "/uploadExhibitionTestData"))
+                       return false;
+
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/json; Charset=UTF-8"; //but this is not enough, see this 
line:
+               //exerciseName = Util.RemoveAccents(exerciseName);
+
+               // Creates the json object
+               JsonObject json = new JsonObject();
+
+               json.Add("schoolID", et.schoolID);
+               json.Add("groupID", et.groupID);
+               json.Add("personID", et.personID);
+               json.Add("testType", et.testType.ToString());
+               json.Add("result", et.resultToJson);
+
+               // Converts it to a String
+               String js = json.ToString();
+
+               // Writes the json object into the request dataStream
+               Stream dataStream;
+               if(! getWebRequestStream (request, out dataStream, "Could not upload exhibition test data 
(A)."))
+                       return false;
+
+               dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
+
+               dataStream.Close ();
+
+               // Get the response.
+               WebResponse response;
+               if(! getWebResponse (request, out response, "Could not upload exhibition test data (B)."))
+                       return false;
+
+               // Display the status (will be 202, CREATED)
+               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+
+               // Clean up the streams.
+               dataStream.Close ();
+               response.Close ();
+
+               this.ResultMessage = "Exhibition test data sent.";
+               return true;
+       }
+
+       ~JsonExhibitions() {}
+}
+
+
+//eg. YOMO
+public class ExhibitionTest
+{
+       public int schoolID;
+       public int groupID;
+       public int personID;
+       public enum testTypes { JUMP, RUN, INERTIAL, FORCE_ROPE, FORCE_SHOT };
+       public testTypes testType;
+       public double result;
+       /* result is:
+        *      on jumps is height
+        *      on runs is maximum speed ?
+        *      on pull rope is maximum force
+        *      on shot is maximum force
+        *      on inertial is mean power of the maximum repetiton
+        */
+
+       public ExhibitionTest(int schoolID, int groupID, int personID, testTypes testType, double result)
+       {
+               this.schoolID = schoolID;
+               this.groupID = groupID;
+               this.personID = personID;
+               this.testType = testType;
+               this.result = result;
+       }
+
+       public string ToSQLTempInsertString()
+       {
+               return
+                       schoolID.ToString() + ", " +
+                       groupID.ToString() + ", " +
+                       personID.ToString() + ", \"" +
+                       testType.ToString() + "\", " +
+                       resultToJson;
+       }
+
+       //convert to decimal point and str
+       public string resultToJson {
+               get { return Util.ConvertToPoint(result); }
+       }
+
+       ~ExhibitionTest() {}
+}
diff --git a/src/json/utils.cs b/src/json/utils.cs
new file mode 100644
index 00000000..7e11a040
--- /dev/null
+++ b/src/json/utils.cs
@@ -0,0 +1,46 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump 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 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump 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 this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016-2017 Carles Pina & Xavier de Blas
+ */
+
+using System;
+using System.Net;
+using System.Web;
+using System.IO;
+using System.Json;
+using System.Text;
+using System.Collections;
+using System.Collections.Generic; //Dictionary
+using Mono.Unix;
+
+
+static class JsonUtils
+{
+       public static JsonValue ValueOrDefault(JsonValue jsonObject, string key, string defaultValue)
+       {
+               // Returns jsonObject[key] if it exists. If the key doesn't exist returns defaultValue and
+               // logs the anomaly into the Chronojump log.
+               if (jsonObject.ContainsKey (key)) {
+                       return jsonObject [key];
+               } else {
+                       LogB.Information ("JsonUtils::valueOrDefault: returning default (" + defaultValue + 
") from JSON: " + jsonObject.ToString ());
+                       return defaultValue;
+               }
+       }
+}
+
diff --git a/src/sqlite/json.cs b/src/sqlite/json.cs
index 5cee0626..c0998621 100644
--- a/src/sqlite/json.cs
+++ b/src/sqlite/json.cs
@@ -236,7 +236,7 @@ class SqliteJson : Sqlite
 
        public static void UploadExhibitionTest(ExhibitionTest et)
        {
-               Json js = new Json();
+               JsonExhibitions js = new JsonExhibitions();
                if( ! js.UploadExhibitionTest (et))
                {
                        LogB.Error(js.ResultMessage);
@@ -248,7 +248,7 @@ class SqliteJson : Sqlite
        //At YOMO free network with transaction each record needs 0.3 seconds
        public static void UploadExhibitionTestsPending()
        {
-               Json json = new Json();
+               JsonExhibitions json = new JsonExhibitions();
                Sqlite.Open(); // ---------------->
 
                int countSucceded = 0;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]