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