|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
| 3 | +#include <string.h> |
3 | 4 |
|
4 | 5 | // Include cJSON Library |
5 | 6 | #include "inc/cJSON.h" |
6 | 7 |
|
| 8 | +// - Testing Constant Variables - |
| 9 | + |
| 10 | +// - Testing Function Prototypes - |
| 11 | +cJSON_String_t readFileContents(const char* fileName); |
| 12 | +void printBtcValue(const char* JSONstr); |
| 13 | + |
7 | 14 | int main() |
8 | 15 | { |
9 | | - printf("cJSON Library Test Script\n"); |
| 16 | + printf("cJSON library test script\n\n"); |
| 17 | + |
| 18 | + // Get test type |
| 19 | + printf(" --- Enter Test Type ---\n"); |
| 20 | + |
| 21 | + //printf("\"a\": Parse JSON string\n"); |
| 22 | + //printf("\"A\": Parse JSON file\n"); |
| 23 | + printf("\"b\": Bitcoin value from coinbase API's JSON string\n"); //https://api.coinbase.com/v2/prices/spot?currency=USD |
| 24 | + //printf("\"B\": Bitcoin value from coinbase API's JSON string (file)\n"); //https://api.coinbase.com/v2/prices/spot?currency=USD |
| 25 | + |
| 26 | + // Get test type |
| 27 | + char testType = getchar(); |
| 28 | + // Discard remaining characters in input |
| 29 | + while (getchar() != '\n' && getchar() != EOF); |
| 30 | + |
| 31 | + switch(testType) |
| 32 | + { |
| 33 | + case 'a': |
| 34 | + printf("Test not implemented!\n"); |
| 35 | + break; |
| 36 | + case 'A': |
| 37 | + printf("Test not implemented!\n"); |
| 38 | + break; |
| 39 | + case 'b':; |
| 40 | + char JSON_BtcStr[100]; |
| 41 | + |
| 42 | + // Get string |
| 43 | + printf("Enter string from bitcoin API:"); |
| 44 | + fgets(JSON_BtcStr, 100, stdin); |
| 45 | + |
| 46 | + printBtcValue(JSON_BtcStr); |
| 47 | + break; |
| 48 | + case 'B': |
| 49 | + printf("Test not implemented!\n"); |
| 50 | + break; |
| 51 | + default: |
| 52 | + printf("Invalid test type \"%c\"\n", testType); |
| 53 | + break; |
| 54 | + } |
| 55 | + |
10 | 56 | return 0; |
11 | 57 | } |
| 58 | + |
| 59 | +// - Testing Function Implementations - |
| 60 | +cJSON_String_t readFileContents(const char* fileName) |
| 61 | +{ |
| 62 | + FILE *fPtr = fopen(fileName, "r"); |
| 63 | + if (!fPtr) { |
| 64 | + perror("Failed to open file"); |
| 65 | + return NULL; |
| 66 | + } |
| 67 | + |
| 68 | + // Seek to the end of the file to get the file size |
| 69 | + fseek(fPtr, 0, SEEK_END); |
| 70 | + long fileSize = ftell(fPtr); |
| 71 | + rewind(fPtr); |
| 72 | + |
| 73 | + // Allocate memory for the file content plus the null terminator |
| 74 | + cJSON_String_t buffer = (cJSON_String_t)malloc((fileSize + 1) * sizeof(char)); |
| 75 | + if (!buffer) { |
| 76 | + perror("Failed to allocate memory"); |
| 77 | + fclose(fPtr); |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + |
| 81 | + // Read the file contents into the buffer |
| 82 | + size_t bytesRead = fread(buffer, sizeof(char), fileSize, fPtr); |
| 83 | + buffer[bytesRead] = '\0'; // Null-terminate the string |
| 84 | + |
| 85 | + // Clean up and return the buffer |
| 86 | + fclose(fPtr); |
| 87 | + return buffer; |
| 88 | +} |
| 89 | + |
| 90 | +void printBtcValue(const char* JSONstr) |
| 91 | +{ |
| 92 | + // Create empty cJSON generic container |
| 93 | + cJSON_Generic_t Base_cJSON_Tree = {0}; |
| 94 | + |
| 95 | + // Parse string into variable |
| 96 | + cJSON_Result_t funcResult = cJSON_parseStr(&Base_cJSON_Tree, JSONstr); |
| 97 | + if (funcResult != cJSON_Ok) |
| 98 | + { |
| 99 | + printf("Parse failed with error code %d!\n", (int)funcResult); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + printf("\nSuccessfully parsed string!\n"); |
| 104 | + |
| 105 | + // Write base cJSON tree to relative cJSON tree |
| 106 | + cJSON_Generic_t Rel_cJSON_Tree = Base_cJSON_Tree; |
| 107 | + |
| 108 | + // Temporary dictionary object |
| 109 | + cJSON_Dict_t dictObj = {0}; |
| 110 | + |
| 111 | + funcResult = cJSON_tryGetDict(Rel_cJSON_Tree, &dictObj); |
| 112 | + if (funcResult != cJSON_Ok) |
| 113 | + { |
| 114 | + printf("Base is not a dictionary!\n"); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + // Get generic object at "data" key |
| 119 | + for (int i = 0; i < dictObj.length; i++) |
| 120 | + { |
| 121 | + if (!strcmp(dictObj.keyData[i], "data")) |
| 122 | + { |
| 123 | + Rel_cJSON_Tree = dictObj.valueData[i]; |
| 124 | + break; |
| 125 | + } |
| 126 | + else if (i == (dictObj.length - 1)) |
| 127 | + { |
| 128 | + printf("Missing \"data\" key in dictionary!\n"); |
| 129 | + return; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + funcResult = cJSON_tryGetDict(Rel_cJSON_Tree, &dictObj); |
| 134 | + if (funcResult != cJSON_Ok) |
| 135 | + { |
| 136 | + printf("Generic object at \"data\" key is not a dictionary!\n"); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Get generic object at "data" -> "amount" key |
| 141 | + for (int i = 0; i < dictObj.length; i++) |
| 142 | + { |
| 143 | + if (!strcmp(dictObj.keyData[i], "amount")) |
| 144 | + { |
| 145 | + Rel_cJSON_Tree = dictObj.valueData[i]; |
| 146 | + break; |
| 147 | + } |
| 148 | + else if (i == (dictObj.length - 1)) |
| 149 | + { |
| 150 | + printf("Missing \"amount\" key in dictionary!\n"); |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + cJSON_String_t btcStr; |
| 156 | + |
| 157 | + // Try get bitcoin amount string |
| 158 | + funcResult = cJSON_tryGetString(Rel_cJSON_Tree, &btcStr); |
| 159 | + if (funcResult != cJSON_Ok) |
| 160 | + { |
| 161 | + printf("Generic object at \"amount\" key is not a string!\n"); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Print bitcoin amount |
| 166 | + printf("Bicoin value in USD: $%s\n", btcStr); |
| 167 | + |
| 168 | + cJSON_delGenObj(Base_cJSON_Tree); |
| 169 | +} |
0 commit comments