JSON file in mbed.

Dependencies:   MbedJSONValue SDFileSystem mbed

Fork of City_Game_JSON by Alex Vancoillie

Committer:
AlexVC97
Date:
Wed Mar 29 07:01:05 2017 +0000
Revision:
2:e6a095fc2274
Parent:
0:b3949bab2aee
Child:
3:0ace19f0cc2b
City_Game_JSON

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexVC97 0:b3949bab2aee 1 #include "mbed.h"
AlexVC97 0:b3949bab2aee 2 #include "MbedJSONValue.h"
AlexVC97 2:e6a095fc2274 3 //#include "SDFileSystem.h"
AlexVC97 2:e6a095fc2274 4 #include "mission.h"
AlexVC97 0:b3949bab2aee 5 #include <iostream>
AlexVC97 2:e6a095fc2274 6 #include <vector>
AlexVC97 2:e6a095fc2274 7
AlexVC97 0:b3949bab2aee 8 using namespace VivesCityGame;
AlexVC97 0:b3949bab2aee 9
AlexVC97 2:e6a095fc2274 10 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // mosi, miso, sclk, cs
AlexVC97 2:e6a095fc2274 11
AlexVC97 2:e6a095fc2274 12 /*
AlexVC97 2:e6a095fc2274 13 #define charlimit 100
AlexVC97 2:e6a095fc2274 14 char words[charlimit];
AlexVC97 2:e6a095fc2274 15 int n = 0, c;
AlexVC97 2:e6a095fc2274 16 */
AlexVC97 2:e6a095fc2274 17
AlexVC97 2:e6a095fc2274 18 int main() {
AlexVC97 0:b3949bab2aee 19 MbedJSONValue demo;
AlexVC97 2:e6a095fc2274 20
AlexVC97 2:e6a095fc2274 21 /*
AlexVC97 2:e6a095fc2274 22 FILE * fp1
AlexVC97 2:e6a095fc2274 23 = fopen("/sd/text.json", "r");
AlexVC97 2:e6a095fc2274 24 if(fp1 == NULL)
AlexVC97 2:e6a095fc2274 25 {
AlexVC97 2:e6a095fc2274 26 error("Could not open file for read!\r\n");
AlexVC97 2:e6a095fc2274 27 }
AlexVC97 2:e6a095fc2274 28
AlexVC97 2:e6a095fc2274 29 printf("Reading from SD card...\r\n");
AlexVC97 2:e6a095fc2274 30
AlexVC97 2:e6a095fc2274 31 while((c = fgetc(fp1)) && c! = EOF)
AlexVC97 2:e6a095fc2274 32 {
AlexVC97 2:e6a095fc2274 33 words[n] = c;
AlexVC97 2:e6a095fc2274 34 n++;
AlexVC97 2:e6a095fc2274 35 }
AlexVC97 2:e6a095fc2274 36
AlexVC97 2:e6a095fc2274 37 printf("Read from SD card: %s\r\n", words);
AlexVC97 2:e6a095fc2274 38
AlexVC97 2:e6a095fc2274 39 fclose(fp1);
AlexVC97 2:e6a095fc2274 40 */
AlexVC97 2:e6a095fc2274 41
AlexVC97 2:e6a095fc2274 42 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}]}";
AlexVC97 0:b3949bab2aee 43 //const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
AlexVC97 2:e6a095fc2274 44
AlexVC97 2:e6a095fc2274 45 // parse the previous string and fill the object demo
AlexVC97 0:b3949bab2aee 46 parse(demo, json);
AlexVC97 2:e6a095fc2274 47
AlexVC97 0:b3949bab2aee 48 printf(">>> testing if it works properly \r\n");
AlexVC97 2:e6a095fc2274 49
AlexVC97 2:e6a095fc2274 50 std::vector <Mission> missions; // declares an object of the class std::vector
AlexVC97 2:e6a095fc2274 51
AlexVC97 2:e6a095fc2274 52 for(std::size_t i = 0; i < demo["missions"].size(); i++)
AlexVC97 2:e6a095fc2274 53 {
AlexVC97 2:e6a095fc2274 54 Mission mission; // declares a local variable of the class Mission
AlexVC97 2:e6a095fc2274 55 mission.from_json(demo["missions"][i]);
AlexVC97 2:e6a095fc2274 56 missions.push_back(mission);
AlexVC97 2:e6a095fc2274 57 }
AlexVC97 2:e6a095fc2274 58
AlexVC97 2:e6a095fc2274 59 cout << "Missions: " << endl;
AlexVC97 2:e6a095fc2274 60
AlexVC97 2:e6a095fc2274 61 for(std::size_t i = 0; i < missions.size(); i++)
AlexVC97 2:e6a095fc2274 62 {
AlexVC97 2:e6a095fc2274 63 printf("Type: %s\r\n", missions[i].get_type().c_str());
AlexVC97 2:e6a095fc2274 64 printf("Id: %d\r\n", missions[i].get_id());
AlexVC97 2:e6a095fc2274 65 printf("Message: %s\r\n", missions[i].get_message().c_str());
AlexVC97 2:e6a095fc2274 66 printf("Hint: %s\r\n", missions[i].get_hint().c_str());
AlexVC97 2:e6a095fc2274 67 printf("Deadline: %s\r\n", missions[i].get_deadline() ? "true" : "false");
AlexVC97 2:e6a095fc2274 68 printf("DeadlineTime: %d\r\n", missions[i].get_deadlineTime());
AlexVC97 2:e6a095fc2274 69 printf("Latitude: %f\r\n", missions[i].get_latitude());
AlexVC97 2:e6a095fc2274 70 printf("Longitude: %f\r\n", missions[i].get_longitude());
AlexVC97 2:e6a095fc2274 71 printf("Radius: %d\r\n", missions[i].get_radius());
AlexVC97 2:e6a095fc2274 72 }
AlexVC97 0:b3949bab2aee 73
AlexVC97 2:e6a095fc2274 74 /*
AlexVC97 2:e6a095fc2274 75 for(std::size_t i = 0; i < missions.size(); i++)
AlexVC97 2:e6a095fc2274 76 {
AlexVC97 2:e6a095fc2274 77 cout << missions[i].to_string();
AlexVC97 2:e6a095fc2274 78 }
AlexVC97 2:e6a095fc2274 79 */
AlexVC97 2:e6a095fc2274 80
AlexVC97 2:e6a095fc2274 81 /*
AlexVC97 0:b3949bab2aee 82 std::string game_mode;
AlexVC97 0:b3949bab2aee 83 int game_mode_id;
AlexVC97 0:b3949bab2aee 84 std::string my_ver;
AlexVC97 0:b3949bab2aee 85 std::string creation_date;
AlexVC97 2:e6a095fc2274 86 */
AlexVC97 2:e6a095fc2274 87
AlexVC97 2:e6a095fc2274 88 /*
AlexVC97 0:b3949bab2aee 89 game_mode = demo["gameMode"].get<std::string>();
AlexVC97 0:b3949bab2aee 90 game_mode_id = demo["gameModeId"].get<int>();
AlexVC97 0:b3949bab2aee 91 my_ver = demo["version"].get<std::string>();
AlexVC97 0:b3949bab2aee 92 creation_date = demo["creationDate"].get<std::string>();
AlexVC97 2:e6a095fc2274 93 */
AlexVC97 2:e6a095fc2274 94
AlexVC97 2:e6a095fc2274 95 /*for(int i = 0; i < 2; i++)
AlexVC97 2:e6a095fc2274 96 {
AlexVC97 2:e6a095fc2274 97 type = demo["missions"][i]["type"].get<std::string>();
AlexVC97 2:e6a095fc2274 98 type = demo["missions"][i]["id"].get<int>();
AlexVC97 2:e6a095fc2274 99 type = demo["missions"][i]["message"].get<std::string>();
AlexVC97 2:e6a095fc2274 100 type = demo["missions"][i]["hint"].get<std::string>();
AlexVC97 2:e6a095fc2274 101 type = demo["missions"][i]["deadline"].get<bool>();
AlexVC97 2:e6a095fc2274 102 type = demo["missions"][i]["deadlineTime"].get<int>();
AlexVC97 2:e6a095fc2274 103 type = demo["missions"][i]["altitude"].get<double>();
AlexVC97 2:e6a095fc2274 104 type = demo["missions"][i]["longitude"].get<double>();
AlexVC97 2:e6a095fc2274 105 type = demo["missions"][i]["radius"].get<int>();
AlexVC97 2:e6a095fc2274 106 }
AlexVC97 2:e6a095fc2274 107 */
AlexVC97 2:e6a095fc2274 108
AlexVC97 2:e6a095fc2274 109 /*
AlexVC97 0:b3949bab2aee 110 printf("game_mode: %s\r\n", game_mode.c_str());
AlexVC97 0:b3949bab2aee 111 printf("game_mode_id: %d\r\n", game_mode_id);
AlexVC97 0:b3949bab2aee 112 printf("my_ver: %s\r\n", my_ver.c_str());
AlexVC97 0:b3949bab2aee 113 printf("creation_date: %s\r\n", creation_date.c_str());
AlexVC97 2:e6a095fc2274 114 printf("lat: %f\r\n", latitude);
AlexVC97 2:e6a095fc2274 115 printf("long: %f\r\n", longitude);
AlexVC97 2:e6a095fc2274 116 printf("rad: %d\r\n", rad);
AlexVC97 2:e6a095fc2274 117 */
AlexVC97 2:e6a095fc2274 118 }