Пример #1
0
static int format_json_meta(yajl_gen g, notification_meta_t *meta) /* {{{ */
{
  if (meta == NULL)
    return 0;

  JSON_ADD(g, meta->name);
  switch (meta->type) {
  case NM_TYPE_STRING:
    JSON_ADD(g, meta->nm_value.nm_string);
    break;
  case NM_TYPE_SIGNED_INT:
    JSON_ADDF(g, "%" PRIi64, meta->nm_value.nm_signed_int);
    break;
  case NM_TYPE_UNSIGNED_INT:
    JSON_ADDF(g, "%" PRIu64, meta->nm_value.nm_unsigned_int);
    break;
  case NM_TYPE_DOUBLE:
    JSON_ADDF(g, JSON_GAUGE_FORMAT, meta->nm_value.nm_double);
    break;
  case NM_TYPE_BOOLEAN:
    JSON_ADD(g, meta->nm_value.nm_boolean ? "true" : "false");
    break;
  default:
    ERROR("format_json_meta: unknown meta data type %d (name \"%s\")",
          meta->type, meta->name);
    yajl_gen_null(g);
  }

  return format_json_meta(g, meta->next);
} /* }}} int format_json_meta */
Пример #2
0
static int json_add_string(yajl_gen g, char const *str) /* {{{ */
{
  if (str == NULL)
    return (int)yajl_gen_null(g);

  return (int)yajl_gen_string(g, (unsigned char const *)str,
                              (unsigned int)strlen(str));
} /* }}} int json_add_string */
Пример #3
0
yajl_gen_status
yajl_gen_cstr(yajl_gen gen, const char * str)
{
  if (!str || str[0] == 0)
    return yajl_gen_null(gen);
  else
    return yajl_gen_string(gen, (unsigned char *)str, strlen(str));
}
Пример #4
0
yajl_gen_status GenVal(yajl_gen g, yajl_val v) {
	yajl_gen_status status;
	switch (v->type) {
	case yajl_t_string:	return yajl_gen_string(g, (unsigned char*)v->u.string, strlen(v->u.string));

	case yajl_t_number: 
		{
			char buffer[100];
			char *num = buffer;
			size_t len;
			//if (YAJL_IS_INTEGER(v)) // buggy
			if (v->u.number.flags & YAJL_NUMBER_INT_VALID)
				len = sprintf(num, "%lld", YAJL_GET_INTEGER(v));
			//else if (YAJL_IS_DOUBLE(v))	// buggy
			else if (v->u.number.flags & YAJL_NUMBER_DOUBLE_VALID)
				len = sprintf(num, "%g", YAJL_GET_DOUBLE(v));
			else {
				num = YAJL_GET_NUMBER(v);
				len = strlen(buffer);
			}
			return yajl_gen_number(g, num, len);
		}

	case yajl_t_object:
		status = yajl_gen_map_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.object.len; i++) {
			status = yajl_gen_string(g, (unsigned char *)v->u.object.keys[i], strlen(v->u.object.keys[i]));
			if (status != yajl_gen_status_ok)
				return status;
			status = GenVal(g, v->u.object.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}
		return yajl_gen_map_close(g);

	case yajl_t_array:
		status = yajl_gen_array_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.array.len; i++) {
			status = GenVal(g, v->u.array.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}

		return yajl_gen_array_close(g);

	case yajl_t_true: return yajl_gen_bool(g, 1);
	case yajl_t_false: return yajl_gen_bool(g, 0);
	case yajl_t_null: return yajl_gen_null(g);
	case yajl_t_any: break;
	}
	return yajl_gen_in_error_state;
}
Пример #5
0
static VALUE rb_cNilClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
    yajl_gen_status status;
    struct yajl_gen_t *yajl_gen;
    Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
    CHECK_STATUS(
        yajl_gen_null(yajl_gen)
    );
    return Qnil;
}
Пример #6
0
/* encode a json null */
VALUE gen_null(VALUE rb_yajl_gen) {
  yajl_gen_status status;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ((status = yajl_gen_null(yajl_gen)) != yajl_gen_status_ok) {
    rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("null"));
  }

  return Qnil;
}
Пример #7
0
int PNode_op_values(PNode *self, Datum *d)
{	
	PQuery *q = PNode_startQuery(self);
	PNode *tmpNode = PDB_allocNode(self->pdb);
	Datum *k;
	Datum *attribute = PQuery_attribute(q);
	int hasAttribute = Datum_size(attribute) != 0;
	
	yajl_gen_array_open(self->yajl);
	
	while (k = PQuery_key(q))
	{		
		if(!Datum_beginsWithCString_(k, "_"))
		{
			PNode_setPid_(tmpNode, PNode_value(self));
			
			if(hasAttribute)
			{
				//Datum *a = PQuery_attributeValue(q);
				Datum *a = PNode_at_(tmpNode, attribute);
				
				yajl_gen_array_open(self->yajl);
				yajl_gen_datum(self->yajl, k);
				
				if(a)
				{
					yajl_gen_datum(self->yajl, a);
				}
				else 
				{
					yajl_gen_null(self->yajl);
				}
				
				yajl_gen_array_close(self->yajl);

			}
			else 
			{
				PNode_setPid_(tmpNode, PNode_value(self));
				PNode_op_object(tmpNode, 0x0);
			}
		}
		else
		{
				yajl_gen_datum(self->yajl, PNode_value(self));
		}

		PQuery_enumerate(q);
	}
	
	yajl_gen_array_close(self->yajl);
	Datum_appendYajl_(d, self->yajl);
	return 0; 
}
static int reformat_null(void* ctx) {
	ccJSONContext* jc = (ccJSONContext*)ctx;

	bool array = *(jc->flagStack->rbegin());
	if(array) {
		CCJSONArray* p = (CCJSONArray*)(*(jc->objStack->rbegin()));
		p->addNull();
	} else {
		CCJSONObject* p = (CCJSONObject*)(*(jc->objStack->rbegin()));
		p->addNull(jc->key);
	}

    return yajl_gen_status_ok == yajl_gen_null(jc->g);
}
Пример #9
0
yajl_gen_status
yajl_gen_value(yajl_gen gen, VALUE obj)
{
  if (FIXNUM_P(obj))
    return yajl_gen_integer(gen, NUM2LONG(obj));
  else if (NIL_P(obj) || obj == Qundef)
    return yajl_gen_null(gen);
  else if (obj == Qtrue)
    return yajl_gen_bool(gen, 1);
  else if (obj == Qfalse)
    return yajl_gen_bool(gen, 0);
  else if (SYMBOL_P(obj))
    return yajl_gen_format(gen, ":%s", rb_id2name(SYM2ID(obj)));
  else
    return yajl_gen_format(gen, "0x%x", obj);
}
Пример #10
0
int orderly_write_json2(yajl_gen g, const orderly_json * j)
{
    yajl_gen_status s;
    int rv = 1;

    if (j) {
        if (j->k) yajl_gen_string(g, (const unsigned char *) j->k, strlen(j->k));

        switch (j->t) {
            case orderly_json_none:
                return 0;
            case orderly_json_null:
                s = yajl_gen_null(g);
                break;
            case orderly_json_string:
                s = yajl_gen_string(g, (const unsigned char *) j->v.s, strlen(j->v.s));
                break;
            case orderly_json_boolean:
                s = yajl_gen_bool(g, j->v.b);
                break;
            case orderly_json_integer:
                s = yajl_gen_integer(g, j->v.i);
                break;
            case orderly_json_number:
                s = yajl_gen_double(g, j->v.n);
                break;
            case orderly_json_object:
                s = yajl_gen_map_open(g);
                rv = orderly_write_json2(g, j->v.children.first);
                s = yajl_gen_map_close(g);
                break;
            case orderly_json_array:
                s = yajl_gen_array_open(g);
                rv = orderly_write_json2(g, j->v.children.first);
                s = yajl_gen_array_close(g);
                break;
        }

        if (rv && j->next) rv = orderly_write_json2(g, j->next);
    }

    return rv;
}
Пример #11
0
int VertexServer_api_read(VertexServer *self)
{
	PNode *node = PDB_allocNode(self->pdb);
	Datum *key = HttpRequest_queryValue_(self->httpRequest, "key");	
	Datum *value;
	
	if (VertexServer_api_setCursorPathOnNode_(self, node)) return 2;

	value = PNode_at_(node, key);

	if (value) 
	{
		yajl_gen_datum(self->yajl, value);
	}
	else
	{
		yajl_gen_null(self->yajl);
	}
	
	Datum_appendYajl_(self->result, self->yajl);
	return 0;
}
Пример #12
0
int
enc_atom(Encoder* enc, ERL_NIF_TERM atom)
{
    int ret = ERROR;
    char buf[512];
    
    if(!enif_get_atom_compat(enc->env, atom, buf, 512))
    {
        enc->error = enif_make_atom(enc->env, "internal_atom_error");
        return ERROR;
    }
    
    if(strncmp(buf, "null", MIN(strlen(buf), 4)) == 0)
    {
        if(yajl_gen_null(enc->handle) != yajl_gen_status_ok) goto done;
    }
    else if(strncmp(buf, "true", MIN(strlen(buf), 4)) == 0)
    {
        if(yajl_gen_bool(enc->handle, 1) != yajl_gen_status_ok) goto done;
    }
    else if(strncmp(buf, "false", MIN(strlen(buf), 5)) == 0)
    {
        if(yajl_gen_bool(enc->handle, 0) != yajl_gen_status_ok) goto done;
    }
    else
    {
        if(yajl_gen_string(enc->handle, (unsigned char*) buf, strlen(buf))
                != yajl_gen_status_ok)
        {
            goto done;
        }
    }
    
    ret = OK;

done:
    return ret;
}
Пример #13
0
int VertexServer_api_queuePopTo(VertexServer *self)
{
	PNode *fromNode = PDB_allocNode(self->pdb);
	PNode *toNode   = PDB_allocNode(self->pdb);
	Datum *toPath   = HttpRequest_queryValue_(self->httpRequest, "toPath");
	
	long ttl = Datum_asLong(HttpRequest_queryValue_(self->httpRequest, "ttl"));
	
	if (PNode_moveToPathIfExists_(fromNode, HttpRequest_uriPath(self->httpRequest)) != 0) 
	{
		VertexServer_setErrorCString_(self, "from path does not exist: ");
		VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest));
		return -1;
	}

	PNode_moveToPath_(toNode, toPath);
	
	//printf("to   pid: %s\n", Datum_data(PNode_pid(toNode)));
	//printf("from pid: %s\n", Datum_data(PNode_pid(fromNode)));
	
	{
		PQuery *q = PNode_query(fromNode);
		VertexServer_setupPQuery_(self, q);
		PNode_startQuery(fromNode);
	
		Datum *k = PQuery_key(q);
		Datum *v = PNode_value(fromNode);
		
		if (k)
		{
			PNode_atPut_(toNode, k, v);
			PNode_moveToKey_(toNode, k);

			// insert queue time
			{
				long now = time(NULL);
				
				Datum *timeKey   = Datum_poolNewWithCString_("_qtime");
				Datum *timeValue = Datum_poolNew();
				
				Datum_fromLong_(timeValue, now);
				PNode_atPut_(toNode, timeKey, timeValue);
				
				Datum_setCString_(timeKey, "_qexpire");
				Datum_fromLong_(timeValue, now + (ttl == 0 ? 3600 : ttl));
				PNode_atPut_(toNode, timeKey, timeValue);
			}
			
			//printf("queueing key %s\n", Datum_data(k));
			yajl_gen_datum(self->yajl, k);
			PNode_removeAt_(fromNode, k);
		}
		else
		{
			yajl_gen_null(self->yajl);
		}
	}
	
	Datum_appendYajl_(self->result, self->yajl);
	
	return 0;
}
Пример #14
0
void yajl_encode_part(yajl_gen g, VALUE obj, VALUE io, int write_buffer_size) {
    size_t len;
	int idx = 0;
    VALUE keys, entry, str;
    const char * cptr;
	yajl_gen_status status;

    switch (TYPE(obj)) {
        case T_HASH:
            status = yajl_gen_map_open(g);
			yajl_helper_check_gen_status(status);
			
            keys = rb_funcall(obj, intern_keys, 0);
            for(idx = 0; idx < RARRAY_LEN(keys); idx++) {
               	entry = rb_ary_entry(keys, idx);
                str = rb_funcall(entry, intern_to_s, 0); /* key must be a string */
                /* the key */
                yajl_encode_part(g, str, io, write_buffer_size);
                /* the value */
                yajl_encode_part(g, rb_hash_aref(obj, entry), io, write_buffer_size);
            }

            status = yajl_gen_map_close(g);
			yajl_helper_check_gen_status(status);
            break;
        case T_ARRAY:
            status = yajl_gen_array_open(g);
			yajl_helper_check_gen_status(status);
			
            for(idx = 0; idx < RARRAY_LEN(obj); idx++) {
                yajl_encode_part(g, rb_ary_entry(obj, idx), io, write_buffer_size);
            }
            status = yajl_gen_array_close(g);
			yajl_helper_check_gen_status(status);
            break;
        case T_NIL:
            status = yajl_gen_null(g);
			yajl_helper_check_gen_status(status);
            break;
        case T_TRUE:
            status = yajl_gen_bool(g, 1);
			yajl_helper_check_gen_status(status);
            break;
        case T_FALSE:
            status = yajl_gen_bool(g, 0);
			yajl_helper_check_gen_status(status);
            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(e_encodeError, "'%s' is an invalid number", cptr);
            }
            status = yajl_gen_number(g, cptr, len);
			yajl_helper_check_gen_status(status);
            break;
        case T_STRING:
            cptr = RSTRING_PTR(obj);
            len = RSTRING_LEN(obj);
            status = yajl_gen_string(g, (const unsigned char *)cptr, len);
			yajl_helper_check_gen_status(status);
            break;
        default:
            if (rb_respond_to(obj, intern_to_json)) {
                str = rb_funcall(obj, intern_to_json, 0);
                Check_Type(str, T_STRING);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_number(g, cptr, len);
				yajl_helper_check_gen_status(status);
            } else {
                str = rb_funcall(obj, intern_to_s, 0);
                Check_Type(str, T_STRING);
                cptr = RSTRING_PTR(str);
                len = RSTRING_LEN(str);
                status = yajl_gen_string(g, (const unsigned char *)cptr, len);
				yajl_helper_check_gen_status(status);
            }
            break;
    }
	
	wankelEncoder_flush(g, io, write_buffer_size);
}
Пример #15
0
static int reformat_null(void * ctx)
{
    yajl_gen g = (yajl_gen) ctx;
    GEN_AND_RETURN(yajl_gen_null(g));
}
Пример #16
0
static yajl_gen_status ProcessObject(_YajlEncoder *self, PyObject *object)
{
    yajl_gen handle = (yajl_gen)(self->_generator);
    yajl_gen_status status = yajl_gen_in_error_state;
    PyObject *iterator, *item;
    unsigned short int decref = 0;

    if (object == Py_None) {
        return yajl_gen_null(handle);
    }
    if (object == Py_True) {
        return yajl_gen_bool(handle, 1);
    }
    if (object == Py_False) {
        return yajl_gen_bool(handle, 0);
    }
    if (PyUnicode_Check(object)) {
        object = PyUnicode_AsUTF8String(object);
        decref = 1;
    }
    if (PyString_Check(object)) {
        const unsigned char *buffer = NULL;
        Py_ssize_t length;
        PyString_AsStringAndSize(object, (char **)&buffer, &length);
        status = yajl_gen_string(handle, buffer, (unsigned int)(length));
        if (decref) {
            Py_XDECREF(object);
        }
        return status;
    }
    if (PyInt_Check(object)) {
        return yajl_gen_integer(handle, PyInt_AsLong(object));
    }
    if (PyLong_Check(object)) {
        return yajl_gen_integer(handle, PyLong_AsLong(object));
    }
    if (PyFloat_Check(object)) {
        return yajl_gen_double(handle, PyFloat_AsDouble(object));
    }
    if (PyList_Check(object)) {
        /*
         * Recurse and handle the list 
         */
        iterator = PyObject_GetIter(object);
        if (iterator == NULL)
            goto exit;
        status = yajl_gen_array_open(handle);
        while ((item = PyIter_Next(iterator))) {
            status = ProcessObject(self, item);
            Py_XDECREF(item);
        }
        Py_XDECREF(iterator);
        status = yajl_gen_array_close(handle);
        return status;
    }
    if (PyDict_Check(object)) {
        PyObject *key, *value;
        Py_ssize_t position = 0;

        status = yajl_gen_map_open(handle);
        while (PyDict_Next(object, &position, &key, &value)) {
            status = ProcessObject(self, key);
            status = ProcessObject(self, value);
        }
        return yajl_gen_map_close(handle);
    }

        
    exit:
        return yajl_gen_in_error_state;
}
Пример #17
0
static int reformat_null(void * ctx)
{
    yajl_gen g = (yajl_gen) ctx;
    return yajl_gen_status_ok == yajl_gen_null(g);
}
Пример #18
0
static int reformat_null(void * ctx)
{
    yajl_gen g = (yajl_gen) ctx;
    yajl_gen_null(g);
    return 1;
}
Пример #19
0
static int js_generator_null(lua_State *L) {
    js_generator_assert(L,
                        yajl_gen_null(*js_check_generator(L, 1)),
                        __FILE__, __LINE__);
    return 0;
}
Пример #20
0
int LKStreamTranslator::handle_null(void *ctx) {
	return yajl_gen_null(static_cast<LKStreamTranslator::ctx_t*>(ctx)->gen) == yajl_gen_status_ok;
}
Пример #21
0
static int lyajl_gen_null (lua_State *L) {
  luvit_generator_t *generator = generator_get(L, 1);
  yajl_gen_null(generator->gen);
  return 0;
}
Пример #22
0
static yajl_gen_status ProcessObject(_YajlEncoder *self, PyObject *object)
{
    yajl_gen handle = (yajl_gen)(self->_generator);
    yajl_gen_status status = yajl_gen_in_error_state;
    PyObject *iterator, *item;

    if (object == Py_None) {
        return yajl_gen_null(handle);
    }
    if (object == Py_True) {
        return yajl_gen_bool(handle, 1);
    }
    if (object == Py_False) {
        return yajl_gen_bool(handle, 0);
    }
    if (PyUnicode_Check(object)) {
        Py_ssize_t length = PyUnicode_GET_SIZE(object);
        Py_UNICODE *raw_unicode = PyUnicode_AS_UNICODE(object);
        /*
         * Create a buffer with enough space for code-points, preceeding and
         * following quotes and a null termination character
         */
        char *buffer = (char *)(malloc(sizeof(char) * (1 + length * 6)));
        unsigned int offset = 0;

        while (length-- > 0) {
            Py_UNICODE ch = *raw_unicode++;

            /* Escape escape characters */
            switch (ch) {
                case '\t':
                    buffer[offset++] = '\\';
                    buffer[offset++] = 't';
                    continue;
                    break;
                case '\n':
                    buffer[offset++] = '\\';
                    buffer[offset++] = 'n';
                    continue;
                    break;
                case '\r':
                    buffer[offset++] = '\\';
                    buffer[offset++] = 'r';
                    continue;
                    break;
                case '\f':
                    buffer[offset++] = '\\';
                    buffer[offset++] = 'f';
                    continue;
                    break;
                case '\b':
                    buffer[offset++] = '\\';
                    buffer[offset++] = 'b';
                    continue;
                    break;
                case '\\':
                    buffer[offset++] = '\\';
                    buffer[offset++] = '\\';
                    continue;
                    break;
                case '\"':
                    buffer[offset++] = '\\';
                    buffer[offset++] = '\"';
                    continue;
                    break;
                default:
                    break;
            }

            /* Map 16-bit characters to '\uxxxx' */
            if (ch >= 256) {
                buffer[offset++] = '\\';
                buffer[offset++] = 'u';
                buffer[offset++] = hexdigit[(ch >> 12) & 0x000F];
                buffer[offset++] = hexdigit[(ch >> 8) & 0x000F];
                buffer[offset++] = hexdigit[(ch >> 4) & 0x000F];
                buffer[offset++] = hexdigit[ch & 0x000F];
                continue;
            }

            /* Map non-printable US ASCII to '\u00hh' */
            if ( (ch < 0x20) || (ch >= 0x7F) ) {
                buffer[offset++] = '\\';
                buffer[offset++] = 'u';
                buffer[offset++] = '0';
                buffer[offset++] = '0';
                buffer[offset++] = hexdigit[(ch >> 4) & 0x0F];
                buffer[offset++] = hexdigit[ch & 0x0F];
                continue;
            }

            /* Handle proper ascii chars */
            if ( (ch >= 0x20) && (ch < 0x7F) ) {
                buffer[offset++] = (char)(ch);
                continue;
            }
        }
Пример #23
0
static int gen_jansson_value(yajl_gen gen, json_t *value) {
	json_error_t jerr;
	const char *str;
	size_t len;
	int rc;

	int type = json_typeof(value);
	switch(type) {
	case JSON_OBJECT:
		yajl_gen_map_open(gen);
		gen_jansson_object(gen,value);
		yajl_gen_map_close(gen);
		break;

	case JSON_ARRAY:
		yajl_gen_array_open(gen);
		gen_jansson_array(gen,value);
		yajl_gen_array_close(gen);
		break;

	case JSON_STRING:
		rc = json_unpack_ex(value, &jerr, 0, "s%", &str,&len);
		if(rc != 0) {
			rdlog(LOG_ERR,"Couldn't extract string: %s",jerr.text);
			return 0;
		}
		yajl_gen_string(gen, (const unsigned char *)str, len);
		break;

	case JSON_INTEGER:
		{
			json_int_t i = json_integer_value(value);
			yajl_gen_integer(gen,i);
		}
		break;

	case JSON_REAL:
		{
			double d = json_number_value(value);
			yajl_gen_double(gen,d);
		}
		break;

	case JSON_TRUE:
		yajl_gen_bool(gen,1);
		break;

	case JSON_FALSE:
		yajl_gen_bool(gen,0);
		break;

	case JSON_NULL:
		yajl_gen_null(gen);
		break;

	default:
		rdlog(LOG_ERR,"Unkown jansson type %d",type);
		break;
	};

	return 1;
}
Пример #24
0
static int virJSONValueToStringOne(virJSONValuePtr object,
                                   yajl_gen g)
{
    int i;

    VIR_DEBUG("object=%p type=%d gen=%p", object, object->type, g);

    switch (object->type) {
    case VIR_JSON_TYPE_OBJECT:
        if (yajl_gen_map_open(g) != yajl_gen_status_ok)
            return -1;
        for (i = 0; i < object->data.object.npairs ; i++) {
            if (yajl_gen_string(g,
                                (unsigned char *)object->data.object.pairs[i].key,
                                strlen(object->data.object.pairs[i].key))
                                != yajl_gen_status_ok)
                return -1;
            if (virJSONValueToStringOne(object->data.object.pairs[i].value, g) < 0)
                return -1;
        }
        if (yajl_gen_map_close(g) != yajl_gen_status_ok)
            return -1;
        break;
    case VIR_JSON_TYPE_ARRAY:
        if (yajl_gen_array_open(g) != yajl_gen_status_ok)
            return -1;
        for (i = 0; i < object->data.array.nvalues ; i++) {
            if (virJSONValueToStringOne(object->data.array.values[i], g) < 0)
                return -1;
        }
        if (yajl_gen_array_close(g) != yajl_gen_status_ok)
            return -1;
        break;

    case VIR_JSON_TYPE_STRING:
        if (yajl_gen_string(g, (unsigned char *)object->data.string,
                            strlen(object->data.string)) != yajl_gen_status_ok)
            return -1;
        break;

    case VIR_JSON_TYPE_NUMBER:
        if (yajl_gen_number(g, object->data.number,
                            strlen(object->data.number)) != yajl_gen_status_ok)
            return -1;
        break;

    case VIR_JSON_TYPE_BOOLEAN:
        if (yajl_gen_bool(g, object->data.boolean) != yajl_gen_status_ok)
            return -1;
        break;

    case VIR_JSON_TYPE_NULL:
        if (yajl_gen_null(g) != yajl_gen_status_ok)
            return -1;
        break;

    default:
        return -1;
    }

    return 0;
}
Пример #25
0
nx_string_t *nx_logdata_to_json(nx_json_parser_ctx_t *ctx)
{

    const unsigned char *json;
    size_t jsonlen;
    yajl_gen gen;
    nx_logdata_field_t *field;
    nx_string_t *retval;
    char *value;

    gen = yajl_gen_alloc(NULL);
    yajl_gen_map_open(gen);

    for ( field = NX_DLIST_FIRST(&(ctx->logdata->fields));
	  field != NULL;
	  field = NX_DLIST_NEXT(field, link) )
    {
	if ( strcmp(field->key, "raw_event") == 0 )
	{
	    continue;
	}
	if ( (field->key[0] == '.') || (field->key[0] == '_') )
	{
	    continue;
	}

	ASSERT(yajl_gen_string(gen, (const unsigned char *) field->key,
			       strlen(field->key)) == yajl_gen_status_ok);
	if ( field->value->defined == FALSE )
	{
	    ASSERT(yajl_gen_null(gen) == yajl_gen_status_ok);
	}
	else
	{
	    switch ( field->value->type )
	    {
		case NX_VALUE_TYPE_BOOLEAN:
		    ASSERT(yajl_gen_bool(gen, (int) field->value->boolean) == yajl_gen_status_ok);
		    break;
		case NX_VALUE_TYPE_INTEGER:
		    ASSERT(yajl_gen_integer(gen, (long long) field->value->integer) == yajl_gen_status_ok);
		    break;
		case NX_VALUE_TYPE_STRING:
		    ASSERT(yajl_gen_string(gen, (const unsigned char *) field->value->string->buf,
					   field->value->string->len) == yajl_gen_status_ok);
		    break;
		default:
		    value = nx_value_to_string(field->value);
		    ASSERT(yajl_gen_string(gen, (const unsigned char *) value,
					   strlen(value)) == yajl_gen_status_ok);
		    free(value);
		    break;
	    }
	}
    }
    yajl_gen_map_close(gen);
    yajl_gen_get_buf(gen, &json, &jsonlen);
    
    retval = nx_string_create((const char *) json, (int) jsonlen);
    yajl_gen_free(gen);

    return ( retval );
}
Пример #26
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;
    }
}
Пример #27
0
static bool element(mpack_reader_t* reader, yajl_gen gen, options_t* options, int depth) {
    const mpack_tag_t tag = mpack_read_tag(reader);
    if (mpack_reader_error(reader) != mpack_ok)
        return false;

    if (!options->debug && depth == 0 && (tag.type != mpack_type_map && tag.type != mpack_type_array)) {
        fprintf(stderr, "%s: Top-level object must be a map or array. Try debug viewing mode (-d)\n", options->command);
        return false;
    }

    // TODO check not depth zero
    switch (tag.type) {
        case mpack_type_bool:   return yajl_gen_bool(gen, tag.v.b) == yajl_gen_status_ok;
        case mpack_type_nil:    return yajl_gen_null(gen) == yajl_gen_status_ok;
        case mpack_type_int:    return yajl_gen_integer(gen, tag.v.i) == yajl_gen_status_ok;
        case mpack_type_float:  return yajl_gen_double(gen, tag.v.f) == yajl_gen_status_ok;
        case mpack_type_double: return yajl_gen_double(gen, tag.v.d) == yajl_gen_status_ok;

        case mpack_type_uint:
            if (tag.v.u > (uint64_t)INT64_MAX) {
                char buf[32];
                snprintf(buf, sizeof(buf), "%" PRIu64, tag.v.u);
                return yajl_gen_string(gen, (const unsigned char*)buf, strlen(buf)) == yajl_gen_status_ok;
            }
            return yajl_gen_integer(gen, (int64_t)tag.v.u) == yajl_gen_status_ok;

        case mpack_type_str:
            return string(reader, gen, options, tag.v.l);

        case mpack_type_bin:
            if (options->base64) {
                return base64_bin(reader, gen, options, tag.v.l, options->base64_prefix);
            } else if (options->debug) {
                mpack_skip_bytes(reader, tag.v.l);
                mpack_done_bin(reader);

                // output nothing to allow us to print our debug string
                skip_quotes = true;
                if (yajl_gen_string(gen, (const unsigned char*)"", 0) != yajl_gen_status_ok)
                    return false;
                skip_quotes = false;

                char buf[64];
                snprintf(buf, sizeof(buf), "<bin of size %u>", tag.v.l);
                print(out_file, buf, strlen(buf));
                return true;
            } else {
                fprintf(stderr, "%s: bin unencodable in JSON. Try debug viewing mode (-d)\n", options->command);
                return false;
            }

        case mpack_type_ext:
            if (options->base64) {
                return base64_ext(reader, gen, options, tag.exttype, tag.v.l);
            } else if (options->debug) {
                mpack_skip_bytes(reader, tag.v.l);
                mpack_done_ext(reader);

                // output nothing to allow us to print our debug string
                skip_quotes = true;
                if (yajl_gen_string(gen, (const unsigned char*)"", 0) != yajl_gen_status_ok)
                    return false;
                skip_quotes = false;

                char buf[64];
                snprintf(buf, sizeof(buf), "<ext of type %i size %u>", tag.exttype, tag.v.l);
                print(out_file, buf, strlen(buf));
                return true;
            } else {
                fprintf(stderr, "%s: ext type %i unencodable in JSON. Try debug viewing mode (-d)\n", options->command, tag.exttype);
                return false;
            }

        case mpack_type_array:
            if (yajl_gen_array_open(gen) != yajl_gen_status_ok)
                return false;
            for (size_t i = 0; i < tag.v.l; ++i)
                if (!element(reader, gen, options, depth + 1))
                    return false;
            mpack_done_array(reader);
            return yajl_gen_array_close(gen) == yajl_gen_status_ok;

        case mpack_type_map:
            if (yajl_gen_map_open(gen) != yajl_gen_status_ok)
                return false;
            for (size_t i = 0; i < tag.v.l; ++i) {

                if (options->debug) {
                    element(reader, gen, options, depth + 1);
                } else {
                    uint32_t len = mpack_expect_str(reader);
                    if (mpack_reader_error(reader) != mpack_ok) {
                        fprintf(stderr, "%s: map key is not a string. Try debug viewing mode (-d)\n", options->command);
                        return false;
                    }
                    if (!string(reader, gen, options, len))
                        return false;
                }

                if (!element(reader, gen, options, depth + 1))
                    return false;
            }
            mpack_done_map(reader);
            return yajl_gen_map_close(gen) == yajl_gen_status_ok;
    }

    return true;
}
Пример #28
0
IoObject *IoYajlGen_pushNull(IoYajlGen *self, IoObject *locals, IoMessage *m)
{
	yajl_gen_null(DATA(self));
	return self;
}
Пример #29
0
void json_from_bson_null(yajl_gen *g)
{
    yajl_gen_null( *g );
}