SEXP R_validate(SEXP x) { /* get data from R */ const char* json = translateCharUTF8(asChar(x)); /* test for BOM */ if(json[0] == '\xEF' && json[1] == '\xBB' && json[2] == '\xBF'){ SEXP output = PROTECT(duplicate(ScalarLogical(0))); SEXP msg = PROTECT(Rf_mkString("JSON string contains UTF8 byte-order-mark.")); setAttrib(output, install("err"), msg); UNPROTECT(2); return(output); } /* allocate a parser */ yajl_handle hand = yajl_alloc(NULL, NULL, NULL); /* parser options */ //yajl_config(hand, yajl_dont_validate_strings, 1); /* go parse */ const size_t rd = strlen(json); yajl_status stat = yajl_parse(hand, (const unsigned char*) json, rd); if(stat == yajl_status_ok) { stat = yajl_complete_parse(hand); } SEXP output = PROTECT(duplicate(ScalarLogical(!stat))); //error message if (stat != yajl_status_ok) { unsigned char* str = yajl_get_error(hand, 1, (const unsigned char*) json, rd); SEXP errstr = PROTECT(mkString((const char *) str)); SEXP offset = PROTECT(ScalarInteger(yajl_get_bytes_consumed(hand))); yajl_free_error(hand, str); setAttrib(output, install("offset"), offset); setAttrib(output, install("err"), errstr); UNPROTECT(2); } /* return boolean vec (0 means no errors, means is valid) */ yajl_free(hand); UNPROTECT(1); return output; }
tyn_document_t* tyn_reader_fetch_document(tyn_reader_t *reader) { tyn_reader_context_t *context = (tyn_reader_context_t *)reader->context; context->document = reader->document; if(context->document->num_fields > 0) { //free the allocated memory in this document for(int i =0; i < context->document->num_fields; i++) { free(*(context->document->fields + i)); free(*(context->document->field_names + i)); } context->document->num_fields = 0; } for (;;) { //if(yajl_get_bytes_consumed(context->handle) == 0 || yajl_get_bytes_consumed(context->handle) >= context->buffer_size) { if(context->parse_state == 0) { context->bytes_consumed = 0; context->bytes_read = fread(context->buffer, 1, context->buffer_size, context->json_stream); if (context->bytes_read == 0) { //tyn_log(stderr, "Error reading json stream!"); return NULL; } } else { context->parse_state = 0; } /* read file data, now pass to parser */ yajl_status stat = yajl_parse(context->handle, context->buffer + context->bytes_consumed, context->bytes_read - context->bytes_consumed); if(stat == yajl_status_client_canceled) { context->bytes_consumed += yajl_get_bytes_consumed(context->handle); return reader->document; } if(stat == yajl_status_ok) { //need to read new block } if (stat != yajl_status_ok) { tyn_log(stderr,"%s\n", yajl_get_error(context->handle, 1, context->buffer + context->bytes_consumed, context->bytes_read - context->bytes_consumed)); return NULL; } } }
static long chf_parse(dbChannel *chan, const char **pjson) { parseContext parser = { chan, NULL, 0 }; yajl_handle yh = yajl_alloc(&chf_callbacks, &chf_config, &chf_alloc, &parser); const char *json = *pjson; size_t jlen = strlen(json); yajl_status ys; long status; if (!yh) return S_db_noMemory; ys = yajl_parse(yh, (const unsigned char *) json, (unsigned int) jlen); if (ys == yajl_status_insufficient_data) ys = yajl_parse_complete(yh); switch (ys) { case yajl_status_ok: status = 0; *pjson += yajl_get_bytes_consumed(yh); break; case yajl_status_error: { unsigned char *err; err = yajl_get_error(yh, 1, (const unsigned char *) json, (unsigned int) jlen); printf("dbChannelCreate: %s\n", err); yajl_free_error(yh, err); } /* fall through */ default: status = S_db_notFound; } if (parser.filter) { assert(status); parser.filter->plug->fif->parse_abort(parser.filter); freeListFree(chFilterFreeList, parser.filter); } yajl_free(yh); return status; }
orderly_json * orderly_read_json(orderly_alloc_funcs * alloc, const char * jsonText, unsigned int * len) { static yajl_callbacks callbacks = { o_json_parse_null, o_json_parse_boolean, o_json_parse_integer, o_json_parse_double, NULL, o_json_parse_string, o_json_parse_start_map, o_json_parse_map_key, o_json_parse_end_map, o_json_parse_start_array, o_json_parse_end_array }; yajl_handle hand; yajl_status stat; /* allow comments! */ yajl_parser_config cfg = { 1, 1 }; o_json_parse_context pc; orderly_json * j = NULL; memset((void *) &pc, 0, sizeof(pc)); pc.alloc = alloc; /* allocate a parser */ hand = yajl_alloc(&callbacks, &cfg, (const yajl_alloc_funcs *) alloc, (void *) &pc); /* read file data, pass to parser */ stat = yajl_parse(hand, (const unsigned char *) jsonText, *len); *len = yajl_get_bytes_consumed(hand); if (stat == yajl_status_insufficient_data) { stat = yajl_parse_complete(hand); } if (stat != yajl_status_ok) { /* unsigned char * str = yajl_get_error(hand, 1, (const unsigned char *) jsonText, *len); */ /* fprintf(stderr, (const char *) str); */ /* yajl_free_error(hand, str); */ } else if (!orderly_ps_length(pc.nodeStack)) { /* XXX: ERROR! */ } else { /* we're ok! */ j = orderly_ps_current(pc.nodeStack); } yajl_free(hand); orderly_ps_free(alloc, pc.nodeStack); orderly_ps_free(alloc, pc.keyStack); return j; }
Validator* parse_schema_n(char const *str, size_t len, UriResolver *uri_resolver, char const *root_scope, JschemaErrorFunc error_func, void *error_ctxt) { //JsonSchemaParserTrace(stdout, ">>> "); void *parser = JsonSchemaParserAlloc(malloc); YajlContext yajl_context = { .parser = parser, .parser_ctxt = { .validator = NULL, .error = SEC_OK, }, }; const bool allow_comments = true; #if YAJL_VERSION < 20000 yajl_parser_config yajl_opts = { allow_comments, 0, }; yajl_handle yh = yajl_alloc(&callbacks, &yajl_opts, NULL, &yajl_context); #else yajl_handle yh = yajl_alloc(&callbacks, NULL, &yajl_context); yajl_config(yh, yajl_allow_comments, allow_comments ? 1 : 0); yajl_config(yh, yajl_dont_validate_strings, 1); #endif // YAJL_VERSION if (!yh) { JsonSchemaParserFree(parser, free); return NULL; } if (yajl_status_ok != yajl_parse(yh, (const unsigned char *)str, len)) { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0/*verbose*/, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } #if YAJL_VERSION < 20000 if (yajl_status_ok != yajl_parse_complete(yh)) #else if (yajl_status_ok != yajl_complete_parse(yh)) #endif { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } // Let the parser finish its job. static TokenParam token_param; JsonSchemaParser(parser, 0, token_param, &yajl_context.parser_ctxt); // Even if parsing was completed there can be an error if (!yajl_context.parser_ctxt.error == SEC_OK) { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); validator_unref(yajl_context.parser_ctxt.validator); yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } yajl_free(yh); JsonSchemaParserFree(parser, free); // Post-parse processing Validator *v = yajl_context.parser_ctxt.validator; // Move parsed features to the validators validator_apply_features(v); // Combine type validator and other validators contaiters (allOf, anyOf, etc.) validator_combine(v); if (uri_resolver) validator_collect_uri(v, root_scope, uri_resolver); // Substitute every SchemaParsing by its type validator for every node // in the AST. Validator *result = validator_finalize_parse(v); // Forget about SchemaParsing then. validator_unref(v); return result; }
//todo: Test layouts with missing or misspelled keys. //print error message if key with non registered name is encountered. int read_layout(char* filename, int layoutTable[NUM_CANONICAL] ) { //("testing_read array size %d \n", layoutTable[0] ); //we'll ignore filename for now. FILE* fd; //selectedLayoutName=layoutname; //I've got an array //i've got its length in NUM_CANONICAL. //It's an int array. //fill it with -1 //in read int //look up the name in canonical names. //really? //no hash. if(filename != NULL) { //("about to open %s to load %s \n", filename); fd = fopen(filename, "r"); if (fd == NULL) { //("Could not open %s, exiting...\n", filename); exit(-7); } else { //("Could open but something else went wrong \n"); } } else { fd = stdin; } //("testing_read 1 \n"); yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ yajl_status stat; size_t rd; int retval = 0; int a = 1; /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, NULL, layoutTable); //("testing_read 2 \n"); /* and let's allow comments by default */ yajl_config(hand, yajl_allow_multiple_values, 1); //("testing_read 3 \n"); size_t bytesConsumed; for (;;) { //("testing_read 4a \n"); rd = fread((void *) fileData, 1, sizeof(fileData) - 1, fd); //("rd %ld\n",rd); if (rd == 0) { if (!feof(fd)) { //(stderr, "error on file read.\n"); retval = 1; } break; } fileData[rd] = 0; stat = yajl_parse(hand, fileData, rd); //("testing_read 4b \n"); bytesConsumed = yajl_get_bytes_consumed(hand); //("loop read: %ld consumed: %ld \n", rd, bytesConsumed); if (stat != yajl_status_ok) { //("not ok in \n"); break; } { //("okay\n"); } } //("testing_read 5 \n"); //("completed parse\n"); stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { //("not ok out\n"); unsigned char * str = yajl_get_error(hand, 1, fileData, rd); //(stderr, "%s", (const char *) str); yajl_free_error(hand, str); retval = 1; } yajl_free(hand); return retval; }
unsigned int ajv_get_bytes_consumed(ajv_state state) { return yajl_get_bytes_consumed(state->yajl); }
/* * Parse the JSON protocol header to determine protocol version and features. * In case the buffer does not contain a valid header (invalid JSON, or no * version field found), the 'correct' field of the returned header is set to * false. The amount of bytes consumed by parsing the header is returned in * *consumed (if non-NULL). * */ void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) { static yajl_callbacks version_callbacks = { .yajl_boolean = header_boolean, .yajl_integer = header_integer, .yajl_map_key = &header_map_key, }; child_init(child); current_key = NO_KEY; yajl_handle handle = yajl_alloc(&version_callbacks, NULL, child); /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for * yajl 2, we need to be explicit. */ yajl_config(handle, yajl_allow_trailing_garbage, 1); yajl_status state = yajl_parse(handle, buffer, length); if (state != yajl_status_ok) { child_init(child); if (consumed != NULL) *consumed = 0; } else { if (consumed != NULL) *consumed = yajl_get_bytes_consumed(handle); } yajl_free(handle); }