Ejemplo n.º 1
0
inline bool __JSONParseWithString(__JSONRef json, CFStringRef string, CFErrorRef *error) {
  bool success = 1;
  json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlAllocFuncs, (void *)json);
  if (json->yajlParser) {
//  yajl_config(json->yajlParser, yajl_allow_comments, kJSONReadOptionAllowComments | options ? 1 : 0);
//  yajl_config(json->yajlParser, yajl_dont_validate_strings, kJSONReadOptionCheckUTF8 | options ? 1 : 0);
  
    CFDataRef data = CFStringCreateExternalRepresentation(json->allocator, string, kCFStringEncodingUTF8, 0);
    if (data) {
      if ((json->yajlParserStatus = yajl_parse(json->yajlParser, CFDataGetBytePtr(data), CFDataGetLength(data))) != yajl_status_ok) {
        if (error) {
          success = 0;
          
          unsigned char * str = yajl_get_error(json->yajlParser, 1, CFDataGetBytePtr(data), CFDataGetLength(data));
          fprintf(stderr, "%s", (const char *) str);
          yajl_free_error(json->yajlParser, str);
          
          *error = CFErrorCreateWithUserInfoKeysAndValues(json->allocator, CFSTR("com.github.mirek.CoreJSON"), (CFIndex)json->yajlParserStatus, (const void *) { kCFErrorDescriptionKey }, (const void *) { CFSTR("Test") }, 1);
        }
        // TODO: Error stuff
        //printf("ERROR: %s\n", yajl_get_error(json->yajlParser, 1, __JSONUTF8StringGetBuffer(utf8), __JSONUTF8StringGetMaximumSize(utf8)));
      }
    
      json->yajlParserStatus = yajl_complete_parse(json->yajlParser);
      CFRelease(data);
    } else {
Ejemplo n.º 2
0
static int js_parser_parse(lua_State *L) {
    yajl_handle* handle = (yajl_handle*)
        lua_touserdata(L, lua_upvalueindex(1));
    if ( lua_isnil(L, 1) ) {
        js_parser_assert(L,
                         yajl_complete_parse(*handle),
                         handle,
                         NULL,
                         0,
                         __FILE__,
                         __LINE__);
    } else {
        size_t len;
        const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len);
        if ( NULL == buff ) return 0;
        js_parser_assert(L,
                         yajl_parse(*handle, buff, len),
                         handle,
                         buff,
                         len,
                         __FILE__,
                         __LINE__);
    }
    return 0;
}
Ejemplo n.º 3
0
/* if this method throw an exception then the object is invalid */
bool JsonShredder::Shred(uint64_t docseq,
                         const std::string& json,
                         std::string* idout,
                         std::string* errout) {
    YajlHandle hand(&ctx_);
    yajl_status stat;
    bool success = true;
    ctx_.docseq = docseq;
    stat = yajl_parse(hand, (unsigned char*)json.c_str(), json.length());

    if (stat == yajl_status_ok)
        stat = yajl_complete_parse(hand);

    if (stat == yajl_status_client_canceled) {
        if (ctx_.exception_ptr) {
            // some sorta exception occurred. rethrow.
            std::rethrow_exception(ctx_.exception_ptr);
        } else {
            // error message must be in tempbuff
            *errout = ctx_.tempbuff;
            success = false;
        }
    } else if (stat != yajl_status_ok) {
        hand.GetError(json, errout);
        success = false;
    }
    if (ctx_.docid.length() == 0) {
        *errout = "missing _id field";
        success = false;
    }
    *idout = ctx_.docid;

    return success;
}
Ejemplo n.º 4
0
yajl_val yajl_tree_parse_options (const char *input,
                                   char *error_buffer, size_t error_buffer_size,
                                   yajl_tree_option options)
{
    static const yajl_callbacks callbacks =
        {
            /* null        = */ handle_null,
            /* boolean     = */ handle_boolean,
            /* integer     = */ NULL,
            /* double      = */ NULL,
            /* number      = */ handle_number,
            /* string      = */ handle_string,
            /* start map   = */ handle_start_map,
            /* map key     = */ handle_string,
            /* end map     = */ handle_end_map,
            /* start array = */ handle_start_array,
            /* end array   = */ handle_end_array
        };

    yajl_handle handle;
    yajl_status status;
    char * internal_err_str;
	context_t ctx = { NULL, NULL, NULL, 0 };

	ctx.errbuf = error_buffer;
	ctx.errbuf_size = error_buffer_size;

    if (error_buffer != NULL)
        memset (error_buffer, 0, error_buffer_size);

    handle = yajl_alloc (&callbacks, NULL, &ctx);
    yajl_config(handle, yajl_allow_comments,
            (options & yajl_tree_option_dont_allow_comments) ? 0 : 1);
    yajl_config(handle, yajl_allow_trailing_separator,
            (options & yajl_tree_option_allow_trailing_separator) ? 1 : 0);

    status = yajl_parse(handle,
                        (unsigned char *) input,
                        strlen (input));
    status = yajl_complete_parse (handle);
    if (status != yajl_status_ok) {
        if (error_buffer != NULL && error_buffer_size > 0) {
               internal_err_str = (char *) yajl_get_error(handle, 1,
                     (const unsigned char *) input,
                     strlen(input));
             snprintf(error_buffer, error_buffer_size, "%s", internal_err_str);
             YA_FREE(&(handle->alloc), internal_err_str);
        }
        while (ctx.stack) {
            yajl_tree_free(context_pop(&ctx));
        }
        yajl_free (handle);
        return NULL;
    }

    yajl_free (handle);
    return (ctx.root);
}
Ejemplo n.º 5
0
CJSONVariantParser::~CJSONVariantParser()
{
#if YAJL_MAJOR == 2
  yajl_complete_parse(m_handler);
#else
  yajl_parse_complete(m_handler);
#endif
  yajl_free(m_handler);
}
Ejemplo n.º 6
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl = NULL;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  rxc->check = noit_poller_lookup(rxc->check_id);
  if (!rxc->check) {
    *complete = 1;
    return NULL;
  }

  if(!strcmp(rxc->check->module, "httptrap")) ccl = rxc->check->closure;
  rxc->immediate = noit_httptrap_check_asynch(ccl ? ccl->self : NULL, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      _YD("inbound payload chunk (%d bytes) continuing YAJL parse\n", len);
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      _YD("no more data, finishing YAJL parse\n");
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}
SLNFilterRef SLNJSONFilterParserEnd(SLNJSONFilterParserRef const parser) {
	if(!parser) return NULL;
	assertf(parser->JSONParser, "Parser in invalid state");
	yajl_status const err = yajl_complete_parse(parser->JSONParser);
	yajl_free(parser->JSONParser); parser->JSONParser = NULL;
	assertf(-1 == parser->depth, "Parser ended at invalid depth %d", parser->depth);
	SLNFilterRef const filter = parser->stack[0];
	parser->stack[0] = NULL;
	return yajl_status_ok == err ? filter : NULL;
}
Ejemplo n.º 8
0
bool jsaxparser_end(jsaxparser_ref parser)
{
#if YAJL_VERSION < 20000
	parser->status = yajl_parse_complete(parser->handle);
#else
	parser->status = yajl_complete_parse(parser->handle);
#endif

	return jsaxparser_process_error(parser, "", 0, true);
}
Ejemplo n.º 9
0
bool hkvJsonStreamReader::Parse(const char* pszFileName, unsigned int uiChunkSize)
{
  yajl_status stat;
  void* pFile;
  size_t rd;

  // Allocate read buffer
  unsigned char* pFileData = (unsigned char*)(YA_MALLOC(&(m_pHandle->alloc), uiChunkSize));
  assert(pFileData);

  // Open file
  pFile = m_pFileHandler->Open(pszFileName, hkvJsonFileHandler::JFM_READ);
  if (pFile == NULL)
    return false;

  for (;;)
  {
    rd = m_pFileHandler->Read((void*)pFileData, uiChunkSize - 1, pFile);

    if (rd == 0)
    {
      if (!m_pFileHandler->IsEOF(pFile))
        fprintf(stderr, "Error encountered on file read.\n");
      break;
    }
    pFileData[rd] = 0;

    // Pass to parser
    stat = yajl_parse(m_pHandle, pFileData, rd);

    if (stat != yajl_status_ok)
      break;
  }

  // Parse any remaining buffered data
  stat = yajl_complete_parse(m_pHandle);

  if (stat != yajl_status_ok)
  {
    unsigned char * str = yajl_get_error(m_pHandle, 1, pFileData, rd);
    fprintf(stderr, "%s", (const char *) str);
    yajl_free_error(m_pHandle, str);

    assert(false);
    return false;
  }

  // Close the file
  m_pFileHandler->Close(pFile);

  // Free read buffer
  YA_FREE(&(m_pHandle->alloc), pFileData);

  return true;
}
Ejemplo n.º 10
0
static int cj_perform (cj_t *db) /* {{{ */
{
    int status;
    yajl_handle yprev = db->yajl;

    db->yajl = yajl_alloc (&ycallbacks,
#if HAVE_YAJL_V2
                           /* alloc funcs = */ NULL,
#else
                           /* alloc funcs = */ NULL, NULL,
#endif
                           /* context = */ (void *)db);
    if (db->yajl == NULL)
    {
        ERROR ("curl_json plugin: yajl_alloc failed.");
        db->yajl = yprev;
        return (-1);
    }

    if (db->url)
        status = cj_curl_perform (db);
    else
        status = cj_sock_perform (db);
    if (status < 0)
    {
        yajl_free (db->yajl);
        db->yajl = yprev;
        return (-1);
    }

#if HAVE_YAJL_V2
    status = yajl_complete_parse(db->yajl);
#else
    status = yajl_parse_complete(db->yajl);
#endif
    if (status != yajl_status_ok)
    {
        unsigned char *errmsg;

        errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
                                 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
        ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
               (char *) errmsg);
        yajl_free_error (db->yajl, errmsg);
        yajl_free (db->yajl);
        db->yajl = yprev;
        return (-1);
    }

    yajl_free (db->yajl);
    db->yajl = yprev;
    return (0);
} /* }}} int cj_perform */
Ejemplo n.º 11
0
/*
 * Public functions
 */
yajl_val yajl_tree_parse (const char *input,
                          char *error_buffer, size_t error_buffer_size)
{
    static const yajl_callbacks callbacks =
    {
        /* null        = */ handle_null,
        /* boolean     = */ handle_boolean,
        /* integer     = */ NULL,
        /* double      = */ NULL,
        /* number      = */ handle_number,
        /* string      = */ handle_string,
        /* start map   = */ handle_start_map,
        /* map key     = */ handle_string,
        /* end map     = */ handle_end_map,
        /* start array = */ handle_start_array,
        /* end array   = */ handle_end_array
    };

    yajl_handle handle;
    yajl_status status;
    context_t ctx = { NULL, NULL, NULL, 0 };

    ctx.errbuf = error_buffer;
    ctx.errbuf_size = error_buffer_size;

    if (error_buffer != NULL)
        memset (error_buffer, 0, error_buffer_size);

    handle = yajl_alloc (&callbacks, NULL, &ctx);
    yajl_config(handle, yajl_allow_comments, 1);

    status = yajl_parse(handle,
                        (unsigned char *) input,
                        strlen (input));
    status = yajl_complete_parse (handle);
    if (status != yajl_status_ok) {
        if (error_buffer != NULL && error_buffer_size > 0) {
            snprintf(
                error_buffer, error_buffer_size, "%s",
                (char *) yajl_get_error(handle, 1,
                                        (const unsigned char *) input,
                                        strlen(input)));
        }
        yajl_free (handle);
        return NULL;
    }

    yajl_free (handle);
    return (ctx.root);
}
Ejemplo n.º 12
0
TEST_F(Yajl, yajl_parse_nullcallbacks) {
	for (size_t i = 0; i < kTrialCount; i++) {
		yajl_handle hand = yajl_alloc(&nullcallbacks, NULL, NULL);
		yajl_status stat = yajl_parse(hand, (unsigned char*)json_, length_ - 1);
		//ASSERT_EQ(yajl_status_ok, stat);
		if (stat != yajl_status_ok) {
			unsigned char * str = yajl_get_error(hand, 1, (unsigned char*)json_, length_ + 1);
			fprintf(stderr, "%s", (const char *) str);
		}
		stat = yajl_complete_parse(hand);
		ASSERT_EQ(yajl_status_ok, stat);
		yajl_free(hand);
	}	
}
Ejemplo n.º 13
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  ccl = rxc->check->closure;
  rxc->immediate = mtev_httptrap_check_aynsch(ccl->self, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}
Ejemplo n.º 14
0
static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) {
  yajl_handle hand;
  yajl_status stat;
  unsigned char *err;
  volatile CTX ctx;

  rb_ivar_set(self, rb_intern("key"), Qnil);
  rb_ivar_set(self, rb_intern("stack"), rb_ary_new());
  rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new());

  ctx.self = self;
  ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys");
  ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking");

  hand = yajl_alloc(&callbacks, NULL, (void *)&ctx);

  if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) {
    yajl_config(hand, yajl_allow_comments, 1);
  }
  if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) {
    yajl_config(hand, yajl_dont_validate_strings, 1);
  }
  if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) {
    yajl_config(hand, yajl_allow_trailing_garbage, 1);
  }
  if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) {
    yajl_config(hand, yajl_allow_multiple_values, 1);
  }
  if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) {
    yajl_config(hand, yajl_allow_partial_values, 1);
  }

  if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) {
    err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
    goto raise;
  }
  if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) {
    err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
    goto raise;
  }
  yajl_free(hand);
  return rb_ary_pop(rb_ivar_get(self, rb_intern("stack")));

raise:
  if (hand) {
    yajl_free(hand);
  }
  rb_raise(cParseError, "%s", err);
}
Ejemplo n.º 15
0
/**
 * Finalise JSON parsing.
 */
int json_complete(modsec_rec *msr, char **error_msg) {
    char *json_data = (char *) NULL;

    if (error_msg == NULL) return -1;
    *error_msg = NULL;

    /* Wrap up the parsing process */
    msr->json->status = yajl_complete_parse(msr->json->handle);
    if (msr->json->status != yajl_status_ok) {
        /* We need to free the yajl error message later, how to do this? */
        *error_msg = yajl_get_error(msr->json->handle, 0, NULL, 0);
        return -1;
    }

    return 1;
}
Ejemplo n.º 16
0
struct aws_dynamo_put_item_response * aws_dynamo_parse_put_item_response(const char *response, int response_len, struct aws_dynamo_attribute *attributes, int num_attributes)
{
	yajl_handle hand;
	yajl_status stat;
	struct put_item_ctx _ctx = { 0 };

	_ctx.r = calloc(sizeof(*(_ctx.r)), 1);
	if (_ctx.r == NULL) {
		Warnx("aws_dynamo_parse_put_item_response: response alloc failed.");
		return NULL;
	}

	if (num_attributes > 0) {
		_ctx.r->attributes = malloc(sizeof(*(_ctx.r->attributes)) * num_attributes);
		if (_ctx.r->attributes == NULL) {
			Warnx("aws_dynamo_parse_put_item_response: attribute alloc failed.");
			free(_ctx.r);
			return NULL;
		}
		memcpy(_ctx.r->attributes, attributes, sizeof(*(attributes)) * num_attributes);
		_ctx.r->num_attributes = num_attributes;
	}

#if YAJL_MAJOR == 2
	hand = yajl_alloc(&put_item_callbacks, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_complete_parse(hand);
#else
	hand = yajl_alloc(&put_item_callbacks, NULL, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_parse_complete(hand);
#endif

	if (stat != yajl_status_ok) {
		unsigned char *str =
		    yajl_get_error(hand, 1, response, response_len);
		Warnx("aws_dynamo_parse_put_item_response: json parse failed, '%s'", (const char *)str);
		yajl_free_error(hand, str);
		yajl_free(hand);
		aws_dynamo_free_put_item_response(_ctx.r);
		return NULL;
	}

	yajl_free(hand);
	return _ctx.r;
}
Ejemplo n.º 17
0
static int lyajl_complete_parse (lua_State *L) {
  yajl_status stat;
  luvit_parser_t *parser = parser_get(L, 1);

  /* Process the args */
  stat = yajl_complete_parse(parser->handle);

  /* Unreference the callback */
  luaL_unref(L, LUA_REGISTRYINDEX, parser->ref->r);

  if (stat != yajl_status_ok) {
    unsigned char * str = yajl_get_error(parser->handle, 1, (const unsigned char*)0, 0);
    luaL_error(L, (const char *) str);
    yajl_free_error(parser->handle, str); /* This doesn't actually happen */
  }

  return 0;
}
Ejemplo n.º 18
0
bool run_test(uint32_t* hash_out) {
    char* data = benchmark_in_situ_copy(file_data, file_size);
    if (!data)
        return false;

    parser_t parser;
    parser_init(&parser, *hash_out);

    yajl_handle handle = yajl_alloc(&callbacks, NULL, &parser);
    yajl_status status = yajl_parse(handle, (const unsigned char*)file_data, file_size);
    if (status == yajl_status_ok)
        status = yajl_complete_parse(handle);
    yajl_free(handle);

    *hash_out = parser.hash;
    benchmark_in_situ_free(data);
    return status == yajl_status_ok;
}
Ejemplo n.º 19
0
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;
}
Ejemplo n.º 20
0
/* STRATEGY:
 *
 * Each of the js_to_value_callbacks perform these actions:
 *
 * [1] Push a new value onto the top of the Lua stack.
 *
 * [2] Call the function that was at the top of the Lua stack before
 *     step [1] occurred.
 *
 * The purpose of the function call in [2] is to take the value at the
 * top of the stack and store it in the appropriate location.
 * Initially, the function is the noop (no operation) function which
 * does nothing.  Therefore we know that the final result is on the
 * top of the Lua stack.
 *
 * The to_value_start_map and to_value_start_array callbacks are
 * different since they need to use a bit of the Lua stack to store
 * some state information.  When these callbacks are ran, they perform
 * these actions:
 *
 * [a] Push a new table which will represent the final "array" or
 *     "object" onto the top of the Lua stack.
 *
 * [b] Allocate space for the "key" (in the case of arrays, this is
 *     the index into the array to use as part of the next insertion)
 *
 * [c] Push the got_array_value or got_map_key function.
 *
 * The got_array_value function will take the value at the top of the
 * stack and insert it into the table created in step [a].  It will
 * then increment the index created in step [b].  As a final step, it
 * removes the value at the top of the stack.
 *
 * The got_map_key function simply takes the value at the top of the
 * stack and stores it in the space allocated by step [b] above.  It
 * then replaces the function pushed onto the stack by step [c] with
 * the got_map_value function.  As a final step, it removes the value
 * at the top of the stack.
 *
 * The got_map_value function takes the value at the top of the stack
 * and inserts it into the table created in step [a] with the key
 * whose space was allocated in step [b].  The function pushed onto
 * the stack by step [c] is then restored back to the got_map_key
 * function.  As a final step, it removes the value at the top of the
 * stack.
 */
static int js_to_value(lua_State *L) {
    yajl_handle          handle;
    size_t               len;
    const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len);

    if ( NULL == buff ) return 0;

    handle = yajl_alloc(&js_to_value_callbacks, NULL, (void*)L);
    lua_pushcfunction(L, noop);

    if ( lua_istable(L, 2) ) {
        lua_getfield(L, 2, "allow_comments");
        if ( ! lua_isnil(L, -1) ) {
            yajl_config(handle, yajl_allow_comments, lua_toboolean(L, -1));
        }
        lua_pop(L, 1);

        lua_getfield(L, 2, "check_utf8");
        if ( ! lua_isnil(L, -1) ) {
            yajl_config(handle, yajl_dont_validate_strings, !lua_toboolean(L, -1));
        }
        lua_pop(L, 1);
    }

    js_parser_assert(L,
                     yajl_parse(handle, buff, len),
                     &handle,
                     buff,
                     len,
                     __FILE__,
                     __LINE__);

    js_parser_assert(L,
                     yajl_complete_parse(handle),
                     &handle,
                     buff,
                     len,
                     __FILE__,
                     __LINE__);

    yajl_free(handle);

    return 1;
}
Ejemplo n.º 21
0
bool readJson (const std::string& str)
{
	yajl_handle hand = yajl_alloc(&callbacks, nullptr, nullptr);
	yajl_status stat = yajl_parse(hand, reinterpret_cast<const unsigned char*>(str.c_str()), str.length());
	if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
		printError(hand, str);
		return false;
	}

	stat = yajl_complete_parse(hand);
	if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
		printError(hand, str);
		return false;
	}

	yajl_free(hand);

	return true;
}
Ejemplo n.º 22
0
static void yajl_init(struct json_streamer *json, json_streamer_frame_cb_t fcb, void *userdata, bool reinit)
{
	struct json_streamer_state_ctx *ctx = json->ctx;

	if (!ctx)
		ctx = ec_malloc(sizeof(struct json_streamer_state_ctx));

	ctx->cur_state = waiting;
	ctx->frame_callback = fcb;
	ctx->userdata = userdata;

	/* Save pointer so that we can free it later in json_streamer_destroy() */
	json->ctx = ctx;

	if (reinit) {
		yajl_complete_parse(json->yajl);
		yajl_free(json->yajl);
	}

	json->yajl = yajl_alloc(&yajl_cbs, NULL, ctx);
}
Ejemplo n.º 23
0
// Client records will be coming through in alphabetical order.
// FIX THIS: If a client is deleted on the server, it is not deleted from
// clist.
int json_input(struct asfd *asfd, struct sel *sel)
{
        static yajl_handle yajl=NULL;
	cslist=&sel->clist;
	sselbu=&sel->backup;
	sllines=&sel->llines;

	if(!yajl)
	{
		if(!(yajl=yajl_alloc(&callbacks, NULL, NULL)))
			goto error;
		yajl_config(yajl, yajl_dont_validate_strings, 1);
	}
	if(yajl_parse(yajl, (const unsigned char *)asfd->rbuf->buf,
		asfd->rbuf->len)!=yajl_status_ok)
	{
		do_yajl_error(yajl, asfd);
		goto error;
	}

	if(!map_depth)
	{
		// Got to the end of the JSON object.
		if(!sel->gotfirstresponse) sel->gotfirstresponse=1;
		if(yajl_complete_parse(yajl)!=yajl_status_ok)
		{
			do_yajl_error(yajl, asfd);
			goto error;
		}
		yajl_free(yajl);
		yajl=NULL;
	}

	return 0;
error:
	yajl_free(yajl);
	yajl=NULL;
	return -1;
}
Ejemplo n.º 24
0
void nx_json_parse(nx_json_parser_ctx_t *ctx,
		   const char *json, size_t len)
{
    yajl_handle hand;
    yajl_gen g;

    g = yajl_gen_alloc(NULL);  
    //yajl_gen_config(g, yajl_gen_validate_utf8, 1);  
  
    hand = yajl_alloc(&callbacks, NULL, (void *) ctx);  
    yajl_config(hand, yajl_allow_comments, 1);  

    if ( (yajl_parse(hand, (const unsigned char *) json, len) != yajl_status_ok) ||
	 (yajl_complete_parse(hand) != yajl_status_ok) )
    {
	unsigned char *errstr = yajl_get_error(hand, 1, (const unsigned char *) json, len);  

	log_error("failed to parse json string, %s [%s]", errstr, json);
        yajl_free_error(hand, errstr);  
    }  

    yajl_gen_free(g);
    yajl_free(hand);  
}
Ejemplo n.º 25
0
/**
 * Initiate JSON parsing and print error if one occurs
 */
static int cconn_process_json(struct cconn *io)
{
    if((io->request_type != ASOK_REQ_DATA) &&
            (io->request_type != ASOK_REQ_SCHEMA))
    {
        return -EDOM;
    }

    int result = 1;
    yajl_handle hand;
    yajl_status status;

    hand = yajl_alloc(&callbacks,
#if HAVE_YAJL_V2
      /* alloc funcs = */ NULL,
#else
      /* alloc funcs = */ NULL, NULL,
#endif
      /* context = */ (void *)(&io->yajl));

    if(!hand)
    {
        ERROR ("ceph plugin: yajl_alloc failed.");
        return ENOMEM;
    }

    io->yajl.depth = 0;

    switch(io->request_type)
    {
        case ASOK_REQ_DATA:
            io->yajl.handler = node_handler_fetch_data;
            result = cconn_process_data(io, &io->yajl, hand);
            break;
        case ASOK_REQ_SCHEMA:
            //init daemon specific variables
            io->d->ds_num = 0;
            io->d->last_idx = 0;
            io->d->last_poll_data = NULL;
            io->yajl.handler = node_handler_define_schema;
            io->yajl.handler_arg = io->d;
            result = traverse_json(io->json, io->json_len, hand);
            break;
    }

    if(result)
    {
        goto done;
    }

#if HAVE_YAJL_V2
    status = yajl_complete_parse(hand);
#else
    status = yajl_parse_complete(hand);
#endif

    if (status != yajl_status_ok)
    {
      unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0,
          /* jsonText = */ NULL, /* jsonTextLen = */ 0);
      ERROR ("ceph plugin: yajl_parse_complete failed: %s",
          (char *) errmsg);
      yajl_free_error (hand, errmsg);
      yajl_free (hand);
      return 1;
    }

    done:
    yajl_free (hand);
    return result;
}
Ejemplo n.º 26
0
int enron_messages_init_fread(FILE *stream, size_t maxrecip, double **time,
			      size_t **from, size_t ***to, size_t **nto,
			      intptr_t **attr, size_t *nmsg)
{
	unsigned char fileData[65536];
	size_t rd;
	yajl_status stat;
	int parse_ok = 1;

	struct message_parse parse;

	message_parse_init(&parse, maxrecip);

	yajl_handle hand = yajl_alloc(&parse_callbacks, NULL, (void *) &parse);
	yajl_config(hand, yajl_allow_comments, 1);
	yajl_config(hand, yajl_dont_validate_strings, 1);

	for (;;) {
		rd = fread((void *)fileData, 1, sizeof(fileData) - 1, stream);

		if (rd == 0) {
			if (!feof(stream)) {
				fprintf(stderr, "error on file read.\n");
				parse_ok = 0;
			}
			break;
		}
		fileData[rd] = 0;

		stat = yajl_parse(hand, fileData, rd);
		if (stat != yajl_status_ok) break;
	}

	stat = yajl_complete_parse(hand);

	if (stat != yajl_status_ok) {
		unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
		fprintf(stderr, "%s", (const char *) str);
		yajl_free_error(hand, str);
		parse_ok = 0;
	}

	yajl_free(hand);
	message_parse_deinit(&parse);

	if (!parse_ok) {
		size_t i, n = parse.nmsg;
		for (i = 0; i < n; i++) {
			free(parse.to[i]);
		}
		free(parse.attr);
		free(parse.nto);
		free(parse.to);
		free(parse.from);
		free(parse.time);
	}

	*time = parse.time;
	*from = parse.from;
	*to = parse.to;
	*nto = parse.nto;
	*attr = parse.attr;
	*nmsg = parse.nmsg;

	return parse_ok ? 0 : -1;
}
Ejemplo n.º 27
0
static int
run(int validate_utf8)
{
    long long times = 0; 
    double starttime;
    unsigned long long sumsize = 0;

    starttime = mygettime();

    /* allocate a parser */
    for (;;) {
		int i;
        {
            double now = mygettime();
            if (now - starttime >= PARSE_TIME_SECS) break;
        }

        for (i = 0; i < 100; i++) {
            yajl_handle hand = yajl_alloc(NULL, NULL, NULL);
            yajl_status stat;        
            const char ** d;

            yajl_config(hand, yajl_dont_validate_strings, validate_utf8 ? 0 : 1);

            for (d = get_doc(times % num_docs()); *d; d++) {
                size_t size = strlen(*d);
                sumsize += size;
                stat = yajl_parse(hand, (unsigned char *) *d, size);
                if (stat != yajl_status_ok) break;
            }
            
            stat = yajl_complete_parse(hand);

            if (stat != yajl_status_ok) {
                unsigned char * str =
                    yajl_get_error(hand, 1,
                                   (unsigned char *) *d,
                                   (*d ? strlen(*d) : 0));
                fprintf(stderr, "%s", (const char *) str);
                yajl_free_error(hand, str);
                return 1;
            }
            yajl_free(hand);
            times++;
        }
    }

    /* parsed doc 'times' times */
    {
        double throughput;
        double now;
        const char * all_units[] = { "B/s", "KB/s", "MB/s", (char *) 0 };
        const char ** units = all_units;

        now = mygettime();

        throughput = sumsize / (now - starttime);
        
        while (*(units + 1) && throughput > 1024) {
            throughput /= 1024;
            units++;
        }
        
        printf("Parsing speed: %g %s\n", throughput, *units);
    }

    return 0;
}
Ejemplo n.º 28
0
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;
}
struct aws_dynamo_create_table_response
*aws_dynamo_parse_create_table_response(const char *response, int response_len)
{
	yajl_handle hand;
	yajl_status stat;
	struct ctx _ctx = { 0 };

	_ctx.r = calloc(sizeof(*(_ctx.r)), 1);
	if (_ctx.r == NULL) {
		Warnx("aws_dynamo_parse_create_table_response: response alloc failed.");
		return NULL;
	}

#if YAJL_MAJOR == 2
	hand = yajl_alloc(&handle_callbacks, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_complete_parse(hand);
#else
	hand = yajl_alloc(&handle_callbacks, NULL, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_parse_complete(hand);
#endif

	if (stat != yajl_status_ok) {
		unsigned char *str = yajl_get_error(hand, 1, response, response_len);
		Warnx("aws_dynamo_parse_create_table_response: json parse failed, '%s'", (const char *)str);
		yajl_free_error(hand, str);
		yajl_free(hand);
		aws_dynamo_free_create_table_response(_ctx.r);
		return NULL;
	}

	yajl_free(hand);
	return _ctx.r;
}
Ejemplo n.º 30
-6
Archivo: json.c Proyecto: Hmaal/slash
static SLVAL
sl_json_parse(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv)
{
    sl_string_t* str = sl_get_string(vm, argv[0]);
    json_parse_t json;
    yajl_alloc_funcs alloc_funcs = {
        sl_yajl_alloc,
        sl_yajl_realloc,
        sl_yajl_free,
        NULL
    };
    alloc_funcs.ctx = vm->arena;
    json.vm = vm;
    json.max_depth = 32;
    json.stack_len = 0;
    json.stack_cap = 32;
    json.stack = sl_alloc(vm->arena, sizeof(SLVAL) * json.stack_cap);
    json.type_stack = sl_alloc(vm->arena, json.stack_cap);
    if(argc > 1) {
        json.max_depth = sl_get_int(sl_expect(vm, argv[1], vm->lib.Int));
    }
    
    json.yajl = yajl_alloc(&callbacks, &alloc_funcs, &json);
    sl_json_parse_check_error(vm, str, &json, yajl_parse(json.yajl, str->buff, str->buff_len));
    sl_json_parse_check_error(vm, str, &json, yajl_complete_parse(json.yajl));
    yajl_free(json.yajl);

    /* must've been OK! */
    return json.stack[0];
        
    (void)self;
}