int json_parse_dump(char * js, jsmntok_t * t, void * ptr) { char s[JSON_STR_LEN_MAX + 1]; if (t->start == JSMN_NULL || t->end == JSMN_NULL) return -JSON_ERR_INVALID_TOKEN; if (ptr == NULL) return -JSON_ERR_NULL_POINTER; switch (t->type) { case JSMN_OBJECT: return json_walk_object(stdout, js, t, 1); case JSMN_ARRAY: return json_walk_array(stdout, js, t, 1); case JSMN_STRING: json_token_tostr(s, JSON_STR_LEN_MAX, js, t); fprintf(stdout, "\"%s\"", s); return 1; case JSMN_PRIMITIVE: json_token_tostr(s, JSON_STR_LEN_MAX, js, t); fprintf(stdout, "%s", s); return 1; } return -JSON_ERR_INVALID_TYPE; }
int json_walk_node(char * js, jsmntok_t *t, int lvl) { if (t->type == JSMN_OBJECT) { return json_walk_object(js, t, lvl); } if (t->type == JSMN_ARRAY) { return json_walk_array(js, t, lvl); } if (t->type == JSMN_STRING) { // printf("STRING: \"%s\"\n", json_token_tostr(js, t)); printf("\"%s\"", json_token_tostr(js, t)); return 1; } if (t->type == JSMN_PRIMITIVE) { // printf("PRIMITIVE: \"%s\"\n", json_token_tostr(js, t)); printf("%s", json_token_tostr(js, t)); return 1; } printf("Invalid response:"); return -1; }
int json_walk_node(FILE * f, char * js, jsmntok_t *t, int lvl) { char s[JSON_STR_LEN_MAX + 1]; if(t->start == JSMN_NULL || t->end == JSMN_NULL) { DCC_LOG(LOG_ERROR, "parameter invalid!"); return -1; } switch (t->type) { case JSMN_OBJECT: return json_walk_object(f, js, t, lvl); case JSMN_ARRAY: return json_walk_array(f, js, t, lvl); case JSMN_STRING: json_token_tostr(s, JSON_STR_LEN_MAX, js, t); fprintf(f, "\"%s\"", s); return 1; case JSMN_PRIMITIVE: json_token_tostr(s, JSON_STR_LEN_MAX, js, t); fprintf(f, "%s", s); return 1; } DCC_LOG(LOG_ERROR, "Invalid type"); return -1; }
mqtt_data_t json_deserialize_command(jsmntok_t *tokens, char* payload, mqtt_data_t data_t) { char* temp_name = json_token_tostr(payload, &tokens[9]); char* name = BS_MEM_ALLOC(strlen(temp_name) + 1); memcpy(name, temp_name, strlen(temp_name) + 1); //data_t->ticketId = uid; int max = (&tokens[11])->size; printf("Command: %s (%d params)\r\n", name, max / 2); int index = 0; char** keys = BS_MEM_ALLOC(max / 2); int key_index = 0; for(index = 0; index < max; index+=2, key_index++) { char* temp_pname = json_token_tostr(payload, &tokens[12 + index]); keys[key_index] = BS_MEM_ALLOC(strlen(temp_pname) + 1); memcpy(keys[key_index], temp_pname, strlen(temp_pname) + 1); char* temp = json_token_tostr(payload, &tokens[12 + index + 1]); char* param_value = BS_MEM_ALLOC(strlen(temp) + 1); memcpy(param_value, temp, strlen(temp) + 1); printf("%s: %s\r\n", keys[key_index], param_value); //x.fieldname = param_name; bsd_data_t x; if(strcmp(param_value, "true") == 0) { printf("param bool @ true\r\n"); x.kind = BSD_BOOL; x.content.boolean = true; } else if(strcmp(param_value, "false") == 0) { printf("param bool @ false\r\n"); x.kind = BSD_BOOL; x.content.boolean = false; } else { // Not supported // x.content.string.length = strlen(param_value); // x.content.string.data = param_value; } data_t.values[key_index] = BS_MEM_ALLOC(sizeof(x)); memcpy(data_t.values[key_index], &x, sizeof(x)); } data_t.path = name; data_t.nbofvalues = max / 2; data_t.keys = keys; return data_t; }
int json_to_token_list(char *json_string, token_list_t *token_list){ int found_pairs = 0; jsmntok_t *tokens = token_list->tokens; jsmn_parser parser; parse_state state = KEY; jsmn_init(&parser); /* LALEE: Run the Jsmn lexical scanner against the JSON String. We're memory-constrained, so no elaborate error-checking here. */ int parse_status = jsmn_parse(&parser, json_string, strlen(json_string), tokens, token_list->length); if (parse_status != JSMN_SUCCESS) { return parse_status; // TODO: Add a mechanism to log a message . . . } /* LALEE: Very minimalist Parser to refine the Scanned Tokens into JSMN_KEYs and JSMN_VALUEs. */ /* This will allow us to retrieve "Key/Value Pairs" via json_get_value() */ /* Token 0 is a JSMN_OBJECT representing the entire JSON structure. */ /* Since we expect Key/Value Pairs in incoming JSON, the following two-state parser (starting at Token 1, not 0) is sufficient. */ for (int i = 1; i < parser.toknext ; i++) { jsmntok_t *t = &tokens[i]; switch (state) { case KEY: /* LALEE: Upgrade this Token into a JSMN_KEY */ t->type = JSMN_KEY; t->size = t->end - t->start; t->string = json_token_tostr(json_string, t); state = VALUE; break; case VALUE: /* LALEE: Upgrade this Token into a JSMN_VALUE */ t->type = JSMN_VALUE; t->size = t->end - t->start; t->string = json_token_tostr(json_string, t); found_pairs++; state = KEY; break; } } return token_list->count = found_pairs; }
inline int add_value_from_token(json_t *obj, char* jsonString, jsmntok_t* token) { if ((obj != NULL) && (jsonString != NULL) && (token != NULL)) { strbuffer_t *tokenStr = json_token_tostr(jsonString, token); obj->value = (char*) json_malloc((tokenStr->length+1) * sizeof(char)); memset(obj->value, 0, (tokenStr->length+1)); strncpy(obj->value, tokenStr->value, tokenStr->length); strbuffer_destroy(tokenStr); return 0; } return -1; }
json_error_t* parsingError( json_error_code_t errCode, char* errMsg, jsmntok_t *token, char* jsonString ) { json_error_t* err = (json_error_t*) json_malloc(sizeof(json_error_t)); err->errorMsg = (strbuffer_t*) json_malloc(sizeof(json_error_t)); strbuffer_init(err->errorMsg); err->errorCode = errCode; strbuffer_append(err->errorMsg, errMsg); if( token != NULL && jsonString != NULL) { strbuffer_t *tokenText = json_token_tostr(jsonString, token); strbuffer_append(err->errorMsg, " Error was in: "); strbuffer_append(err->errorMsg, tokenText->value); strbuffer_destroy(tokenText); } return err; }
int main() { int rc; char token[86] = "a"; char query[512]; struct songitem songarray[300]; // 10 = count in query below rc = getToken(token); check(rc == OK, "Could not get token"); /* printf("Token: %s\n", token); */ rc = snprintf(query, sizeof(query), "https://api.vk.com/method/audio.get?count=300&access_token=%s", token); check( ((rc > 0) && (rc < (int)sizeof(query))), "Could not create query"); char *js = jsonFetch(query); jsmntok_t *tokens = json_tokenise(js); typedef enum { START, KEY, STOP, RESPONSE, ARRAY, SONG, TITLE, URL} parse_state; // state is the current state of parser parse_state state = START; // stack is the state we return to when reaching end of an object parse_state stack = STOP; // counters to keep track of how far through parsing we are size_t object_tokens = 0; size_t songs = 0; size_t song_tokens = 0; size_t song_id = 0; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; // should never reach uninitialized tokens check((t->start != -1 && t->end != -1), "Have reached uninitialized tokens"); if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: if (t->type != JSMN_OBJECT) sentinel("Invalid response: root element must be an object."); state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; if (object_tokens % 2 != 0) sentinel("Invalid response: object must have even number of children."); break; case KEY: object_tokens--; if (t->type != JSMN_STRING) sentinel("Invalid response: object keys must be strings."); if (json_token_streq(js, t, "response")) state = RESPONSE; if (object_tokens == 0) state = STOP; break; case RESPONSE: /* printf("songs t->type: %d\n", t->type); */ if (t->type != JSMN_ARRAY) sentinel("Unknown RESPONSE value, expected array of songs."); songs = t->size; state = ARRAY; stack = ARRAY; if (songs == 0) state = STOP; break; case ARRAY: songs--; /* printf("Q_SONGS = %ld\n", songs); */ song_tokens = t->size; state = SONG; /* if (song_tokens != 0) */ /* printf("%ld - song tokens\n", song_tokens); */ if (song_tokens == 0) state = STOP; if (songs == 0) stack = STOP; break; case SONG: case TITLE: case URL: song_tokens--; // Keys are odd numbered tokens within the object if (song_tokens % 2 == 1) { if (t->type == JSMN_STRING && json_token_streq(js, t, "title")) { state = TITLE; } else if (t->type == JSMN_STRING && json_token_streq(js, t, "url")) { state = URL; } } // Values in the TITLE state else if (state == TITLE) { if (t->type != JSMN_STRING) sentinel("Invalid title name."); char *str = json_token_tostr(js, t); /* printf("%s\n", str); */ songarray[song_id].id = song_id+1; snprintf(songarray[song_id].title, sizeof(songarray[song_id].title), "%s", str); state = SONG; } else if (state == URL) { if (t->type != JSMN_STRING) sentinel("Invalid url name."); // token to string char *p_url = json_token_tostr(js, t); // concatanate rubish after 'mp3' char *pch = strstr(p_url, ".mp3"); if (!pch) { sentinel("The links format does not seem to be correct, please try again"); } pch += 4; *pch = '\0'; // stupid procedure to remove '\' while ( (pch = strstr(p_url, "\\")) ) { int idx = (pch-p_url); memmove(&p_url[idx], &p_url[idx+1], strlen(p_url) - (idx)); } /* printf("%s\n", p_url); */ snprintf(songarray[song_id].url, sizeof(songarray[song_id].url), "%s", p_url); /* play_url(p_url); */ song_id++; state = SONG; } if (song_tokens == 0) state = stack; break; case STOP: printf("STOP\n"); break; default: sentinel("Invalid state %u", state); } } // end of for loop int i; pthread_t thread1; int ret1; for (i = 0; i < (int)sizeof(songarray)/(int)sizeof(songitem); i++) { printf("%d - %s\n", songarray[i].id, songarray[i].title); ret1 = pthread_create(&thread1, NULL, play_url2, (void *) songarray[i].url); pthread_join(thread1, NULL); sleep(1); /* play_url(songarray[i].url); */ } cleanup(); return 0; error: return 1; }
int main(void) { //char *js = json_fetch(URL); /*char js[] = "{" "\"following_url\": \"https://api.github.com/users/alisdair/following{/other_user}\"," "\"name\": \"Alisdair McDiarmid\"," "\"location\": \"Glasgow\"," "\"hireable\": true," "\"public_repos\": 49," "\"created_at\": \"2009-03-31T12:36:07Z\"," "\"updated_at\": \"2015-04-15T09:39:22Z\"" "}";*/ char *js = json_fetch_file("test_json.txt"); char token[64]; char name[64]; char location[64]; char hireable[64]; char following_url[64]; int public_repos; double public_gists; char *str; jsmntok_t *tokens = json_tokenise(js); typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state; parse_state state = START; size_t object_tokens = 0; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; break; case KEY: object_tokens--; state = SKIP; for (size_t i = 0; i < sizeof(KEYS)/sizeof(char *); i++) { if (json_token_streq(js, t, KEYS[i])) { printf("%s: ", KEYS[i]); strcpy(token, KEYS[i]); state = PRINT; break; } } break; case SKIP: object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case PRINT: str = json_token_tostr(js, t); puts(str); if(strcmp("name", token) == 0) // token = string { strcpy(name, str); } if(strcmp("location", token) == 0) // token = string { strcpy(location, str); } if(strcmp("public_repos", token) == 0) // token = int { public_repos = atoi(str); } if(strcmp("hireable", token) == 0) // token = string { strcpy(hireable, str); } if(strcmp("following_url", token) == 0) // token = string { strcpy(following_url, str); } if(strcmp("public_gists", token) == 0) // token = double { public_gists = atof(str); } object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case STOP: break; default: break; } } return 0; }
int json_walk_object(char * js, jsmntok_t *t, int lvl) { int len; int n; int i; int j; n = t->size; // printf("OBJECT: (size=%d)\n", n); printf("{\n"); if (n == 0) return 0; if (n % 2 != 0) { printf("Invalid response: object must have even number of children."); return -1; } lvl++; t++; len = 1; for (i = 0; i < n; i += 2) { int r; if (t->type != JSMN_STRING) { printf("Invalid response: object keys must be strings."); return -1; } if (i != 0) printf(",\n"); for (j = 0; j < lvl; ++j) printf(" "); printf("\"%s\": ", json_token_tostr(js, t)); t++; len++; r = json_walk_node(js, t, lvl); if (r < 0) return -1; t += r; len += r; } printf("\n"); lvl--; for (j = 0; j < lvl; ++j) printf(" "); printf("}"); return len; }
int parse_json_request(Reader& reader, const char* const keys[], const jsmntype_t types[], unsigned count) { char* js = reader.fetch_as_string(); if (!js) return -1; jsmntok_t *tokens = json_tokenise(js); enum parse_state { START, KEY, VALUE, SKIP, STOP }; parse_state state = START; jsmntype_t expected_type = JSMN_OBJECT; int result = 0; int key = -1; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size * 2; switch (state) { case START: state = KEY; break; case KEY: state = VALUE; key = -1; for (size_t i = 0; i < count; i++) { if (json_token_streq(js, t, keys[i])) { expected_type = types[i]; if (parsed_key(i)) { key = i; JSON_DEBUG( ( "key: %s %d %d\n", keys[i], i, (int)expected_type ) ); } } } if (key==-1) { JSON_DEBUG( ( "unknown key: %s\n", json_token_tostr(js, t) ) ); result = -1; } break; case VALUE: if (key!=-1) { if (t->type != expected_type) { result = -1; JSON_DEBUG( ( "type mismatch\n" ) ); } else { char *str = json_token_tostr(js, t); if (!parsed_value(key, t, str)) result = -1; } } state = KEY; break; case STOP: // Just consume the tokens break; default: result = -1; } } free(js); return result; }
int read_leap_dump(void) { // printf("%s\n", "Reaadding???"); lock(); int count = 1; char* js = 0; long length; FILE * f = fopen ("../src/leap_data.json", "rb"); if (f) { // printf("%s\n", "File Found"); fseek (f, 0, SEEK_END); length = ftell (f); fseek (f, 0, SEEK_SET); js = malloc (length); if (js) { // printf("%s\n", "File non trivial"); fread (js, 1, length, f); *(js + length) = '\0'; } fclose (f); } jsmntok_t *tokens = json_tokenise(js); /* The GitHub user API response is a single object. States required to * parse this are simple: start of the object, keys, values we want to * print, values we want to skip, and then a marker state for the end. */ typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state; parse_state state = START; size_t object_tokens = 0; size_t i; size_t j; for (i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; // Should never reach uninitialized tokens log_assert(t->start != -1 && t->end != -1); if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: if (t->type != JSMN_OBJECT) log_die("Invalid response: root element must be an object."); state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; if (object_tokens % 2 != 0) log_die("Invalid response: object must have even number of children."); break; case KEY: object_tokens--; if (t->type != JSMN_STRING) log_die("Invalid response: object keys must be strings."); state = SKIP; size_t i; for (i = 0; i < sizeof(KEYS)/sizeof(char *); i++) { if (json_token_streq(js, t, KEYS[i])) { printf("%s: ", KEYS[i]); state = PRINT; break; } } break; case SKIP: if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE) log_die("Invalid response: object values must be strings or primitives."); object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case PRINT: if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE) log_die("Invalid response: object values must be strings or primitives."); char *str = json_token_tostr(js, t); puts(str); char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,":"); while (pch != NULL) { printf ("%s\n",pch); if (count == 1) { py1 = atof(pch); } if (count == 2) { pz1 = atof(pch); } if (count == 3){ px1 = atof(pch); } pch = strtok (NULL, " "); } object_tokens--; state = KEY; count++; if (object_tokens == 0) state = STOP; break; case STOP: // Just consume the tokens break; default: log_die("Invalid state %u", state); } } unlock(); return 0; }
int json_walk_object(FILE * f, char * js, jsmntok_t *t, int lvl) { char s[JSON_STR_LEN_MAX + 1]; int len; int n; int i; int j; if (t->type != JSMN_OBJECT) { DCC_LOG(LOG_ERROR, "invalid type!"); return -1; } n = t->size; printf("{\n"); if (n == 0) return 0; if (n % 2 != 0) { DCC_LOG(LOG_ERROR, "object must have even number of children."); return -1; } lvl++; t++; len = 1; for (i = 0; i < n; i += 2) { int r; if (t->type != JSMN_STRING) { DCC_LOG(LOG_ERROR, "object keys must be strings."); return -1; } if (i != 0) printf(",\n"); for (j = 0; j < lvl; ++j) printf(" "); json_token_tostr(s, JSON_STR_LEN_MAX, js, t); printf("\"%s\": ", s); t++; len++; r = json_walk_node(f, js, t, lvl); if (r < 0) return -1; t += r; len += r; } printf("\n"); lvl--; for (j = 0; j < lvl; ++j) printf(" "); printf("}"); return len; }