Ejemplo n.º 1
0
double json_number_value(const json_t *json)
{
    if(json_is_integer(json))
        return json_integer_value(json);
    else if(json_is_real(json))
        return json_real_value(json);
    else
        return 0.0;
}
Ejemplo n.º 2
0
int json_real_set(const json_t *json, double value)
{
    if(!json_is_real(json))
        return 0;

    json_to_real(json)->value = value;

    return 0;
}
Ejemplo n.º 3
0
Value NDKHelper::getValueFromJson(json_t *obj)
{
    if (obj == NULL) {
        return Value::Null;
    }

    if (json_is_object(obj)) {
        ValueMap valueMap;

        const char *key;
        json_t *value;

        void *iter = json_object_iter(obj);
        while (iter) {
            key = json_object_iter_key(iter);
            value = json_object_iter_value(iter);

            valueMap[key] = NDKHelper::getValueFromJson(value);

            iter = json_object_iter_next(obj, iter);
        }

        return Value(valueMap);
    } else if (json_is_array(obj)) {
        ValueVector valueVector;

        size_t sizeArray = json_array_size(obj);

        for (unsigned int i = 0; i < sizeArray; i++) {
            valueVector.push_back(NDKHelper::getValueFromJson(json_array_get(obj, i)));
        }

        return Value(valueVector);
    } else if (json_is_boolean(obj)) {
        if (json_is_true(obj)) {
            return Value(true);
        } else {
            return Value(false);
        }
    } else if (json_is_integer(obj)) {
        int value = (int) json_integer_value(obj);

        return Value(value);
    } else if (json_is_real(obj)) {
        double value = json_real_value(obj);

        return Value(value);
    } else if (json_is_string(obj)) {
        std::string value = json_string_value(obj);

        return Value(value);
    }

    return Value::Null;
}
Ejemplo n.º 4
0
static void decode_int_as_real()
{
    json_t *json;
    json_error_t error;

#if JSON_INTEGER_IS_LONG_LONG
    const char *imprecise;
    json_int_t expected;
#endif

    char big[311];

    json = json_loads("42", JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
    if (!json || !json_is_real(json) || json_real_value(json) != 42.0)
        fail("json_load decode int as real failed - int");
    json_decref(json);

#if JSON_INTEGER_IS_LONG_LONG
    /* This number cannot be represented exactly by a double */
    imprecise = "9007199254740993";
    expected = 9007199254740992ll;

    json = json_loads(imprecise, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY,
                      &error);
    if (!json || !json_is_real(json) || expected != (json_int_t)json_real_value(json))
        fail("json_load decode int as real failed - expected imprecision");
    json_decref(json);
#endif

    /* 1E309 overflows. Here we create 1E309 as a decimal number, i.e.
       1000...(309 zeroes)...0. */
    big[0] = '1';
    memset(big + 1, '0', 309);
    big[310] = '\0';

    json = json_loads(big, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
    if (json || strcmp(error.text, "real number overflow") != 0 ||
        json_error_code(&error) != json_error_numeric_overflow)
        fail("json_load decode int as real failed - expected overflow");
    json_decref(json);

}
Ejemplo n.º 5
0
int jansson_to_val(pv_value_t* val, char** freeme, json_t* v) {

	val->flags = 0;

	if(json_is_object(v) || json_is_array(v)) {
		const char* value = json_dumps(v, JSON_COMPACT|JSON_PRESERVE_ORDER);
		*freeme = (char*)value;
		val->rs.s = (char*)value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_string(v)) {
		const char* value = json_string_value(v);
		val->rs.s = (char*)value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_boolean(v)) {
		val->ri = json_is_true(v) ? 0 : 1;
		val->flags = PV_TYPE_INT|PV_VAL_INT;
	}else if(json_is_real(v)) {
		char* value = NULL;
		if(asprintf(&value, "%.15g", json_real_value(v))<0) {
			ERR("asprintf failed\n");
			return -1;
		}
		*freeme = value;
		val->rs.s = value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_integer(v)) {
		long long value = json_integer_value(v);
		if ((value > INT_MAX) || (value < INT_MIN))  {
			char* svalue = NULL;
			if (asprintf(&svalue, "%"JSON_INTEGER_FORMAT, value) < 0) {
				ERR("asprintf failed\n");
				return -1;
			}
			*freeme = svalue;
			val->rs.s = svalue;
			val->rs.len = strlen(svalue);
			val->flags = PV_VAL_STR;
		} else {
			val->ri = (int)value;
			val->flags = PV_TYPE_INT|PV_VAL_INT;
		}
	}else if(json_is_null(v)) {
		val->flags = PV_VAL_NULL;
	}else {
		ERR("unrecognized json type: %d\n", json_typeof(v));
		return -1;
	}
	return 0;
}
Ejemplo n.º 6
0
//## float Json.getFloat(String key);
static KMETHOD Json_getFloat(KonohaContext *kctx, KonohaStack *sfp)
{
	json_t* obj = ((struct _kJson *)sfp[0].asObject)->obj;
	CHECK_JSON(obj, KReturnFloatValue(0.0));
	const char *key = S_text(sfp[1].asString);
	json_t* ret = json_object_get(obj, key);
	if(!json_is_real(ret)) {
		KReturnFloatValue(0.0);
	}
	ret = json_incref(ret);
	double val = json_real_value(ret);
	KReturnFloatValue(val);
}
Ejemplo n.º 7
0
/* 判断janson的类型 */
int
my_json_type (json_t * value)
{
	if (json_is_object (value)) {
		printf ("json_is_object\n");
		return JSON_OBJECT;
	}

	if (json_is_array (value)) {
		printf ("json_is_array\n");
		return JSON_ARRAY;
	}

	if (json_is_string (value)) {
		printf ("json_is_string\n");
		return JSON_STRING;
	}

	if (json_is_integer (value)) {
		printf ("json_is_integer\n");
		return JSON_INTEGER;
	}

	if (json_is_real (value)) {
		printf ("json_is_real\n");
		return JSON_REAL;
	}

	if (json_is_number (value)) {
		printf ("json_is_number\n");
	}

	if (json_is_boolean (value)) {
		printf ("json_is_boolean\n");
	}

	if (json_is_null (value)) {
		printf ("json_is_null\n");
		return JSON_NULL;
	}

	if (json_is_true (value)) {
		printf ("json_boolean(1)\n");
		return JSON_TRUE;
	}

	if (json_is_false (value)) {
		printf ("json_boolean(0)\n");
		return JSON_FALSE;
	}
}
Ejemplo n.º 8
0
json_t *json_true(void)
{
    static json_t the_true = {
        .type = JSON_TRUE,
        .refcount = (unsigned int)1
    };
    return &the_true;
}


json_t *json_false(void)
{
    static json_t the_false = {
        .type = JSON_FALSE,
        .refcount = (unsigned int)1
    };
    return &the_false;
}


json_t *json_null(void)
{
    static json_t the_null = {
        .type = JSON_NULL,
        .refcount = (unsigned int)1
    };
    return &the_null;
}


/*** deletion ***/

void json_delete(json_t *json)
{
    if(json_is_object(json))
        json_delete_object(json_to_object(json));

    else if(json_is_array(json))
        json_delete_array(json_to_array(json));

    else if(json_is_string(json))
        json_delete_string(json_to_string(json));

    else if(json_is_integer(json))
        json_delete_integer(json_to_integer(json));

    else if(json_is_real(json))
        json_delete_real(json_to_real(json));

    /* json_delete is not called for true, false or null */
}
GratingParams parse_grating_info(const json_t * const root) {
    GratingParams result;

    json_t *data_json;

    data_json = json_object_get(root, "reset_phase_position");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_boolean(data_json));
    result.reset_phase_position = json_is_true( data_json );

    data_json = json_object_get(root, "phase_position");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.phase_position = json_real_value( data_json );

    data_json = json_object_get(root, "phase_velocity");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.phase_velocity = json_real_value( data_json );

    data_json = json_object_get(root, "wavelength");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.wavelength = json_real_value( data_json );

    data_json = json_object_get(root, "contrast");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.contrast = json_real_value( data_json );

    data_json = json_object_get(root, "orientation");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.orientation = json_real_value( data_json );

    return result;
}
Ejemplo n.º 10
0
static kbool_t IsJsonType(struct JsonBuf *jsonbuf, KJSONTYPE type)
{
	switch(type) {
		case KJSON_OBJECT:   return json_is_object(jsonbuf->jsonobj);
		case KJSON_ARRAY:    return json_is_array(jsonbuf->jsonobj);
		case KJSON_STRING:   return json_is_string(jsonbuf->jsonobj);
		case KJSON_INT:      return json_is_integer(jsonbuf->jsonobj);
		case KJSON_DOUBLE:   return json_is_real(jsonbuf->jsonobj);
		case KJSON_BOOLEAN:  return json_is_boolean(jsonbuf->jsonobj);
		case KJSON_NULL:     return json_is_null(jsonbuf->jsonobj);
		case KJSON_INT64:    return json_is_integer(jsonbuf->jsonobj);
		case KJSON_LONG:     return json_is_integer(jsonbuf->jsonobj);
	}
	return false;
}
Ejemplo n.º 11
0
static size_t
write_real(json_t* json, stream_t* stream)
{
    uint8_t op = BSER_TAG_REAL;
    assert(json_is_real(json));
    double val = json_real_value(json);
    size_t bytes = 0;

    if (stream->write(stream, &op, SIZE_U8) == SIZE_U8) {
        if (stream->write(stream, &val, SIZE_DBL) == SIZE_DBL) {
            bytes = SIZE_U8 + SIZE_DBL;
        }
    }
    return bytes;
}
Ejemplo n.º 12
0
static cxConstChars jsonToConstChars(json_t *v)
{
    CX_RETURN(v == NULL, NULL);
    cxConstChars str = NULL;
    v = jsonParseRegisterValue(v);
    if(json_is_string(v)){
        str = json_string_value(v);
    }else if(json_is_integer(v)){
        str = cxConstStr("%"JSON_INTEGER_FORMAT,json_integer_value(v));
    }else if(json_is_real(v)){
        str = cxConstStr("%f",json_real_value(v));
    }else if(json_is_boolean(v)){
        str = cxConstStr("%s",json_is_true(v)?"true":"false");
    }else if(json_is_object(v)){
        str = jsonStrConvert(v);
    }
    return str;
}
Ejemplo n.º 13
0
int
jsonUnpackOoiRespFloat( json_t *responseObj, float **outFloat ) {
    float myFloat;

    if ( !json_is_real( responseObj ) ) {
        rodsLog( LOG_ERROR,
                 "jsonUnpackOoiRespInt: responseObj type %d is not JSON_REAL",
                 json_typeof( responseObj ) );
        return OOI_JSON_TYPE_ERR;
    }
    else {
        myFloat = json_real_value( responseObj );
    }

    *outFloat = ( float * ) malloc( sizeof( float ) );
    *( *outFloat ) = myFloat;
    return 0;
}
Ejemplo n.º 14
0
//## void Json.setFloat(String key, String value);
static KMETHOD Json_setFloat(KonohaContext *kctx, KonohaStack *sfp)
{
	json_t* obj = ((struct _kJson *)sfp[0].asObject)->obj;
	CHECK_JSON(obj, KReturnDefaultObjectValue());
	const char *key = S_text(sfp[1].asString);
	float fval = sfp[2].floatValue;
	json_t* val = json_real(fval);
	if(!json_is_real(val)) {
		DBG_P("[ERROR]: Value is not Json object.");
		//KLIB KonohaRuntime_raise(kctx, 1, sfp, pline, msg);
		KReturnDefaultObjectValue();
	}
	int ret = json_object_set(obj, key, val);
	if(ret < 0) {
		DBG_P("[WARNING] Json set cannnot set target object");
		KReturnDefaultObjectValue();
	}
	KReturnVoid();
}
Ejemplo n.º 15
0
void json_delete(json_t *json)
{
    if(json_is_object(json))
        json_delete_object(json_to_object(json));

    else if(json_is_array(json))
        json_delete_array(json_to_array(json));

    else if(json_is_string(json))
        json_delete_string(json_to_string(json));

    else if(json_is_integer(json))
        json_delete_integer(json_to_integer(json));

    else if(json_is_real(json))
        json_delete_real(json_to_real(json));

    /* json_delete is not called for true, false or null */
}
Ejemplo n.º 16
0
QMap<QString, QVariant> mapFromJSON(json_t *json, json_error_t *error)
{
    QMap<QString, QVariant> dict;
    void *member;
    const char *key;
    json_t *value;

    for (member = json_object_iter(json); member; member = json_object_iter_next(json, member)) {
        key = json_object_iter_key(member);
        value = json_object_iter_value(member);

        QString k = QString::fromUtf8(key);
        QVariant v;

        // json_is_object(const json_t *json)
        // json_is_array(const json_t *json)
        // json_is_string(const json_t *json)
        // json_is_integer(const json_t *json)
        // json_is_real(const json_t *json)
        // json_is_true(const json_t *json)
        // json_is_false(const json_t *json)
        // json_is_null(const json_t *json)
        if (json_is_object(value)) {
            v = mapFromJSON(json, NULL);
        } else if (json_is_array(value)) {
            v = listFromJSON(value);
        } else if (json_is_string(value)) {
            v = QString::fromUtf8(json_string_value(value));
        } else if (json_is_integer(value)) {
            v = json_integer_value(value);
        } else if (json_is_real(value)) {
            v = json_real_value(value);
        } else if (json_is_boolean(value)) {
            v = json_is_true(value);
        }

        if (v.isValid()) {
            dict[k] = v;
        }
    }
    return dict;
}
Ejemplo n.º 17
0
int read_cfg_real(json_t *root, const char *key, double *val, bool required, double default_val)
{
    json_t *node = json_object_get(root, key);
    if (!node) {
        if (required) {
            return -__LINE__;
        } else {
            *val = default_val;
            return 0;
        }
    }
    if (json_is_integer(node)) {
        *val = json_integer_value(node);
    } else if (json_is_real(node)) {
        *val = json_real_value(node);
    } else {
        return -__LINE__;
    }

    return 0;
}
Ejemplo n.º 18
0
static void
dump_perf(json_t *time)
{
    const char *key = NULL;
    bool first = true;
    json_t *val = 0;

    json_object_foreach(time, key, val) {
        int v = 0;

        if (!first)
            printf(" ");
        else
            first = false;

        if (json_is_integer(val))
            v = json_integer_value(val);
        else if (json_is_real(val))
            v = json_real_value(val) * 1000000;

        printf("%s=%d", key, v);
    }
Ejemplo n.º 19
0
/**
 * Convert JSON object to PyObject
 * @param JSONObject
 * @return PyObject
 */
static PyObject* convert(json_t *json) {
    PyObject* object = NULL;
    json_t* val;
    int result = 0;

    if (json_is_object(json)) {
        object = PyDict_New();

        char* key;
        json_object_foreach(json, key, val) {
            if (json_is_object(val) || json_is_array(val)) {
                result = PyDict_SetItemString(object, key, convert(val));
            }
            else if(json_is_string(val)) {
                const char *str = json_string_value(val);
                result = PyDict_SetItemString(object, key,
                    PyString_DecodeEscape(str, str ? strlen(str) : 0, NULL, 0, NULL));
            }
            else if(json_is_boolean(val)) {
                result = PyDict_SetItemString(object, key,
                    json_is_true(val) ? Py_True : Py_False);
            }
            else if(json_is_integer(val)) {
                result = PyDict_SetItemString(object, key,
                    PyInt_FromLong(json_integer_value(val)));
            }
            else if(json_is_real(val)) {
                result = PyDict_SetItemString(object, key,
                    PyFloat_FromDouble(json_real_value(val)));
            }
            else {
                result = PyDict_SetItemString(object, key, Py_None);
            }
            if (result == -1)
                goto failure;
        }
    }
    else if (json_is_array(json)) {
Ejemplo n.º 20
0
static int _jsonParseInt(const char *key, json_t *value)
{
    if (!value)
    {
        return 0;
    }

    if (json_is_true(value))
    {
        return 1;
    }

    if (json_is_false(value))
    {
        return 0;
    }

    if (json_is_integer(value))
    {
        return (int)json_integer_value(value);
    }

    if (json_is_real(value))
    {
        return (int)json_real_value(value);
    }

    if (json_is_string(value))
    {
        char *pEnd;
        return strtol(json_string_value(value), &pEnd, 10);
    }

    lmLog(gLoomApplicationConfigLogGroup, "WARNING: unknown json int conversion in config for key %s", key);

    return 0;
}
Ejemplo n.º 21
0
// little helpers that do conversion
static bool _jsonParseBool(const char *key, json_t *value)
{
    if (!value)
    {
        return false;
    }

    if (json_is_true(value))
    {
        return true;
    }

    if (json_is_false(value))
    {
        return false;
    }

    if (json_is_integer(value))
    {
        return json_integer_value(value) ? true : false;
    }

    if (json_is_real(value))
    {
        return json_real_value(value) ? true : false;
    }

    if (json_is_string(value))
    {
        return !stricmp(json_string_value(value), "true") ? true : false;
    }

    lmLog(gLoomApplicationConfigLogGroup, "WARNING: unknown json bool conversion in config for key %s", key);

    return false;
}
Ejemplo n.º 22
0
/* Call the simple functions not covered by other tests of the public API */
int main()
{
    json_t *value;

    value = json_integer(1);
    if(json_typeof(value) != JSON_INTEGER)
        fail("json_typeof failed");

    if(json_is_object(value))
        fail("json_is_object failed");

    if(json_is_array(value))
        fail("json_is_array failed");

    if(json_is_string(value))
        fail("json_is_string failed");

    if(!json_is_integer(value))
        fail("json_is_integer failed");

    if(json_is_real(value))
        fail("json_is_real failed");

    if(!json_is_number(value))
        fail("json_is_number failed");

    if(json_is_true(value))
        fail("json_is_true failed");

    if(json_is_false(value))
        fail("json_is_false failed");

    if(json_is_boolean(value))
        fail("json_is_boolean failed");

    if(json_is_null(value))
        fail("json_is_null failed");

    json_decref(value);


    value = json_string("foo");
    if(!value)
        fail("json_string failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set(value, "bar"))
        fail("json_string_set failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    value = json_string(NULL);
    if(value)
        fail("json_string(NULL) failed");

    /* invalid UTF-8  */
    value = json_string("a\xefz");
    if(value)
        fail("json_string(<invalid utf-8>) failed");

    value = json_string_nocheck("foo");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "bar"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    /* invalid UTF-8 */
    value = json_string_nocheck("qu\xff");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "qu\xff"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
        fail("invalid string value");

    json_decref(value);


    value = json_integer(123);
    if(!value)
        fail("json_integer failed");
    if(json_integer_value(value) != 123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.0)
        fail("invalid number value");

    if(json_integer_set(value, 321))
        fail("json_integer_set failed");
    if(json_integer_value(value) != 321)
        fail("invalid integer value");
    if(json_number_value(value) != 321.0)
        fail("invalid number value");

    json_decref(value);

    value = json_real(123.123);
    if(!value)
        fail("json_real failed");
    if(json_real_value(value) != 123.123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.123)
        fail("invalid number value");

    if(json_real_set(value, 321.321))
        fail("json_real_set failed");
    if(json_real_value(value) != 321.321)
        fail("invalid real value");
    if(json_number_value(value) != 321.321)
        fail("invalid number value");

    json_decref(value);

    value = json_true();
    if(!value)
        fail("json_true failed");
    json_decref(value);

    value = json_false();
    if(!value)
        fail("json_false failed");
    json_decref(value);

    value = json_null();
    if(!value)
        fail("json_null failed");
    json_decref(value);

    /* Test reference counting on singletons (true, false, null) */
    value = json_true();
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");

    value = json_false();
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");

    value = json_null();
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");

    return 0;
}
Ejemplo n.º 23
0
void read_ff_json(const char *json_file, struct atomgrp *ag)
{
	json_error_t json_file_error;
	json_t *base = json_load_file(json_file, 0, &json_file_error);

	if (!base) {
		fprintf(stderr,
			"error reading json file %s on line %d column %d: %s\n",
			json_file, json_file_error.line, json_file_error.column,
			json_file_error.text);
	}

	if (!json_is_object(base)) {
		fprintf(stderr, "json file not an object %s\n", json_file);
	}

	json_t *atoms, *bonds, *angles, *torsions, *impropers;
	atoms = json_object_get(base, "atoms");
	if (!json_is_array(atoms)) {
		fprintf(stderr, "json atoms are not an array %s\n", json_file);
	}
	size_t natoms = json_array_size(atoms);
	if (natoms != (size_t) ag->natoms) {
		fprintf(stderr,
			"json file has a different number of atoms %zd vs. %d : %s\n",
			natoms, ag->natoms, json_file);
	}

	ag->num_atom_types = 0;
	for (size_t i = 0; i < natoms; i++) {
		json_t *atom = json_array_get(atoms, i);
		if (!json_is_object(atom)) {
			fprintf(stderr,
				"Atom %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *ace_volume, *ftype_index, *ftype_name, *eps03;
		json_t *name, *radius03, *eps;
		json_t *charge, *radius, *element;

		ace_volume = json_object_get(atom, "ace_volume");
		if (!json_is_real(ace_volume)) {
			fprintf(stderr,
				"json ace volume is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].acevolume = json_real_value(ace_volume);

		ftype_index = json_object_get(atom, "ftype_index");
		if (!json_is_integer(ftype_index)) {
			fprintf(stderr,
				"json ftype index is not integer for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].atom_ftypen = json_integer_value(ftype_index);
		if (ag->atoms[i].atom_ftypen > ag->num_atom_types) {
			ag->num_atom_types = ag->atoms[i].atom_ftypen;
		}

		ftype_name = json_object_get(atom, "ftype_name");
		if (!json_is_string(ftype_name)) {
			fprintf(stderr,
				"json ftype name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].ftype_name = strdup(json_string_value(ftype_name));

		element = json_object_get(atom, "element");
		if (!json_is_string(element)) {
			fprintf(stderr,
				"json element name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].element = strdup(json_string_value(element));

		eps = json_object_get(atom, "eps");
		if (!json_is_real(eps)) {
			fprintf(stderr,
				"json eps is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps = sqrt(-json_real_value(eps));

		eps03 = json_object_get(atom, "eps03");
		if (!json_is_real(eps03)) {
			fprintf(stderr,
				"json eps03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps03 = sqrt(-json_real_value(eps03));

		radius = json_object_get(atom, "radius");
		if (!json_is_real(radius)) {
			fprintf(stderr,
				"json radius is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh = json_real_value(radius);

		radius03 = json_object_get(atom, "radius03");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json radius03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh03 = json_real_value(radius03);

		charge = json_object_get(atom, "charge");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json charge is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].chrg = json_real_value(charge);

		name = json_object_get(atom, "name");
		if (!json_is_string(name)) {
			fprintf(stderr,
				"json name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].name = strdup(json_string_value(name));

		ag->atoms[i].nbonds = 0;
		ag->atoms[i].nangs = 0;
		ag->atoms[i].ntors = 0;
		ag->atoms[i].nimps = 0;
		ag->atoms[i].fixed = 0;
	}

	bonds = json_object_get(base, "bonds");
	if (!json_is_array(bonds)) {
		fprintf(stderr, "json bonds are not an array %s\n", json_file);
	}
	size_t nbonds = json_array_size(bonds);
	ag->nbonds = nbonds;
	ag->bonds = _mol_calloc(nbonds, sizeof(struct atombond));
	for (size_t i = 0; i < nbonds; i++) {
		json_t *bond = json_array_get(bonds, i);
		if (!json_is_object(bond)) {
			fprintf(stderr,
				"Bond %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *length, *atom1, *atom2, *spring_constant, *sdf_type;

		atom1 = json_object_get(bond, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->bonds[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nbonds)++;

		atom2 = json_object_get(bond, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->bonds[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nbonds)++;

		length = json_object_get(bond, "length");
		if (!json_is_real(length)) {
			fprintf(stderr,
				"json length is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].l0 = json_real_value(length);

		spring_constant = json_object_get(bond, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].k = json_real_value(spring_constant);

		sdf_type = json_object_get(bond, "sdf_type");
		if (!json_is_integer(sdf_type)) {
			fprintf(stderr,
				"json sdf_type is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].sdf_type = json_integer_value(sdf_type);
	}

	angles = json_object_get(base, "angles");
	if (!json_is_array(angles)) {
		fprintf(stderr, "json angles are not an array %s\n", json_file);
	}
	size_t nangles = json_array_size(angles);
	ag->nangs = nangles;
	ag->angs = _mol_calloc(nangles, sizeof(struct atomangle));
	for (size_t i = 0; i < nangles; i++) {
		json_t *angle = json_array_get(angles, i);
		if (!json_is_object(angle)) {
			fprintf(stderr,
				"Angle %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *theta, *atom1, *atom2, *atom3, *spring_constant;

		atom1 = json_object_get(angle, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->angs[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nangs)++;

		atom2 = json_object_get(angle, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->angs[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nangs)++;

		atom3 = json_object_get(angle, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->angs[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nangs)++;

		theta = json_object_get(angle, "theta");
		if (!json_is_real(theta)) {
			fprintf(stderr,
				"json theta is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].th0 = json_real_value(theta);

		spring_constant = json_object_get(angle, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].k = json_real_value(spring_constant);
	}

	torsions = json_object_get(base, "torsions");
	if (!json_is_array(torsions)) {
		fprintf(stderr, "json torsions are not an array %s\n",
			json_file);
	}
	size_t ntorsions = json_array_size(torsions);
	ag->ntors = ntorsions;
	ag->tors = _mol_calloc(ntorsions, sizeof(struct atomtorsion));
	for (size_t i = 0; i < ntorsions; i++) {
		json_t *torsion = json_array_get(torsions, i);
		if (!json_is_object(torsion)) {
			fprintf(stderr,
				"Torsion %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *minima, *delta_constant,
		    *spring_constant;

		atom1 = json_object_get(torsion, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->tors[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].ntors)++;

		atom2 = json_object_get(torsion, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->tors[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].ntors)++;

		atom3 = json_object_get(torsion, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->tors[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].ntors)++;

		atom4 = json_object_get(torsion, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->tors[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].ntors)++;

		minima = json_object_get(torsion, "minima");
		if (!json_is_integer(minima)) {
			fprintf(stderr,
				"json minima is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].n = json_integer_value(minima);

		delta_constant = json_object_get(torsion, "delta_constant");
		if (!json_is_real(delta_constant)) {
			fprintf(stderr,
				"json delta_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].d = json_real_value(delta_constant);

		spring_constant = json_object_get(torsion, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].k = json_real_value(spring_constant);
	}

	impropers = json_object_get(base, "impropers");
	if (!json_is_array(impropers)) {
		fprintf(stderr, "json impropers are not an array %s\n",
			json_file);
	}
	size_t nimpropers = json_array_size(impropers);
	ag->nimps = nimpropers;
	ag->imps = _mol_calloc(nimpropers, sizeof(struct atomimproper));
	for (size_t i = 0; i < nimpropers; i++) {
		json_t *improper = json_array_get(impropers, i);
		if (!json_is_object(improper)) {
			fprintf(stderr,
				"Improper %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *phi, *spring_constant;

		atom1 = json_object_get(improper, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->imps[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nimps)++;

		atom2 = json_object_get(improper, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->imps[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nimps)++;

		atom3 = json_object_get(improper, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->imps[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nimps)++;

		atom4 = json_object_get(improper, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->imps[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].nimps)++;

		phi = json_object_get(improper, "phi");
		if (!json_is_real(phi)) {
			fprintf(stderr,
				"json phi is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].psi0 = json_real_value(phi);

		spring_constant = json_object_get(improper, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].k = json_real_value(spring_constant);
	}

	json_decref(base);

//allocate atom arrays of pointers to parameters
	for (size_t i = 0; i < natoms; i++) {
		int i1 = ag->atoms[i].nbonds;
		ag->atoms[i].bonds = _mol_calloc(i1, sizeof(struct atombond *));
		ag->atoms[i].nbonds = 0;
		i1 = ag->atoms[i].nangs;
		ag->atoms[i].angs = _mol_calloc(i1, sizeof(struct atomangle *));
		ag->atoms[i].nangs = 0;
		i1 = ag->atoms[i].ntors;
		ag->atoms[i].tors =
		    _mol_calloc(i1, sizeof(struct atomtorsion *));
		ag->atoms[i].ntors = 0;
		i1 = ag->atoms[i].nimps;
		ag->atoms[i].imps =
		    _mol_calloc(i1, sizeof(struct atomimproper *));
		ag->atoms[i].nimps = 0;
	}
	struct atom *atm;
//fill bonds
	for (int i = 0; i < ag->nbonds; i++) {
		atm = ag->bonds[i].a0;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
		atm = ag->bonds[i].a1;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
	}
//fill angles
	for (int i = 0; i < ag->nangs; i++) {
		atm = ag->angs[i].a0;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a1;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a2;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
	}
//fill torsions
	for (int i = 0; i < ag->ntors; i++) {
		atm = ag->tors[i].a0;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a1;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a2;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a3;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
	}
//fill impropers
	for (int i = 0; i < ag->nimps; i++) {
		atm = ag->imps[i].a0;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a1;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a2;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a3;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
	}
//atom indices in the group
	fill_ingrp(ag);

	ag->is_psf_read = true;

	// copy vals from deprecated to new data structures
	int atomsi;
	for (atomsi = 0; atomsi < ag->natoms; atomsi++) {
		_mol_atom_create_bond_indices(&ag->atoms[atomsi],
					      ag->atoms[atomsi].nbonds);
	}
	_mol_atom_group_copy_from_deprecated(ag);
}
Ejemplo n.º 24
0
struct atomgrp *read_json_ag(const char *json_file)
{
	struct atomgrp *ag =
	    (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));
	json_error_t json_file_error;
	json_t *base = json_load_file(json_file, 0, &json_file_error);

	if (!base) {
		fprintf(stderr,
			"error reading json file %s on line %d column %d: %s\n",
			json_file, json_file_error.line, json_file_error.column,
			json_file_error.text);
	}

	if (!json_is_object(base)) {
		fprintf(stderr, "json file not an object %s\n", json_file);
	}

	json_t *atoms, *bonds, *angles, *torsions, *impropers;
	atoms = json_object_get(base, "atoms");
	if (!json_is_array(atoms)) {
		fprintf(stderr, "json atoms are not an array %s\n", json_file);
	}
	size_t natoms = json_array_size(atoms);
	ag->natoms = natoms;
	ag->atoms = (struct atom *)_mol_calloc(ag->natoms, sizeof(struct atom));

	ag->num_atom_types = 0;

	char *prev_segment = _mol_calloc(1, sizeof(char));
	char *prev_residue = _mol_calloc(1, sizeof(char));
	int prev_residue_seq = -107;
	ag->nres = 0;
	int alloc_res = 250;
	ag->iares = _mol_malloc(alloc_res*sizeof(int));
	for (size_t i = 0; i < natoms; i++) {
		json_t *atom = json_array_get(atoms, i);
		if (!json_is_object(atom)) {
			fprintf(stderr,
				"Atom %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *ace_volume, *ftype_index, *ftype_name, *eps03;
		json_t *name, *radius03, *eps, *acp_type, *residue_name;
		json_t *charge, *radius, *element;
		json_t *x, *y, *z;
		json_t *yeti_type, *sybyl_type;
		json_t *backbone, *hb_acceptor, *hb_donor, *hb_weight;
		json_t *segment, *residue;


		segment = json_object_get(atom, "segment");
		residue = json_object_get(atom, "residue");
		if ((segment != NULL) && (residue != NULL)) {
			if (!json_is_string(segment)) {
				fprintf(stderr,
					"json segment is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (!json_is_string(residue)) {
				fprintf(stderr,
					"json residue is not string for atom %zd in json_file %s\n",
					i, json_file);
			}

			const char *cur_segment = json_string_value(segment);
			const char *cur_residue = json_string_value(residue);

			if (strcmp(cur_segment, prev_segment) != 0) {
				prev_residue_seq += 100;
				free(prev_segment);
				prev_segment = strdup(cur_segment);
			}
			if (strcmp(cur_residue, prev_residue) != 0) {
				int cur_residue_int = atoi(cur_residue);
				int prev_residue_int = atoi(prev_residue);
				if ((cur_residue_int - prev_residue_int) > 1) {
					prev_residue_seq +=
					    (cur_residue_int -
					     prev_residue_int);
				} else {
					prev_residue_seq += 1;
				}
				free(prev_residue);
				prev_residue = strdup(cur_residue);

				if (ag->nres +1 == alloc_res) {
					alloc_res *= 2;
					ag->iares = _mol_realloc(ag->iares, alloc_res * i);
				}
				ag->iares[ag->nres] = i;
				ag->nres++;
			}


			ag->atoms[i].comb_res_seq = prev_residue_seq;
		} else {
			ag->atoms[i].comb_res_seq = prev_residue_seq;
		}

		ace_volume = json_object_get(atom, "ace_volume");
		if (!json_is_real(ace_volume)) {
			fprintf(stderr,
				"json ace volume is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].acevolume = json_real_value(ace_volume);

		ftype_index = json_object_get(atom, "ftype_index");
		if (!json_is_integer(ftype_index)) {
			fprintf(stderr,
				"json ftype index is not integer for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].atom_ftypen = json_integer_value(ftype_index);
		if (ag->atoms[i].atom_ftypen > ag->num_atom_types) {
			ag->num_atom_types = ag->atoms[i].atom_ftypen;
		}

		ftype_name = json_object_get(atom, "ftype_name");
		if (!json_is_string(ftype_name)) {
			fprintf(stderr,
				"json ftype name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].ftype_name = strdup(json_string_value(ftype_name));

		element = json_object_get(atom, "element");
		if (!json_is_string(element)) {
			fprintf(stderr,
				"json element name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].element = strdup(json_string_value(element));

		eps = json_object_get(atom, "eps");
		if (!json_is_real(eps)) {
			fprintf(stderr,
				"json eps is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps = sqrt(-json_real_value(eps));

		eps03 = json_object_get(atom, "eps03");
		if (!json_is_real(eps03)) {
			fprintf(stderr,
				"json eps03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].eps03 = sqrt(-json_real_value(eps03));

		radius = json_object_get(atom, "radius");
		if (!json_is_real(radius)) {
			fprintf(stderr,
				"json radius is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh = json_real_value(radius);

		radius03 = json_object_get(atom, "radius03");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json radius03 is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].rminh03 = json_real_value(radius03);

		charge = json_object_get(atom, "charge");
		if (!json_is_real(radius03)) {
			fprintf(stderr,
				"json charge is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].chrg = json_real_value(charge);

		name = json_object_get(atom, "name");
		if (!json_is_string(name)) {
			fprintf(stderr,
				"json name is not string for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].name = strdup(json_string_value(name));

		residue_name = json_object_get(atom, "residue_name");
		if (residue_name != NULL) {
			if (!json_is_string(residue_name)) {
				fprintf(stderr,
					"json residue_name is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].residue_name =
			    strdup(json_string_value(residue_name));
		} else {
			ag->atoms[i].residue_name = NULL;
		}

		x = json_object_get(atom, "x");
		if (!json_is_real(x)) {
			fprintf(stderr,
				"json coordinate x is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].X = json_real_value(x);

		y = json_object_get(atom, "y");
		if (!json_is_real(y)) {
			fprintf(stderr,
				"json coordinate y is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].Y = json_real_value(y);

		z = json_object_get(atom, "z");
		if (!json_is_real(z)) {
			fprintf(stderr,
				"json coordinate z is not floating point for atom %zd in json_file %s\n",
				i, json_file);
		}
		ag->atoms[i].Z = json_real_value(z);

		acp_type = json_object_get(atom, "acp_type");
		if (acp_type != NULL) {
			if (!json_is_integer(acp_type)) {
				fprintf(stderr,
					"json acp_type index is not integer for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].acp_type = json_integer_value(acp_type);
		} else {
			ag->atoms[i].acp_type = -1;
		}

		yeti_type = json_object_get(atom, "yeti_type");
		if (yeti_type != NULL) {
			const char *yeti_type_string;
			if (!json_is_string(yeti_type)) {
				fprintf(stderr,
					"json yeti_type is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			yeti_type_string = json_string_value(yeti_type);
			if (strcmp("carbonyl", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_CARBONYL;
			} else if (strcmp("hydroxyl", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_HYDROXYL;
			} else if (strcmp("sulfonamide", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_SULFONAMIDE;
			} else if (strcmp("N5_aromatic", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_N5_AROMATIC;
			} else if (strcmp("N6_aromatic", yeti_type_string) == 0) {
				ag->atoms[i].yeti_type = MOL_YETI_N6_AROMATIC;
			} else {
				fprintf(stderr,
					"unknown json yeti_type %s for atom %zd in json_file %s\n",
					yeti_type_string, i, json_file);
				ag->atoms[i].yeti_type = MOL_YETI_NONE;
			}
		} else {
			ag->atoms[i].yeti_type = MOL_YETI_NONE;
		}

		sybyl_type = json_object_get(atom, "sybyl_type");
		if (sybyl_type != NULL) {
			const char *sybyl_type_string;
			if (!json_is_string(sybyl_type)) {
				fprintf(stderr,
					"json sybyl_type is not string for atom %zd in json_file %s\n",
					i, json_file);
			}
			sybyl_type_string = json_string_value(sybyl_type);
			ag->atoms[i].hybridization =
			    mol_hydridization_from_sybyl(sybyl_type_string);
		} else {
			ag->atoms[i].hybridization = UNKNOWN_HYBRID;
		}

		backbone = json_object_get(atom, "backbone");
		if (backbone != NULL) {
			if (!json_is_boolean(backbone)) {
				fprintf(stderr,
					"json backbone is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].backbone = json_is_true(backbone);
		} else {
			ag->atoms[i].backbone = false;
		}

		ag->atoms[i].hprop = 0;
		hb_acceptor = json_object_get(atom, "hb_acceptor");
		if (hb_acceptor != NULL) {
			if (!json_is_boolean(hb_acceptor)) {
				fprintf(stderr,
					"json hb_acceptor is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (json_is_true(hb_acceptor)) {
				ag->atoms[i].hprop |= HBOND_ACCEPTOR;
			}
		}
		hb_donor = json_object_get(atom, "hb_donor");
		if (hb_donor != NULL) {
			if (!json_is_boolean(hb_donor)) {
				fprintf(stderr,
					"json hb_donor is not boolean for atom %zd in json_file %s\n",
					i, json_file);
			}
			if (json_is_true(hb_donor)) {
				ag->atoms[i].hprop |= DONATABLE_HYDROGEN;
			}
		}
		ag->atoms[i].hbond_weight = 1.0;
		hb_weight = json_object_get(atom, "hb_weight");
		if (hb_weight != NULL) {
			if (!json_is_real(hb_weight)) {
				fprintf(stderr,
					"json hb_weight is not floating point for atom %zd in json_file %s\n",
					i, json_file);
			}
			ag->atoms[i].hbond_weight = json_real_value(hb_weight);
		}

		ag->atoms[i].nbonds = 0;
		ag->atoms[i].nangs = 0;
		ag->atoms[i].ntors = 0;
		ag->atoms[i].nimps = 0;
		ag->atoms[i].fixed = 0;
		ag->atoms[i].sa = -1;
		ag->atoms[i].base = -1;
		ag->atoms[i].base2 = -1;
	}
	ag->iares[ag->nres] = ag->natoms;
	ag->iares = _mol_realloc(ag->iares, (ag->nres+1) * sizeof(int));
	free(prev_segment);
	free(prev_residue);

	bonds = json_object_get(base, "bonds");
	if (!json_is_array(bonds)) {
		fprintf(stderr, "json bonds are not an array %s\n", json_file);
	}
	size_t nbonds = json_array_size(bonds);
	ag->nbonds = nbonds;
	ag->bonds = _mol_calloc(nbonds, sizeof(struct atombond));
	for (size_t i = 0; i < nbonds; i++) {
		json_t *bond = json_array_get(bonds, i);
		if (!json_is_object(bond)) {
			fprintf(stderr,
				"Bond %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *length, *atom1, *atom2, *spring_constant, *sdf_type;

		atom1 = json_object_get(bond, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->bonds[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nbonds)++;

		atom2 = json_object_get(bond, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for bond %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->bonds[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nbonds)++;

		length = json_object_get(bond, "length");
		if (!json_is_real(length)) {
			fprintf(stderr,
				"json length is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].l0 = json_real_value(length);

		spring_constant = json_object_get(bond, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for bond %zd in json_file %s\n",
				i, json_file);
		}
		ag->bonds[i].k = json_real_value(spring_constant);

		sdf_type = json_object_get(bond, "sdf_type");
		if (sdf_type != NULL) {
			if (!json_is_integer(sdf_type)) {
				fprintf(stderr,
					"json sdf_type is not integer for bond %zd in json_file %s\n",
					i, json_file);
			}
			ag->bonds[i].sdf_type = json_integer_value(sdf_type);
		} else {
			ag->bonds[i].sdf_type = 0;
		}
	}

	angles = json_object_get(base, "angles");
	if (!json_is_array(angles)) {
		fprintf(stderr, "json angles are not an array %s\n", json_file);
	}
	size_t nangles = json_array_size(angles);
	ag->nangs = nangles;
	ag->angs = _mol_calloc(nangles, sizeof(struct atomangle));
	for (size_t i = 0; i < nangles; i++) {
		json_t *angle = json_array_get(angles, i);
		if (!json_is_object(angle)) {
			fprintf(stderr,
				"Angle %zd not an object in json file %s\n", i,
				json_file);
		}
		json_t *theta, *atom1, *atom2, *atom3, *spring_constant;

		atom1 = json_object_get(angle, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->angs[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nangs)++;

		atom2 = json_object_get(angle, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->angs[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nangs)++;

		atom3 = json_object_get(angle, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for angle %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->angs[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nangs)++;

		theta = json_object_get(angle, "theta");
		if (!json_is_real(theta)) {
			fprintf(stderr,
				"json theta is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].th0 = json_real_value(theta);

		spring_constant = json_object_get(angle, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for angle %zd in json_file %s\n",
				i, json_file);
		}
		ag->angs[i].k = json_real_value(spring_constant);
	}

	torsions = json_object_get(base, "torsions");
	if (!json_is_array(torsions)) {
		fprintf(stderr, "json torsions are not an array %s\n",
			json_file);
	}
	size_t ntorsions = json_array_size(torsions);
	ag->ntors = ntorsions;
	ag->tors = _mol_calloc(ntorsions, sizeof(struct atomtorsion));
	for (size_t i = 0; i < ntorsions; i++) {
		json_t *torsion = json_array_get(torsions, i);
		if (!json_is_object(torsion)) {
			fprintf(stderr,
				"Torsion %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *minima, *delta_constant,
		    *spring_constant;

		atom1 = json_object_get(torsion, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->tors[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].ntors)++;

		atom2 = json_object_get(torsion, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->tors[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].ntors)++;

		atom3 = json_object_get(torsion, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->tors[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].ntors)++;

		atom4 = json_object_get(torsion, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->tors[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].ntors)++;

		minima = json_object_get(torsion, "minima");
		if (!json_is_integer(minima)) {
			fprintf(stderr,
				"json minima is not integer for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].n = json_integer_value(minima);

		delta_constant = json_object_get(torsion, "delta_constant");
		if (!json_is_real(delta_constant)) {
			fprintf(stderr,
				"json delta_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].d = json_real_value(delta_constant);

		spring_constant = json_object_get(torsion, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for torsion %zd in json_file %s\n",
				i, json_file);
		}
		ag->tors[i].k = json_real_value(spring_constant);
	}

	impropers = json_object_get(base, "impropers");
	if (!json_is_array(impropers)) {
		fprintf(stderr, "json impropers are not an array %s\n",
			json_file);
	}
	size_t nimpropers = json_array_size(impropers);
	ag->nimps = nimpropers;
	ag->imps = _mol_calloc(nimpropers, sizeof(struct atomimproper));
	for (size_t i = 0; i < nimpropers; i++) {
		json_t *improper = json_array_get(impropers, i);
		if (!json_is_object(improper)) {
			fprintf(stderr,
				"Improper %zd not an object in json file %s\n",
				i, json_file);
		}
		json_t *atom1, *atom2, *atom3, *atom4, *phi, *spring_constant;

		atom1 = json_object_get(improper, "atom1");
		if (!json_is_integer(atom1)) {
			fprintf(stderr,
				"json atom1 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i1 = json_integer_value(atom1) - 1;
		ag->imps[i].a0 = &(ag->atoms[i1]);
		(ag->atoms[i1].nimps)++;

		atom2 = json_object_get(improper, "atom2");
		if (!json_is_integer(atom2)) {
			fprintf(stderr,
				"json atom2 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i2 = json_integer_value(atom2) - 1;
		ag->imps[i].a1 = &(ag->atoms[i2]);
		(ag->atoms[i2].nimps)++;

		atom3 = json_object_get(improper, "atom3");
		if (!json_is_integer(atom3)) {
			fprintf(stderr,
				"json atom3 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i3 = json_integer_value(atom3) - 1;
		ag->imps[i].a2 = &(ag->atoms[i3]);
		(ag->atoms[i3].nimps)++;

		atom4 = json_object_get(improper, "atom4");
		if (!json_is_integer(atom4)) {
			fprintf(stderr,
				"json atom4 is not integer for improper %zd in json_file %s\n",
				i, json_file);
		}
		int i4 = json_integer_value(atom4) - 1;
		ag->imps[i].a3 = &(ag->atoms[i4]);
		(ag->atoms[i4].nimps)++;

		phi = json_object_get(improper, "phi");
		if (!json_is_real(phi)) {
			fprintf(stderr,
				"json phi is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].psi0 = json_real_value(phi);

		spring_constant = json_object_get(improper, "spring_constant");
		if (!json_is_real(spring_constant)) {
			fprintf(stderr,
				"json spring_constant is not floating point for improper %zd in json_file %s\n",
				i, json_file);
		}
		ag->imps[i].k = json_real_value(spring_constant);
	}

	json_decref(base);

//allocate atom arrays of pointers to parameters
	for (size_t i = 0; i < natoms; i++) {
		int i1 = ag->atoms[i].nbonds;
		ag->atoms[i].bonds = _mol_calloc(i1, sizeof(struct atombond *));
		ag->atoms[i].nbonds = 0;
		i1 = ag->atoms[i].nangs;
		ag->atoms[i].angs = _mol_calloc(i1, sizeof(struct atomangle *));
		ag->atoms[i].nangs = 0;
		i1 = ag->atoms[i].ntors;
		ag->atoms[i].tors =
		    _mol_calloc(i1, sizeof(struct atomtorsion *));
		ag->atoms[i].ntors = 0;
		i1 = ag->atoms[i].nimps;
		ag->atoms[i].imps =
		    _mol_calloc(i1, sizeof(struct atomimproper *));
		ag->atoms[i].nimps = 0;
	}
	struct atom *atm;
//fill bonds
	for (int i = 0; i < ag->nbonds; i++) {
		atm = ag->bonds[i].a0;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
		atm = ag->bonds[i].a1;
		atm->bonds[(atm->nbonds)++] = &(ag->bonds[i]);
	}
//fill angles
	for (int i = 0; i < ag->nangs; i++) {
		atm = ag->angs[i].a0;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a1;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
		atm = ag->angs[i].a2;
		atm->angs[(atm->nangs)++] = &(ag->angs[i]);
	}
//fill torsions
	for (int i = 0; i < ag->ntors; i++) {
		atm = ag->tors[i].a0;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a1;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a2;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
		atm = ag->tors[i].a3;
		atm->tors[(atm->ntors)++] = &(ag->tors[i]);
	}
//fill impropers
	for (int i = 0; i < ag->nimps; i++) {
		atm = ag->imps[i].a0;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a1;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a2;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
		atm = ag->imps[i].a3;
		atm->imps[(atm->nimps)++] = &(ag->imps[i]);
	}
//atom indices in the group
	fill_ingrp(ag);

	ag->is_psf_read = true;

	// copy vals from deprecated to new data structures
	int atomsi;
	for (atomsi = 0; atomsi < ag->natoms; atomsi++) {
		_mol_atom_create_bond_indices(&ag->atoms[atomsi],
					      ag->atoms[atomsi].nbonds);
	}
	_mol_atom_group_copy_from_deprecated(ag);
	return ag;
}
Ejemplo n.º 25
0
CCObject* NDKHelper::GetCCObjectFromJson(json_t *obj)
{
    if (obj == NULL)
        return NULL;
    
    if (json_is_object(obj))
    {
        CCDictionary *dictionary = new CCDictionary();
        //CCDictionary::create();
        
        const char *key;
        json_t *value;
        
        void *iter = json_object_iter(obj);
        while(iter)
        {
            key = json_object_iter_key(iter);
            value = json_object_iter_value(iter);
            
            dictionary->setObject(NDKHelper::GetCCObjectFromJson(value)->autorelease(), string(key));
            
            iter = json_object_iter_next(obj, iter);
        }
        
        return dictionary;
    }
    else if (json_is_array(obj))
    {
        size_t sizeArray = json_array_size(obj);
        CCArray *array = new CCArray();
        //CCArray::createWithCapacity(sizeArray);
        
        for (unsigned int i = 0; i < sizeArray; i++)
        {
            array->addObject(NDKHelper::GetCCObjectFromJson(json_array_get(obj, i))->autorelease());
        }
        
        return array;
    }
    else if (json_is_boolean(obj))
    {
        stringstream str;
        if (json_is_true(obj))
            str << true;
        else if (json_is_false(obj))
            str << false;
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_integer(obj))
    {
        stringstream str;
        str << json_integer_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_real(obj))
    {
        stringstream str;
        str << json_real_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_string(obj))
    {
        stringstream str;
        str << json_string_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
	else if (json_is_null(obj)) 
	{
		return new CCString("null");
	}
    
    return NULL;
}
Ejemplo n.º 26
0
static qeo_retcode_t array_to_data(json_t *elemtype, json_t *json_data, qeocore_data_t *data)
{
    qeo_retcode_t result = QEO_EINVAL;

    json_t *type = json_object_get(elemtype, KEY_QEO_TYPE_CODE); // Mandatory

    if ((NULL == type) || (!json_is_integer(type))) {
        return result;
    }

    qeo_sequence_t seq = { 0 };
    if (QEO_OK != qeocore_data_sequence_new(data, &seq, json_array_size(json_data))) {
        result = QEO_EFAIL;
        return result;
    }

    qeocore_typecode_t qeoTypeCode = (qeocore_typecode_t) json_integer_value(type);
    result = QEO_OK;
    int i;
    int numOfElem = json_array_size(json_data);
    for (i = 0; i < numOfElem; ++i) {
        json_t *elem = json_array_get(json_data, i);
        if (NULL == elem) {
            result = QEO_EFAIL;
            break;
        }

        result = QEO_EFAIL;

        switch (qeoTypeCode) {
            case QEOCORE_TYPECODE_BOOLEAN:
            {
                if (json_is_boolean(elem)) {
                    ((qeo_boolean_t *)DDS_SEQ_DATA(seq))[i] = json_is_true(elem);
                    result = QEO_OK;
                }
            }
            break;

            case QEOCORE_TYPECODE_INT8:
            {
                if (json_is_integer(elem)) {
                    json_int_t json_value = json_integer_value(elem);
                    if ((-128LL <= json_value) && (json_value <= 127LL)) {
                        ((int8_t *)DDS_SEQ_DATA(seq))[i] = (int8_t) json_value;
                        result = QEO_OK;
                    }
                }
            }
            break;

            case QEOCORE_TYPECODE_INT16:
            {
                if (json_is_integer(elem)) {
                    json_int_t json_value = json_integer_value(elem);
                    if ((-32768LL <= json_value) && (json_value <= 32767LL)) {
                        ((int16_t *)DDS_SEQ_DATA(seq))[i] = (int16_t) json_value;
                        result = QEO_OK;
                    }
                }
            }
            break;

            case QEOCORE_TYPECODE_INT32:
            {
                if (json_is_integer(elem)) {
                    json_int_t json_value = json_integer_value(elem);
                    if ((-2147483648LL <= json_value) && (json_value <= 2147483647LL)) {
                        ((int32_t *)DDS_SEQ_DATA(seq))[i] = (int32_t) json_value;
                        result = QEO_OK;
                    }
                }
            }
            break;

            case QEOCORE_TYPECODE_INT64:
            {
                if (json_is_integer(elem)) {
                    ((int64_t *)DDS_SEQ_DATA(seq))[i] = (int64_t) json_integer_value(elem);
                    result = QEO_OK;
                }

                if (json_is_string(elem)) {
                    const char  *qeo_value  = json_string_value(elem);
                    intmax_t    num         = strtoimax(qeo_value, NULL, 10);
                    ((int64_t *)DDS_SEQ_DATA(seq))[i] = (int64_t) num;
                    result = QEO_OK;
                }
            }
            break;

            case QEOCORE_TYPECODE_FLOAT32:
            {
                /*
                   *Also allow integer values, when float:0.0 is written in a webview then JSON.stringify can result into float:0
                   *meaning that the float 0.0 is transformed in an integer like 0, that's why we also need to allow integers, to be
                   *able to handle these rounding issues.
                 */
                if (json_is_real(json_data) || json_is_integer(json_data)) {
                    ((float *)DDS_SEQ_DATA(seq))[i] = (float) json_real_value(elem);
                    result = QEO_OK;
                }
            }
            break;

            case QEOCORE_TYPECODE_STRING:
            {
                if (json_is_string(elem)) {
                    ((char **)DDS_SEQ_DATA(seq))[i] = strdup(json_string_value(elem));
                    result = QEO_OK;
                }
            }
            break;

            case QEOCORE_TYPECODE_STRUCT:
            {
                if (json_is_object(elem)) {
                    json_t *item = json_object_get(elemtype, KEY_ITEM);
                    if ((NULL != item) && json_is_object(item)) {
                        qeocore_data_t *qeo_value = ((qeocore_data_t **)DDS_SEQ_DATA(seq))[i];
                        result = object_to_data(item, elem, qeo_value);
                    }
                }
            }
            break;

            case QEOCORE_TYPECODE_SEQUENCE:
            {
                if (json_is_array(elem)) {
                    json_t *items = json_object_get(elemtype, KEY_ITEMS);
                    if ((NULL != items) && json_is_object(items)) {
                        qeocore_data_t *qeo_value = ((qeocore_data_t **)DDS_SEQ_DATA(seq))[i];
                        result = array_to_data(items, elem, qeo_value);
                    }
                }
            }
            break;
            case QEOCORE_TYPECODE_ENUM:
            {
                if (!json_is_integer(json_data)){
                    qeo_log_e("Enum should be integer");
                    break;
                }
                qeo_enum_value_t enum_val = (qeo_enum_value_t) json_integer_value(json_data);
                if (is_valid_enum_value(elemtype, enum_val) == false){
                    qeo_log_e("Could not convert json to enum");
                    break;
                }
                ((qeo_enum_value_t *)DDS_SEQ_DATA(seq))[i] = enum_val;
            }
            break;
        }

        if (QEO_OK != result) {
            break;
        }
    }

    if (QEO_OK == result) {
        result = qeocore_data_sequence_set(data, &seq, 0);
    }

    return result;
}
Ejemplo n.º 27
0
static qeo_retcode_t member_to_data(json_t *member, json_t *json_data, qeocore_data_t *data)
{
    qeo_retcode_t result = QEO_EINVAL;

    json_t  *id   = json_object_get(member, KEY_ID);            // Mandatory
    json_t  *type = json_object_get(member, KEY_QEO_TYPE_CODE); // Mandatory

    if ((NULL == id) || (!json_is_integer(id)) ||
        (NULL == type) || (!json_is_integer(type))) {
        return result;
    }
    result = QEO_EFAIL;

    qeocore_member_id_t qeo_member_id = (qeocore_member_id_t) json_integer_value(id);
    qeocore_typecode_t  qeoTypeCode   = (qeocore_typecode_t) json_integer_value(type);
/*
    bool isKey = false;
    json_t *key_value = json_object_get(member, KEY_KEY);
    if ((NULL != key_value) && (json_is_true(key_value))) {
        isKey = true;
    }
 */
    switch (qeoTypeCode) {
        case QEOCORE_TYPECODE_BOOLEAN:
        {
            if (json_is_boolean(json_data)) {
                qeo_boolean_t qeo_value = (qeo_boolean_t) json_is_true(json_data);
                result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                if (QEO_OK != result) {
                    qeo_log_e("qeocore_data_set_member failed");
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_INT8:
        {
            if (json_is_integer(json_data)) {
                json_int_t json_value = json_integer_value(json_data);
                if ((-128LL <= json_value) && (json_value <= 127LL)) {
                    int8_t qeo_value = (int8_t)json_value;
                    result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                    if (QEO_OK != result) {
                        qeo_log_e("qeocore_data_set_member failed");
                    }
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_INT16:
        {
            if (json_is_integer(json_data)) {
                json_int_t json_value = json_integer_value(json_data);
                if ((-32768LL <= json_value) && (json_value <= 32767LL)) {
                    int16_t qeo_value = (int16_t)json_value;
                    result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                    if (QEO_OK != result) {
                        qeo_log_e("qeocore_data_set_member failed");
                    }
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_INT32:
        {
            if (json_is_integer(json_data)) {
                json_int_t json_value = json_integer_value(json_data);
                if ((-2147483648LL <= json_value) && (json_value <= 2147483647LL)) {
                    int32_t qeo_value = (int32_t)json_value;
                    result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_INT64:
        {
            if (json_is_integer(json_data)) {
                int64_t qeo_value = (int64_t) json_integer_value(json_data);
                result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                if (QEO_OK != result) {
                    qeo_log_e("qeocore_data_set_member failed");
                }
            }

            if (json_is_string(json_data)) {
                const char *qeo_value = json_string_value(json_data);

                intmax_t num = strtoimax(qeo_value, NULL, 10);
                result = qeocore_data_set_member(data, qeo_member_id, (int64_t *) &num);
                if (QEO_OK != result) {
                    qeo_log_e("qeocore_data_set_member failed");
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_FLOAT32:
        {
            /*
               *Also allow integer values, when float:0.0 is written in a webview then JSON.stringify can result into float:0
               *meaning that the float 0.0 is transformed in an integer like 0, that's why we also need to allow integers, to be
               *able to handle these rounding issues.
             */
            if (json_is_real(json_data) || json_is_integer(json_data)) {
                float qeo_value = (float) json_real_value(json_data);
                result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                if (QEO_OK != result) {
                    qeo_log_e("qeocore_data_set_member failed");
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_STRING:
        {
            if (json_is_string(json_data)) {
                const char *qeo_value = json_string_value(json_data);
                result = qeocore_data_set_member(data, qeo_member_id, &qeo_value);
                if (QEO_OK != result) {
                    qeo_log_e("qeocore_data_set_member failed");
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_STRUCT:
        {
            if (json_is_object(json_data)) {
                qeocore_data_t *qeo_data = NULL;
                do {
                    if (QEO_OK != qeocore_data_get_member(data, qeo_member_id, &qeo_data)) {
                        qeo_log_e("qeocore_data_get_member failed");
                        break;
                    }

                    if (NULL == qeo_data) {
                        qeo_log_e("NULL == qeo_data");
                        break;
                    }

                    json_t *item = json_object_get(member, KEY_ITEM);
                    if (NULL == item) {
                        qeo_log_e("NULL == item");
                        break;
                    }
                    if (!json_is_object(item)) {
                        qeo_log_e("item is not an object");
                        break;
                    }

                    result = object_to_data(item, json_data, qeo_data);
                    if (QEO_OK != result) {
                        qeo_log_e("object_to_data failed");
                        break;
                    }

                    result = qeocore_data_set_member(data, qeo_member_id, &qeo_data);
                    if (QEO_OK != result) {
                        qeo_log_e("qeocore_data_set_member failed: member_id:%u", qeo_member_id);
                        break;
                    }
                } while (0);

                if (NULL != qeo_data) {
                    qeocore_data_free(qeo_data);
                }
            }
        }
        break;

        case QEOCORE_TYPECODE_SEQUENCE:
        {
            if (json_is_array(json_data)) {
                qeocore_data_t *qeo_data = NULL;
                if ((QEO_OK == qeocore_data_get_member(data, qeo_member_id, &qeo_data)) && (NULL != qeo_data)) {
                    json_t *items = json_object_get(member, KEY_ITEMS);
                    if ((NULL != items) && json_is_object(items)) {
                        if (QEO_OK == array_to_data(items, json_data, qeo_data)) {
                            result = qeocore_data_set_member(data, qeo_member_id, &qeo_data);
                        }
                    }
                }
                qeocore_data_free(qeo_data);
            }
        }
        break;
        case QEOCORE_TYPECODE_ENUM:
        {
            if (!json_is_integer(json_data)){
                qeo_log_e("Enum should be integer");
                break;
            }
            qeo_enum_value_t enum_val = (qeo_enum_value_t) json_integer_value(json_data);
            if (is_valid_enum_value(member, enum_val) == false){
                qeo_log_e("Could not convert json to enum");
                break;
            }
            result = qeocore_data_set_member(data, qeo_member_id, &enum_val);
        }
        break;
    }
    return result;
}
Ejemplo n.º 28
0
double JsonReader::_readDouble(const string& name){
    auto val = json_object_get(getCurrentObject(), name.c_str());
    if (!json_is_real(val))
        throwBadType(name, "real");
    return json_real_value(val);
}
Ejemplo n.º 29
0
// Parse pools array in json config
bool parse_pool_array(json_t *obj)
{
	size_t idx;
	json_t *p, *val;

	if (!json_is_array(obj))
		return false;

	// array of objects [ {}, {} ]
	json_array_foreach(obj, idx, p)
	{
		if (!json_is_object(p))
			continue;

		for (int i = 0; i < ARRAY_SIZE(cfg_array_keys); i++)
		{
			int opt = -1;
			char *s = NULL;
			if (cfg_array_keys[i].cat != CFG_POOL)
				continue;

			val = json_object_get(p, cfg_array_keys[i].name);
			if (!val)
				continue;

			for (int k = 0; k < options_count(); k++)
			{
				const char *alias = cfg_array_keys[i].longname;
				if (alias && !strcasecmp(options[k].name, alias)) {
					opt = k;
					break;
				}
				if (!alias && !strcasecmp(options[k].name, cfg_array_keys[i].name)) {
					opt = k;
					break;
				}
			}
			if (opt == -1)
				continue;

			if (json_is_string(val)) {
				s = strdup(json_string_value(val));
				if (!s)
					continue;

				// applog(LOG_DEBUG, "pool key %s '%s'", options[opt].name, s);
				parse_arg(options[opt].val, s);
				free(s);
			} else {
				// numeric or bool
				char buf[32] = { 0 };
				double d = 0.;
				if (json_is_true(val)) d = 1.;
				else if (json_is_integer(val))
					d = 1.0 * json_integer_value(val);
				else if (json_is_real(val))
					d = json_real_value(val);
				snprintf(buf, sizeof(buf)-1, "%f", d);
				// applog(LOG_DEBUG, "pool key %s '%f'", options[opt].name, d);
				parse_arg(options[opt].val, buf);
			}
		}
	}
	return true;
}
Ejemplo n.º 30
0
bool Json::isReal() const { 
   return json_is_real(m_json) > 0;
}