Пример #1
0
static int js_to_string(lua_State *L) {
    yajl_gen* gen;
    const unsigned char *buf;
    size_t len;

    lua_pushcfunction(L, js_generator);
    /* convert_me, {extra}, ?, js_gen */
    if ( lua_istable(L, 2) ) {
        /* Be sure printer is not defined: */
        lua_pushliteral(L, "printer");
        lua_pushnil(L);
        lua_rawset(L, 2);
        lua_pushvalue(L, 2);
        /* convert_me, {extra}, ?, js_gen, {extra} */
    } else {
        lua_newtable(L);
        /* convert_me, {extra}, ?, js_gen, {} */
    }
    lua_call(L, 1, 1);
    /* convert_me, {extra}, ?, gen_ud */
    lua_pushcfunction(L, js_generator_value);
    /* convert_me, {extra}, ?, gen_ud, js_gen_val */
    lua_pushvalue(L, -2);
    /* convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud */
    lua_pushvalue(L, 1);
    /* convert_me, {extra}, ?, gen_ud, js_gen_val, gen_ud, convert_me */
    lua_call(L, 2, 0);
    /* convert_me, {extra}, ?, gen_ud */
    gen = js_check_generator(L, -1);
    yajl_gen_get_buf(*gen, &buf, &len);
    /* Copy into results: */
    lua_pushlstring(L, (char*)buf, len);
    yajl_gen_clear(*gen);
    return 1;
}
Пример #2
0
static int write_all(struct asfd *asfd)
{
	int ret=-1;
	size_t w=0;
	size_t len=0;
	const unsigned char *buf;
	struct iobuf wbuf;

	yajl_gen_get_buf(yajl, &buf, &len);
	while(len)
	{
		w=len;
		if(w>ASYNC_BUF_LEN) w=ASYNC_BUF_LEN;
		iobuf_set(&wbuf, CMD_GEN /* not used */, (char *)buf, w);
		if((ret=asfd->write(asfd, &wbuf)))
			break;
		buf+=w;
		len-=w;
	}
	if(!ret && !pretty_print)
	{
		iobuf_set(&wbuf, CMD_GEN /* not used */, (char *)"\n", 1);
		ret=asfd->write(asfd, &wbuf);
	}

	yajl_gen_clear(yajl);
	return ret;
}
Пример #3
0
xlw::XlfOper HttpProtocol::Execute(const char* name, bool sendCaller, xlw::XlfOper* args, int argc)
{
	REQUEST_CONTEXT context;
	context.hEvent = CreateEvent(0, 1, 0, 0);
	context.hConnect = WinHttpConnect(hSession, host, urlc.nPort, 0);
	int flags = WINHTTP_FLAG_BYPASS_PROXY_CACHE;
	if(urlc.nScheme == INTERNET_SCHEME_HTTPS)
		flags |= WINHTTP_FLAG_SECURE;
	context.hRequest = WinHttpOpenRequest(context.hConnect, L"POST", path, 0, 0, 0, 
		flags);
	context.conf.beautify = 0;
	context.conf.indentString = "";
	context.g = yajl_gen_alloc(&context.conf, 0);
	context.px = xlw::XlfOper();
	//context.px->xltype = xltypeNil | xlbitDLLFree;
	GenerateRequest(context.g, name, sendCaller, args, argc);
	const unsigned char * buf;
    unsigned int len = 0;
    yajl_gen_get_buf(context.g, &buf, &len);
	BOOL res = FALSE;
	res = WinHttpSendRequest(context.hRequest, 0, 0, (LPVOID) buf, len, len, (DWORD_PTR) &context);
	if(!res) {
		const char* err = "#Could not connect to server";
		Log::Error(err);
		WinHttpCloseHandle(context.hRequest);
		WinHttpCloseHandle(context.hConnect);
		context.px = xlw::XlfOper(err);
		return context.px;
	}
	// TODO timeout/background
	res = WinHttpReceiveResponse(context.hRequest, 0);
	if(!res) {
		const char* err = "#Error retrieving server response";
		Log::Error(err);
		WinHttpCloseHandle(context.hRequest);
		WinHttpCloseHandle(context.hConnect);
		context.px = xlw::XlfOper(err);
		return context.px;
	}
	// Check http response code
	DWORD status;
	DWORD statusLength = 4;
	res = WinHttpQueryHeaders(context.hRequest, WINHTTP_QUERY_STATUS_CODE| WINHTTP_QUERY_FLAG_NUMBER,
		NULL, &status, &statusLength, 0);
	if(!res || status != 200) {
		Log::Error("Status code: %d", status);
		const char* err = "#Server returned an error";
		WinHttpCloseHandle(context.hRequest);
		WinHttpCloseHandle(context.hConnect);
		context.px = xlw::XlfOper(err);
		return context.px;
	}
	ReadData(&context);
	WinHttpCloseHandle(context.hRequest);
	WinHttpCloseHandle(context.hConnect);
    yajl_gen_clear(context.g);
	yajl_gen_free(context.g);
	//context.px->xltype |= xlbitDLLFree;
	return context.px;
}
Пример #4
0
gboolean
log_save(gchar *filename)
{
    gzFile gzfp = NULL;
    FILE *fp = NULL;
    gchar *ext;
    gboolean compression;
    yajl_gen gen;
    const guchar *json_string;
    size_t json_length;
    gint wrote;

    ext = strrchr(filename, '.');
    compression = (ext && !g_ascii_strcasecmp(ext, ".gz"));

    if(compression)
        gzfp = gzopen(filename, "wb");
    else
        fp = fopen(filename, "w");

    if(!gzfp && !fp)
    {
        ui_dialog(ui.window,
                  GTK_MESSAGE_ERROR,
                  "Error",
                  "Unable to save a file:\n%s", filename);
        return FALSE;
    }

    gen = yajl_gen_alloc(NULL);
    //yajl_gen_config(gen, yajl_gen_beautify, 1);
    yajl_gen_map_open(gen);
    gtk_tree_model_foreach(GTK_TREE_MODEL(ui.model->store), log_save_foreach, gen);
    yajl_gen_map_close(gen);
    yajl_gen_get_buf(gen, &json_string, &json_length);

    if(compression)
    {
        wrote = gzwrite(gzfp, json_string, json_length);
        gzclose(gzfp);
    }
    else
    {
        wrote = fwrite(json_string, sizeof(gchar), json_length, fp);
        fclose(fp);
    }

    yajl_gen_free(gen);

    if(json_length != wrote)
    {
        ui_dialog(ui.window,
                  GTK_MESSAGE_ERROR,
                  "Error",
                  "Unable to save a file:\n%s\n\nWrote only %d of %d uncompressed bytes.",
                  filename, wrote, json_length);
        return FALSE;
    }
    return TRUE;
}
Пример #5
0
static gchar *
_j4status_i3bar_output_generate_header(J4statusPluginContext *context)
{
    yajl_gen json_gen;
    json_gen = yajl_gen_alloc(NULL);

    yajl_gen_map_open(json_gen);
    yajl_gen_string(json_gen, (const unsigned char *)"version", strlen("version"));
    yajl_gen_integer(json_gen, 1);
    yajl_gen_string(json_gen, (const unsigned char *)"stop_signal", strlen("stop_signal"));
    yajl_gen_integer(json_gen, SIGUSR2);
    yajl_gen_string(json_gen, (const unsigned char *)"cont_signal", strlen("cont_signal"));
    yajl_gen_integer(json_gen, SIGUSR1);
    if ( ! context->no_click_events )
    {
        yajl_gen_string(json_gen, (const unsigned char *)"click_events", strlen("click_events"));
        yajl_gen_bool(json_gen, 1);
    }
    yajl_gen_map_close(json_gen);

    const unsigned char *buffer;
    size_t length;
    yajl_gen_get_buf(json_gen, &buffer, &length);

    gchar *header;

    header = g_strdup_printf("%s\n[[]\n", buffer);
    yajl_gen_free(json_gen);

    return header;
}
Пример #6
0
std::vector<char> LKStreamTranslator::process(const std::vector<char> &chunk)
{
	std::vector<char> retval;
	
	// We work with C strings here, because YAJL
	const char *str = chunk.data();
	size_t len = chunk.size();
	
	// Abort if the string is empty anyways
	if(len == 0)
		return retval;
	
	// Skip any BOM if there is one
	const unsigned char bom[] = {0xEF, 0xBB, 0xBF};
	if(len >= 3 && memcmp(bom, str, 3) == 0)
	{
		str += 3;
		len -= 3;
	}
	
	// Abort if it had nothing but a BOM
	if(len == 0)
		return retval;
	
	// KanColle specific: most datablobs are prefixed with "svdata=", that's
	// needed for the game to work, but we can't parse with it left in
	const char *svdata = "svdata=";
	if(memcmp(svdata, str, (size_t)std::min(strlen(svdata), len)) == 0)
	{
		str += strlen(svdata);
		len -= strlen(svdata);
		
		retval.insert(retval.end(), svdata, svdata + strlen(svdata));
	}
	
	if(len > 0)
	{
		if(yajl_parse(ctx.parser, (const unsigned char *)str, len) != yajl_status_ok)
		{
			unsigned char *error = yajl_get_error(ctx.parser, true, (const unsigned char *)str, len);
			std::string error_string((const char *)error);
			yajl_free_error(ctx.parser, error);
			
			throw std::runtime_error("JSON Parsing Error: " + error_string);
		}
		
		const unsigned char *buf;
		size_t buf_len;
		yajl_gen_get_buf(ctx.gen, &buf, &buf_len);
		
		if(buf_len > 0)
		{
			retval.insert(retval.end(), buf, buf + buf_len);
			yajl_gen_clear(ctx.gen);
		}
	}
	
	return retval;
}
Пример #7
0
void yajl_print( yajl_gen g )
{
    const unsigned char * buf;
    size_t len;

    yajl_gen_get_buf( g, &buf, &len );
    printf( "%s\n", buf );
}
Пример #8
0
static void json_print(FILE *f, yajl_gen yajl)
{
	const unsigned char *buf;
	size_t len;
	yajl_gen_get_buf(yajl, &buf, &len);
	fputs((char*)buf, f);
	fputc('\n', f);
	yajl_gen_clear(yajl);
}
Пример #9
0
static int lyajl_gen_get_buf (lua_State *L) {
  const unsigned char *buf;
  size_t len;
  luvit_generator_t *generator = generator_get(L, 1);
  yajl_gen_get_buf(generator->gen, &buf, &len);
  lua_pushlstring(L, (const char*)buf, len);
  yajl_gen_clear(generator->gen);
  return 1;
}
Пример #10
0
void Datum_setYajl_(Datum *self, yajl_gen y)
{
	const unsigned char *jsonBuffer;
	size_t jsonBufferLength;
		
	yajl_gen_get_buf(y, &jsonBuffer, &jsonBufferLength);
	
	Datum_setData_size_(self, (char *)jsonBuffer, (size_t)jsonBufferLength);
	yajl_gen_clear(y);
}
Пример #11
0
void Datum_appendYajl_(Datum *self, yajl_gen y)
{
	const unsigned char *jsonBuffer;
	size_t jsonBufferLength;
		
	yajl_gen_get_buf(y, &jsonBuffer, &jsonBufferLength);
	
	Datum_appendBytes_size_(self, jsonBuffer, (size_t)jsonBufferLength);
	yajl_gen_clear(y);
}
Пример #12
0
bool
JSONGenerator::getJSON(std::string& key)
{
  VALIDATE_STATE();
  const unsigned char* buf = NULL;
  size_t len = 0;
  if (yajl_gen_get_buf(mState->mGen, &buf, &len) != yajl_gen_status_ok) {
    return false;
  }
  key = (const char*)buf;
  return true;
}
Пример #13
0
veBool pubnub_atPublishN(struct PubnubAt* nubat, char const *buf, size_t buf_len)
{
	struct PubnubRequest *nubreq;
	u8 const *json;
	size_t json_len;

	/* allocate request */
	if (!nubat->g) {
		nubat->g = yajl_gen_alloc(NULL);
		if (!nubat->g)
			return veFalse;
	}

	nubreq = (struct PubnubRequest *) ve_malloc(sizeof(struct PubnubRequest));
	if (!nubreq) {
		/* reuse the nubat->q next time */
		return veFalse;
	}

	pubnub_req_init(&nubat->nub, nubreq, 512, 512);

	/* build json data.. */
	if (yajl_gen_string(nubat->g, (u8*) buf, buf_len) != yajl_gen_status_ok) {
		ve_error("json: not a valid string");
		goto error;
	}

	if (yajl_gen_get_buf(nubat->g, &json, &json_len) != yajl_gen_status_ok) {
		ve_error("json: could not get buf");
		return veFalse;
	}

	/* sent it */
	if (pubnub_publish(nubreq, (char*) json, publish_callback) != RET_OK) {
		ve_error("could not pubnub_publish");
		goto error;
	}

	yajl_gen_clear(nubat->g);	/* empty buffers */
	yajl_gen_free(nubat->g);
	nubat->g = NULL;
	return veTrue;

error:
	pubnub_req_deinit(nubreq);
	ve_free(nubreq);
	yajl_gen_free(nubat->g);
	nubat->g = NULL;

	return veFalse;
}
Пример #14
0
void reformat(ErlDrvPort port, char* buf, int len)
{
    yajl_handle hand;
    /* generator config */
    yajl_gen_config conf = { 1, "  " };
	yajl_gen g;
    yajl_status stat;
    /* allow comments */
    yajl_parser_config cfg = { 1, 1 };

    g = yajl_gen_alloc(&conf, NULL);

    /* ok.  open file.  let's read and parse */
    hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
    
    /* read file data, pass to parser */
    stat = yajl_parse(hand, (unsigned char*) buf, len);

    if (stat != yajl_status_ok &&
        stat != yajl_status_insufficient_data)
    {
        char* err = (char*) yajl_get_error(hand, 1, (unsigned char*) buf, len);
        int len = strlen(err);

        ErlDrvTermData msg[] = {
            ERL_DRV_ATOM, driver_mk_atom("error"), 
            ERL_DRV_BUF2BINARY, (ErlDrvTermData) err, (ErlDrvUInt) len,
            ERL_DRV_TUPLE, 2
        };

        driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0]));
    } else {
        const unsigned char* json;
        unsigned int len;
        yajl_gen_get_buf(g, &json, &len);

        ErlDrvTermData msg[] = {
            ERL_DRV_ATOM, driver_mk_atom("ok"),
            ERL_DRV_BUF2BINARY, (ErlDrvTermData) json, (ErlDrvUInt) len,
            ERL_DRV_TUPLE, 2
        };

        driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0]));

        yajl_gen_clear(g);
    }

    yajl_gen_free(g);
    yajl_free(hand);
}
Пример #15
0
int main(int argc, char ** argv)
{
    yajl_handle hand;
    yajl_status stat;
    yajl_parser_config cfg = { 1, 1 };

    int done = 0;

    hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
        
    while (!done) {
        rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
        
        if (rd == 0) {
            if (!feof(stdin)) {
                fprintf(stderr, "error on file read.\n");
                break;
            }
            done = 1;
        }
        fileData[rd] = 0;
        
        if (done)
            /* parse any remaining buffered data */
            stat = yajl_parse_complete(hand);
        else
            /* read file data, pass to parser */
            stat = yajl_parse(hand, fileData, rd);

        if (stat != yajl_status_ok &&
            stat != yajl_status_insufficient_data)
        {
            unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
            fprintf(stderr, (const char *) str);
            yajl_free_error(hand, str);
        } else {
            const unsigned char * buf;
            unsigned int len;
            yajl_gen_get_buf(g, &buf, &len);
            fwrite(buf, 1, len, stdout);
            yajl_gen_clear(g);
        }
    }

    yajl_gen_free(g);
    yajl_free(hand);
    
    return 0;
}
Пример #16
0
IoObject *IoYajlGen_generate(IoYajlGen *self, IoObject *locals, IoMessage *m)
{
	const unsigned char *jsonBuffer;
	unsigned int jsonBufferLength;
		
	yajl_gen_get_buf(DATA(self), &jsonBuffer, &jsonBufferLength);
	
	IoSeq *out = IOSEQ(jsonBuffer, jsonBufferLength);
	
	yajl_gen_free(DATA(self));
	yajl_gen_config config = { 0, "" };
	IoObject_setDataPointer_(self, yajl_gen_alloc(&config, NULL));
	
    return out;  
}
Пример #17
0
char *virJSONValueToString(virJSONValuePtr object,
                           bool pretty)
{
    yajl_gen g;
    const unsigned char *str;
    char *ret = NULL;
    yajl_size_t len;
# ifndef WITH_YAJL2
    yajl_gen_config conf = { pretty ? 1 : 0, pretty ? "    " : " "};
# endif

    VIR_DEBUG("object=%p", object);

# ifdef WITH_YAJL2
    g = yajl_gen_alloc(NULL);
    if (g) {
        yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0);
        yajl_gen_config(g, yajl_gen_indent_string, pretty ? "    " : " ");
        yajl_gen_config(g, yajl_gen_validate_utf8, 1);
    }
# else
    g = yajl_gen_alloc(&conf, NULL);
# endif
    if (!g) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to create JSON formatter"));
        goto cleanup;
    }

    if (virJSONValueToStringOne(object, g) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (yajl_gen_get_buf(g, &str, &len) != yajl_gen_status_ok) {
        virReportOOMError();
        goto cleanup;
    }

    ignore_value(VIR_STRDUP(ret, (const char *)str));

cleanup:
    yajl_gen_free(g);

    VIR_DEBUG("result=%s", NULLSTR(ret));

    return ret;
}
Пример #18
0
char *virJSONValueToString(virJSONValuePtr object)
{
    yajl_gen g;
    const unsigned char *str;
    char *ret = NULL;
    yajl_size_t len;
# ifndef HAVE_YAJL2
    yajl_gen_config conf = { 0, " " }; /* Turns off pretty printing since QEMU can't cope */
# endif

    VIR_DEBUG("object=%p", object);

# ifdef HAVE_YAJL2
    g = yajl_gen_alloc(NULL);
    if (g) {
        yajl_gen_config(g, yajl_gen_beautify, 0);
        yajl_gen_config(g, yajl_gen_indent_string, " ");
        yajl_gen_config(g, yajl_gen_validate_utf8, 1);
    }
# else
    g = yajl_gen_alloc(&conf, NULL);
# endif
    if (!g) {
        virJSONError(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("Unable to create JSON formatter"));
        goto cleanup;
    }

    if (virJSONValueToStringOne(object, g) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (yajl_gen_get_buf(g, &str, &len) != yajl_gen_status_ok) {
        virReportOOMError();
        goto cleanup;
    }

    if (!(ret = strdup((const char *)str)))
        virReportOOMError();

cleanup:
    yajl_gen_free(g);

    VIR_DEBUG("result=%s", NULLSTR(ret));

    return ret;
}
Пример #19
0
IoObject *IoYajlGen_generate(IoYajlGen *self, IoObject *locals, IoMessage *m)
{
	const unsigned char *jsonBuffer;
	size_t jsonBufferLength;

	yajl_gen_get_buf(DATA(self), &jsonBuffer, &jsonBufferLength);

	IoSeq *out = IOSEQ(jsonBuffer, jsonBufferLength);

	yajl_gen_free(DATA(self));
	yajl_gen yg = yajl_gen_alloc(NULL);
	//yajl_gen_config(yg, yajl_gen_beautify, 0);
	IoObject_setDataPointer_(self, yg);

    return out;
}
Пример #20
0
static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts, VALUE json_opts) {
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
  VALUE sym_yajl_gen_beautify = ID2SYM(rb_intern("yajl_gen_beautify"));
  VALUE sym_yajl_gen_validate_utf8 = ID2SYM(rb_intern("yajl_gen_validate_utf8"));
  VALUE sym_yajl_gen_indent_string = ID2SYM(rb_intern("yajl_gen_indent_string"));
  yajl_gen yajl_gen;
  const unsigned char *buf;
  size_t len;
  VALUE state;
  VALUE ret;
  VALUE indent_string;
  VALUE rb_yajl_gen;

  yajl_gen = yajl_gen_alloc(NULL);

  if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_beautify) == Qtrue ) {
    yajl_gen_config(yajl_gen, yajl_gen_beautify, 1);
  }
  if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_validate_utf8) == Qtrue ) {
    yajl_gen_config(yajl_gen, yajl_gen_validate_utf8, 1);
  }

  indent_string = rb_hash_aref(yajl_gen_opts, sym_yajl_gen_indent_string);
  if (indent_string != Qnil) {
    yajl_gen_config(yajl_gen, yajl_gen_indent_string, RSTRING_PTR(indent_string));
  } else {
    yajl_gen_config(yajl_gen, yajl_gen_indent_string, " ");
  }

  state = rb_hash_new();

  rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse);

  rb_hash_aset(state, rb_str_new2("json_opts"), json_opts);

  rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen);

  rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state);

  yajl_gen_get_buf(yajl_gen, &buf, &len);

  ret = rb_str_new2((char *)buf);

  yajl_gen_free(yajl_gen);

  return ret;
}
Пример #21
0
/*
 * Format (prometheus/alertmanager v1):
 *
 * [{
 *   "labels": {
 *     "alertname": "collectd_cpu",
 *     "instance":  "host.example.com",
 *     "severity":  "FAILURE",
 *     "service":   "collectd",
 *     "cpu":       "0",
 *     "type":      "wait"
 *   },
 *   "annotations": {
 *     "summary": "...",
 *     // meta
 *   },
 *   "startsAt": <rfc3339 time>,
 *   "endsAt": <rfc3339 time>, // not used
 * }]
 */
int format_json_notification(char *buffer, size_t buffer_size, /* {{{ */
                             notification_t const *n) {
  yajl_gen g;
  unsigned char const *out;
#if HAVE_YAJL_V2
  size_t unused_out_len;
#else
  unsigned int unused_out_len;
#endif

  if ((buffer == NULL) || (n == NULL))
    return EINVAL;

#if HAVE_YAJL_V2
  g = yajl_gen_alloc(NULL);
  if (g == NULL)
    return -1;
#if COLLECT_DEBUG
  yajl_gen_config(g, yajl_gen_beautify, 1);
  yajl_gen_config(g, yajl_gen_validate_utf8, 1);
#endif

#else /* !HAVE_YAJL_V2 */
  yajl_gen_config conf = {0};
#if COLLECT_DEBUG
  conf.beautify = 1;
  conf.indentString = "  ";
#endif
  g = yajl_gen_alloc(&conf, NULL);
  if (g == NULL)
    return -1;
#endif

  if (format_alert(g, n) != 0) {
    yajl_gen_clear(g);
    yajl_gen_free(g);
    return -1;
  }

  /* copy to output buffer */
  yajl_gen_get_buf(g, &out, &unused_out_len);
  sstrncpy(buffer, (void *)out, buffer_size);

  yajl_gen_clear(g);
  yajl_gen_free(g);
  return 0;
} /* }}} format_json_notification */
Пример #22
0
static package
bf_generate_json(Var arglist, Byte next, void *vdata, Objid progr)
{
    yajl_gen g;
    yajl_gen_config cfg = { 0, "" };

    struct generate_context gctx;
    gctx.mode = MODE_COMMON_SUBSET;

    const char *buf;
    unsigned int len;

    Var json;

    package pack;

    if (1 < arglist.v.list[0].v.num) {
	if (!mystrcasecmp(arglist.v.list[2].v.str, "common-subset")) {
	    gctx.mode = MODE_COMMON_SUBSET;
	} else if (!mystrcasecmp(arglist.v.list[2].v.str, "embedded-types")) {
	    gctx.mode = MODE_EMBEDDED_TYPES;
	} else {
	    free_var(arglist);
	    return make_error_pack(E_INVARG);
	}
    }

    g = yajl_gen_alloc(&cfg, NULL);

    if (yajl_gen_status_ok == generate(g, arglist.v.list[1], &gctx)) {
	yajl_gen_get_buf(g, (const unsigned char **)&buf, &len);

	json.type = TYPE_STR;
	json.v.str = str_dup(buf);

	pack = make_var_pack(json);
    } else {
	pack = make_error_pack(E_INVARG);
    }

    yajl_gen_clear(g);
    yajl_gen_free(g);

    free_var(arglist);
    return pack;
}
Пример #23
0
/*
 * Document-method: encode
 *
 * call-seq: encode(obj[, io[, &block]])
 *
 * +obj+ is the Ruby object to encode to JSON
 *
 * +io+ is an optional IO used to stream the encoded JSON string to.
 * If +io+ isn't specified, this method will return the resulting JSON string. If +io+ is specified, this method returns nil
 *
 * If an optional block is passed, it's called when encoding is complete and passed the resulting JSON string
 *
 * It should be noted that you can reuse an instance of this class to continue encoding multiple JSON
 * to the same stream. Just continue calling this method, passing it the same IO object with new/different
 * ruby objects to encode. This is how streaming is accomplished.
 */
static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
    yajl_encoder_wrapper * wrapper;
    const unsigned char * buffer;
    unsigned int len;
    VALUE obj, io, blk, outBuff;

    GetEncoder(self, wrapper);

    rb_scan_args(argc, argv, "11&", &obj, &io, &blk);

    if (blk != Qnil) {
        wrapper->on_progress_callback = blk;
    }

    /* begin encode process */
    yajl_encode_part(wrapper, obj, io);

    /* just make sure we output the remaining buffer */
    yajl_gen_get_buf(wrapper->encoder, &buffer, &len);
    outBuff = rb_str_new((const char *)buffer, len);
#ifdef HAVE_RUBY_ENCODING_H
    rb_enc_associate(outBuff, utf8Encoding);
#endif
    yajl_gen_clear(wrapper->encoder);

    if (io != Qnil) {
        rb_io_write(io, outBuff);
        if (wrapper->terminator != 0 && wrapper->terminator != Qnil) {
            rb_io_write(io, wrapper->terminator);
        }
        return Qnil;
    } else if (blk != Qnil) {
        rb_funcall(blk, intern_call, 1, outBuff);
        if (wrapper->terminator != 0) {
            rb_funcall(blk, intern_call, 1, wrapper->terminator);
        }
        return Qnil;
    } else {
        if (wrapper->terminator != 0 && wrapper->terminator != Qnil) {
            rb_str_concat(outBuff, wrapper->terminator);
        }
        return outBuff;
    }
    return Qnil;
}
Пример #24
0
/* Encode an IB list into JSON */
ib_status_t ib_json_encode(
    ib_mpool_t       *mpool,
    const ib_list_t  *list,
    bool              pretty,
    char            **obuf,
    size_t           *olen)
{
    assert(mpool != NULL);
    assert(list != NULL);
    assert(obuf != NULL);

    yajl_gen handle;
    json_yajl_alloc_context_t alloc_ctx = { mpool, IB_OK };
    ib_status_t rc;
    yajl_gen_status status;
    yajl_alloc_funcs alloc_fns = {
        json_yajl_alloc,
        json_yajl_realloc,
        json_yajl_free,
        &alloc_ctx
    };

    handle = yajl_gen_alloc(&alloc_fns);
    /* Probably should validate the handle here, but it's not clear from the
     * YAJL documentation how to do that */

    /* Set pretty option */
    if (pretty) {
        int opt = yajl_gen_config(handle, yajl_gen_beautify);
        if (opt == 0) {
            return IB_EINVAL;
        }
    }

    rc = encode_list(handle, NULL, 0, list);
    if (rc != IB_OK) {
        return rc;
    }
    status = yajl_gen_get_buf(handle, (const unsigned char **)obuf, olen);
    if (status != yajl_gen_status_ok) {
        return IB_EUNKNOWN;
    }

    return rc;
}
Пример #25
0
void wankelEncoder_flush(yajl_gen g, VALUE io, int write_buffer_size) {
	VALUE rbBuffer;
    yajl_gen_status status;
    const unsigned char * buffer;
    size_t len;
	
	if (io != Qnil) {
		status = yajl_gen_get_buf(g, &buffer, &len);
		yajl_helper_check_gen_status(status);
		
		if (len >= (size_t)write_buffer_size) {
			rbBuffer = rb_str_new((const char *)buffer, len);
			rb_enc_associate(rbBuffer, rb_utf8_encoding());
			rb_io_write(io, rbBuffer);
			yajl_gen_clear(g);
		}
	}
}
Пример #26
0
TEST_F(Yajl, yajl_gen) {
	for (size_t i = 0; i < kTrialCount; i++) {
		yajl_gen g = yajl_gen_alloc(NULL);

		yajl_gen_status status = GenVal(g, root_);
		if (status != yajl_gen_status_ok) {
			std::cout << "gen error: " << status << std::endl;
			FAIL();
		}

		const unsigned char * buf;
		size_t len;
		status = yajl_gen_get_buf(g, &buf, &len);
		ASSERT_EQ(yajl_gen_status_ok, status);
		//if (i == 0)
		//	std::cout << len << std::endl;
		yajl_gen_free(g);
	}	
}
Пример #27
0
/*
 * Document-method: encode
 *
 * call-seq: encode(obj[, io])
 *
 * +obj+ is the Ruby object to encode to JSON
 *
 * +io+ is an optional IO used to stream the encoded JSON string to. If no io
 *      is specified the resulting JSON string is returned. If io is specified,
 *      this method returns nil
 */
static VALUE wankelEncoder_encode(int argc, VALUE * argv, VALUE self) {
	VALUE obj, io, options;
	yajl_gen g;
	yajl_alloc_funcs alloc_funcs;
	yajl_gen_status status;
	int write_buffer_size;
    const unsigned char * buffer;
    size_t len;

	rb_scan_args(argc, argv, "11", &obj, &io);
	options = rb_iv_get(self, "@options");
	
	alloc_funcs.malloc = yajl_helper_malloc;
	alloc_funcs.realloc = yajl_helper_realloc;
	alloc_funcs.free = yajl_helper_free;
	g = yajl_gen_alloc(&alloc_funcs);
	
	yajl_gen_configure(g, options);
	
	if (io != Qnil && !rb_respond_to(io, intern_io_write)) {
		rb_raise(e_encodeError, "output must be a an IO");
	}
	
	write_buffer_size = FIX2INT(rb_hash_aref(options, ID2SYM(rb_intern("write_buffer_size"))));
	
	yajl_encode_part(g, obj, io, write_buffer_size);
	
	// TODO: add terminator here if desired
	if (io == Qnil) {
		status = yajl_gen_get_buf(g, &buffer, &len);
		yajl_helper_check_gen_status(status);
	    io = rb_str_new((const char *)buffer, len);
		rb_enc_associate(io, rb_utf8_encoding());
		yajl_gen_clear(g);
		yajl_gen_free(g);
		return io;
	} else {
		wankelEncoder_flush(g, io, 1);
		yajl_gen_free(g);
		return Qnil;
	}	
	return self;
}
Пример #28
0
void writejson(FILE * out, char *stamp, char *line, size_t len, int fragment)
{
	yajl_gen g;

	const unsigned char *buf;
	size_t buflen;

	g = yajl_gen_alloc(NULL);

	yajl_gen_config(g, yajl_gen_beautify, 0);
	yajl_gen_config(g, yajl_gen_validate_utf8, 1);

	chk(yajl_gen_map_open(g));

	chk(yajl_gen_string(g, (const unsigned char *)"timestamp", 9));
	chk(yajl_gen_string(g, (const unsigned char *)stamp, TIMESTAMP));

	chk(yajl_gen_string(g, (const unsigned char *)"message", 7));
	chk(yajl_gen_string(g, (const unsigned char *)line, len));

	if (fragment >= 0) {
		char fragmentStr[30];

		snprintf(fragmentStr, 30, "%d", fragment);

		chk(yajl_gen_string(g, (const unsigned char *)"fragmentId", 10));
		chk(yajl_gen_number(g, fragmentStr, strlen(fragmentStr)));
	}

	chk(yajl_gen_map_close(g));

	yajl_gen_get_buf(g, &buf, &buflen);
	fwrite(buf, 1, buflen, out);
	yajl_gen_clear(g);
	yajl_gen_free(g);

	putc('\n', out);
}
Пример #29
0
void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
    VALUE str, outBuff, otherObj;
    yajl_encoder_wrapper * w = wrapper;
    yajl_gen_status status;
    int idx = 0;
    const unsigned char * buffer;
    const char * cptr;
    unsigned int len;

    if (io != Qnil || w->on_progress_callback != Qnil) {
        status = yajl_gen_get_buf(w->encoder, &buffer, &len);
        if (len >= WRITE_BUFSIZE) {
            outBuff = rb_str_new((const char *)buffer, len);
            if (io != Qnil) {
                rb_io_write(io, outBuff);
            } else if (w->on_progress_callback != Qnil) {
                rb_funcall(w->on_progress_callback, intern_call, 1, outBuff);
            }
            yajl_gen_clear(w->encoder);
        }
    }

    switch (TYPE(obj)) {
        case T_HASH:
            status = yajl_gen_map_open(w->encoder);

            /* TODO: itterate through keys in the hash */
            VALUE keys = rb_funcall(obj, intern_keys, 0);
            VALUE entry, keyStr;
            for(idx=0; idx<RARRAY_LEN(keys); idx++) {
                entry = rb_ary_entry(keys, idx);
                keyStr = rb_funcall(entry, intern_to_s, 0); /* key must be a string */
                /* the key */
                yajl_encode_part(w, keyStr, io);
                /* the value */
                yajl_encode_part(w, rb_hash_aref(obj, entry), io);
            }

            status = yajl_gen_map_close(w->encoder);
            break;
        case T_ARRAY:
            status = yajl_gen_array_open(w->encoder);
            for(idx=0; idx<RARRAY_LEN(obj); idx++) {
                otherObj = rb_ary_entry(obj, idx);
                yajl_encode_part(w, otherObj, io);
            }
            status = yajl_gen_array_close(w->encoder);
            break;
        case T_NIL:
            status = yajl_gen_null(w->encoder);
            break;
        case T_TRUE:
            status = yajl_gen_bool(w->encoder, 1);
            break;
        case T_FALSE:
            status = yajl_gen_bool(w->encoder, 0);
            break;
        case T_FIXNUM:
        case T_FLOAT:
        case T_BIGNUM:
            str = rb_funcall(obj, intern_to_s, 0);
            cptr = RSTRING_PTR(str);
            len = RSTRING_LEN(str);
            if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
                rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
            }
            status = yajl_gen_number(w->encoder, cptr, len);
            break;
        case T_STRING:
            cptr = RSTRING_PTR(obj);
            len = RSTRING_LEN(obj);
            status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
            break;
        default:
            if (rb_respond_to(obj, intern_to_json)) {
                str = rb_funcall(obj, intern_to_json, 0);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_number(w->encoder, cptr, len);
            } else {
                str = rb_funcall(obj, intern_to_s, 0);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
            }
            break;
    }
}
Пример #30
0
static void log_logstash_print (yajl_gen g, int severity,
	   	cdtime_t timestamp_time)
{
	FILE *fh;
	_Bool do_close = 0;
	struct tm timestamp_tm;
	char timestamp_str[64];
	const unsigned char *buf;
	time_t tt;
#if HAVE_YAJL_V2
	size_t len;
#else
	unsigned int len;
#endif

	if (yajl_gen_string(g, (u_char *)"@level", strlen("@level")) !=
	    yajl_gen_status_ok)
		goto err;

	switch (severity) {
	case LOG_ERR:
		if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	case LOG_WARNING:
		if (yajl_gen_string(g, (u_char *)"warning",
				    strlen("warning")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	case LOG_NOTICE:
		if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	case LOG_INFO:
		if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	case LOG_DEBUG:
		if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	default:
		if (yajl_gen_string(g, (u_char *)"unknown",
				    strlen("unknown")) !=
		    yajl_gen_status_ok)
			goto err;
		break;
	}

	if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
	    yajl_gen_status_ok)
		goto err;

	tt = CDTIME_T_TO_TIME_T (timestamp_time);
	gmtime_r (&tt, &timestamp_tm);

	/*
	 * format time as a UTC ISO 8601 compliant string
	 */
	strftime (timestamp_str, sizeof (timestamp_str),
		  "%Y-%m-%d %H:%M:%SZ", &timestamp_tm);
	timestamp_str[sizeof (timestamp_str) - 1] = '\0';

	if (yajl_gen_string(g, (u_char *)timestamp_str,
			    strlen(timestamp_str)) !=
	    yajl_gen_status_ok)
		goto err;

	if (yajl_gen_map_close(g) != yajl_gen_status_ok)
		goto err;

	if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
		goto err;
	pthread_mutex_lock (&file_lock);

	if (log_file == NULL)
	{
		fh = fopen (DEFAULT_LOGFILE, "a");
		do_close = 1;
	} else if (strcasecmp(log_file, "stdout") == 0) {
        fh = stdout;
        do_close = 0;
	} else if (strcasecmp(log_file, "stderr") == 0) {
        fh = stderr;
        do_close = 0;
	} else {
		fh = fopen (log_file, "a");
		do_close = 1;
	}

	if (fh == NULL)
	{
			char errbuf[1024];
			fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
					(log_file == NULL) ? DEFAULT_LOGFILE : log_file,
					sstrerror (errno, errbuf, sizeof (errbuf)));
	}
	else
	{
		fprintf(fh, "%s\n", buf);
		if (do_close) {
			fclose (fh);
		} else {
			fflush(fh);
		}
	}
	pthread_mutex_unlock (&file_lock);
	yajl_gen_free(g);
	return;

 err:
	yajl_gen_free(g);
	fprintf(stderr, "Could not correctly generate JSON message\n");
	return;
} /* void log_logstash_print */