int main(int argc, const char * argv[]) { const int JSON_DEPTH = 4; // Limitation of JSON depth char json[] = "[-13453453, 0, 1, 123, 45345, -94534555]"; // Numbers to sum uint8_t parser_memory[jsonlite_parser_estimate_size(JSON_DEPTH)]; // Parser memory jsonlite_parser_callbacks cbs = jsonlite_default_callbacks; // Init callbacks with default values cbs.number_found = &number_callback; // Assign new callback function jsonlite_parser p = jsonlite_parser_init(parser_memory, sizeof(parser_memory), jsonlite_null_buffer); // Init parser jsonlite_parser_set_callback(p, &cbs); // Set callback function(s) jsonlite_parser_tokenize(p, json, sizeof(json)); // Tokenize/find numbers printf("Total sum: %lld\n", sum); return 0; }
int M2XStreamClient::readLocation(location_read_callback callback, void* context) { const int BUF_LEN = 40; char buf[BUF_LEN]; int length = readContentLength(); if (length < 0) { close(); return length; } int index = skipHttpHeader(); if (index != E_OK) { close(); return index; } index = 0; location_parsing_context_state state; state.state = state.index = 0; state.callback = callback; state.context = context; jsonlite_parser_callbacks cbs = jsonlite_default_callbacks; cbs.key_found = on_location_key_found; cbs.string_found = on_location_string_found; cbs.context.client_state = &state; jsonlite_parser p = jsonlite_parser_init(jsonlite_parser_estimate_size(5)); jsonlite_parser_set_callback(p, &cbs); jsonlite_result result = jsonlite_result_unknown; while (index < length) { int i = 0; #ifdef DEBUG printf("Received Data: "); #endif while ((i < BUF_LEN) && _client->available()) { buf[i++] = _client->read(); #ifdef DEBUG printf("%c", buf[i - 1]); #endif } #ifdef DEBUG printf("\n"); #endif if ((!_client->connected()) && (!_client->available()) && ((index + i) < length)) { jsonlite_parser_release(p); close(); return E_NOCONNECTION; } result = jsonlite_parser_tokenize(p, buf, i); if ((result != jsonlite_result_ok) && (result != jsonlite_result_end_of_stream)) { jsonlite_parser_release(p); close(); return E_JSON_INVALID; } index += i; } jsonlite_parser_release(p); close(); return (result == jsonlite_result_ok) ? (E_OK) : (E_JSON_INVALID); }