JSON file in mbed.

Dependencies:   MbedJSONValue SDFileSystem mbed

Fork of City_Game_JSON by Alex Vancoillie

Revision:
2:e6a095fc2274
Parent:
0:b3949bab2aee
Child:
3:0ace19f0cc2b
--- a/main.cpp	Wed Mar 15 08:03:43 2017 +0000
+++ b/main.cpp	Wed Mar 29 07:01:05 2017 +0000
@@ -1,40 +1,118 @@
 #include "mbed.h"
 #include "MbedJSONValue.h"
-#include "picojson.h"
+//#include "SDFileSystem.h"
+#include "mission.h"
 #include <iostream>
- 
+#include <vector>
+
 using namespace VivesCityGame;
 
-int main() {     
+//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // mosi, miso, sclk, cs
+
+/*
+#define charlimit 100
+char words[charlimit];
+int n = 0, c;
+*/
+
+int main() {
     MbedJSONValue demo;
-    
-    const char * json = "{\r\n  \"gameMode\":\"LocationGame\",\r\n  \"gameModeId\":1,\r\n  \"version\":\"1.235\",\r\n  \"creationDate\":\"11-02-2017\",\r\n  \"missions\":[\r\n    {\r\n        \"missionType\":\"gotoLocation\",\r\n        \"missionTypeId\":1,\r\n        \"missionMessage\":\"Go to the big roundabout in Oostende.\",\r\n        \"deadline\":true,\r\n        \"deadlineTime\":30,\r\n        \"latitude\":51.215430,\r\n        \"longitude\":2.928656,\r\n        \"radius\":200\r\n    },\r\n    {\r\n        \"missionType\":\"puzzle\",\r\n        \"missionTypeId\":13,\r\n        \"missionMessage\":\"Find the key A\",\r\n        \"hint\":\"Look in a tree\",\r\n        \"deadline\":false,\r\n    }\r\n  ]\r\n}\r\n";   
+
+    /*
+    FILE * fp1
+    = fopen("/sd/text.json", "r");
+    if(fp1 == NULL)
+    {
+        error("Could not open file for read!\r\n");
+    }
+
+    printf("Reading from SD card...\r\n");
+
+    while((c = fgetc(fp1)) && c! = EOF)
+    {
+        words[n] = c;
+        n++;
+    }
+
+    printf("Read from SD card: %s\r\n", words);
+
+    fclose(fp1);
+    */
+
+    const char * json = "{\"gameMode\":\"LocationGame\",\"gameModeId\":1,\"version\":\"1.235\",\"creationDate\":\"11-02-2017\",\"missions\":[{\"type\":\"gotoLocation\",\"id\":1,\"message\":\"Go to the big roundabout in Oostende.\",\"deadline\":true,\"deadlineTime\":30,\"latitude\":51.21543,\"longitude\":2.928656,\"radius\":200},{\"type\":\"puzzle\",\"id\":2,\"message\":\"Find the key A\",\"hint\":\"Look in a tree\",\"deadline\":false}]}";
     //const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
- 
-    //parse the previous string and fill the object demo
+
+    // parse the previous string and fill the object demo
     parse(demo, json);
-    
+
     printf(">>> testing if it works properly \r\n");
+
+    std::vector <Mission> missions; // declares an object of the class std::vector
+
+    for(std::size_t i = 0; i < demo["missions"].size(); i++)
+    {
+        Mission mission; // declares a local variable of the class Mission
+        mission.from_json(demo["missions"][i]);
+        missions.push_back(mission);
+    }
+
+    cout << "Missions: " << endl;
+
+    for(std::size_t i = 0; i < missions.size(); i++)
+    {
+        printf("Type: %s\r\n", missions[i].get_type().c_str());
+        printf("Id: %d\r\n", missions[i].get_id());
+        printf("Message: %s\r\n", missions[i].get_message().c_str());
+        printf("Hint: %s\r\n", missions[i].get_hint().c_str());
+        printf("Deadline: %s\r\n", missions[i].get_deadline() ? "true" : "false");
+        printf("DeadlineTime: %d\r\n", missions[i].get_deadlineTime());
+        printf("Latitude: %f\r\n", missions[i].get_latitude());
+        printf("Longitude: %f\r\n", missions[i].get_longitude());
+        printf("Radius: %d\r\n", missions[i].get_radius());
+    }
     
+    /*
+    for(std::size_t i = 0; i < missions.size(); i++)
+    {
+        cout << missions[i].to_string();
+    }
+    */
+
+    /*
     std::string game_mode;
     int game_mode_id;
     std::string my_ver;
     std::string creation_date;
-    //std::string my_missions;
-    //std::string mission_type;
-    
+    */
+
+    /*
     game_mode = demo["gameMode"].get<std::string>();
     game_mode_id = demo["gameModeId"].get<int>();
     my_ver = demo["version"].get<std::string>();
     creation_date = demo["creationDate"].get<std::string>();
-    //my_missions = demo["missions"][0].demo["missionType"].get<std::string>();
-    //mission_type = demo["missionType"].get<std::string>();
-    
+    */
+
+    /*for(int i = 0; i < 2; i++)
+    {
+        type = demo["missions"][i]["type"].get<std::string>();
+        type = demo["missions"][i]["id"].get<int>();
+        type = demo["missions"][i]["message"].get<std::string>();
+        type = demo["missions"][i]["hint"].get<std::string>();
+        type = demo["missions"][i]["deadline"].get<bool>();
+        type = demo["missions"][i]["deadlineTime"].get<int>();
+        type = demo["missions"][i]["altitude"].get<double>();
+        type = demo["missions"][i]["longitude"].get<double>();
+        type = demo["missions"][i]["radius"].get<int>();
+    }
+    */
+
+    /*
     printf("game_mode: %s\r\n", game_mode.c_str());
     printf("game_mode_id: %d\r\n", game_mode_id);
     printf("my_ver: %s\r\n", my_ver.c_str());
     printf("creation_date: %s\r\n", creation_date.c_str());
-    //printf("my_missions: %s\r\n", my_missions.c_str());
-    //printf("mission_type: %s\r\n", mission_type.c_str());
-    
-}
\ No newline at end of file
+    printf("lat: %f\r\n", latitude);
+    printf("long: %f\r\n", longitude);
+    printf("rad: %d\r\n", rad);
+    */
+}