コード例 #1
0
ファイル: rlist.c プロジェクト: nickanderson/core
static void RlistAppendContainerPrimitive(Rlist **list, const JsonElement *primitive)
{
    assert(JsonGetElementType(primitive) == JSON_ELEMENT_TYPE_PRIMITIVE);

    switch (JsonGetPrimitiveType(primitive))
    {
    case JSON_PRIMITIVE_TYPE_BOOL:
        RlistAppendScalar(list, JsonPrimitiveGetAsBool(primitive) ? "true" : "false");
        break;
    case JSON_PRIMITIVE_TYPE_INTEGER:
        {
            char *str = StringFromLong(JsonPrimitiveGetAsInteger(primitive));
            RlistAppendScalar(list, str);
            free(str);
        }
        break;
    case JSON_PRIMITIVE_TYPE_REAL:
        {
            char *str = StringFromDouble(JsonPrimitiveGetAsReal(primitive));
            RlistAppendScalar(list, str);
            free(str);
        }
        break;
    case JSON_PRIMITIVE_TYPE_STRING:
        RlistAppendScalar(list, JsonPrimitiveGetAsString(primitive));
        break;

    case JSON_PRIMITIVE_TYPE_NULL:
        break;
    }
}
コード例 #2
0
ファイル: json_test.c プロジェクト: FancsalMelinda/core
static void test_object_iterator(void **state)
{
    JsonElement *obj = JsonObjectCreate(10);

    JsonObjectAppendString(obj, "first", "one");
    JsonObjectAppendString(obj, "second", "two");
    JsonObjectAppendInteger(obj, "third", 3);
    JsonObjectAppendBool(obj, "fourth", true);
    JsonObjectAppendBool(obj, "fifth", false);


    {
        JsonIterator it = JsonIteratorInit(obj);

        assert_string_equal("first", JsonIteratorNextKey(&it));
        assert_string_equal("second", JsonIteratorNextKey(&it));
        assert_string_equal("third", JsonIteratorNextKey(&it));
        assert_string_equal("fourth", JsonIteratorNextKey(&it));
        assert_string_equal("fifth", JsonIteratorNextKey(&it));
        assert_false(JsonIteratorNextKey(&it));
    }

    {
        JsonIterator it = JsonIteratorInit(obj);

        assert_string_equal("one", JsonPrimitiveGetAsString(JsonIteratorNextValue(&it)));
        assert_string_equal("two", JsonPrimitiveGetAsString(JsonIteratorNextValue(&it)));
        assert_int_equal(3, JsonPrimitiveGetAsInteger(JsonIteratorNextValue(&it)));
        assert_true(JsonPrimitiveGetAsBool(JsonIteratorNextValue(&it)));
        assert_false(JsonPrimitiveGetAsBool(JsonIteratorNextValue(&it)));
        assert_false(JsonIteratorNextValue(&it));
    }

    JsonElementDestroy(obj);
}
コード例 #3
0
ファイル: mustache.c プロジェクト: amousset/core
static bool RenderVariablePrimitive(Buffer *out, const JsonElement *primitive, const bool escaped, const bool key_mode)
{
    if (key_mode && JsonElementGetPropertyName(primitive))
    {
        if (escaped)
        {
            RenderHTMLContent(out, JsonElementGetPropertyName(primitive), strlen(JsonElementGetPropertyName(primitive)));
        }
        else
        {
            BufferAppendString(out, JsonElementGetPropertyName(primitive));
        }
        return true;
    }

    switch (JsonGetPrimitiveType(primitive))
    {
    case JSON_PRIMITIVE_TYPE_STRING:
        if (escaped)
        {
            RenderHTMLContent(out, JsonPrimitiveGetAsString(primitive), strlen(JsonPrimitiveGetAsString(primitive)));
        }
        else
        {
            BufferAppendString(out, JsonPrimitiveGetAsString(primitive));
        }
        return true;

    case JSON_PRIMITIVE_TYPE_INTEGER:
        {
            char *str = StringFromLong(JsonPrimitiveGetAsInteger(primitive));
            BufferAppendString(out, str);
            free(str);
        }
        return true;

    case JSON_PRIMITIVE_TYPE_REAL:
        {
            char *str = StringFromDouble(JsonPrimitiveGetAsReal(primitive));
            BufferAppendString(out, str);
            free(str);
        }
        return true;

    case JSON_PRIMITIVE_TYPE_BOOL:
        BufferAppendString(out, JsonPrimitiveGetAsBool(primitive) ? "true" : "false");
        return true;

    case JSON_PRIMITIVE_TYPE_NULL:
        return true;

    default:
        assert(!"Unrecognised JSON primitive type");
    }
    return false;
}
コード例 #4
0
ファイル: json_test.c プロジェクト: GregorioDiStefano/core
static void test_parse_primitives(void)
{
    JsonElement *pri = NULL;

    const char *data = "\"foo\"";
    assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &pri));
    assert_string_equal("foo", JsonPrimitiveGetAsString(pri));
    JsonDestroy(pri);

    data = "-123";
    assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &pri));
    assert_true(-123 == JsonPrimitiveGetAsInteger(pri));
    JsonDestroy(pri);

    data = "1.23";
    assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &pri));
    assert_double_close(1.23, JsonPrimitiveGetAsReal(pri));
    JsonDestroy(pri);

    data = "true";
    assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &pri));
    assert_true(JsonPrimitiveGetAsBool(pri));
    JsonDestroy(pri);
}
コード例 #5
0
static void test_policy_json_offsets(void)
{
    JsonElement *json = NULL;
    {
        Policy *original = LoadPolicy("benchmark.cf");
        json = PolicyToJson(original);
        PolicyDestroy(original);
    }
    assert_true(json);

    JsonElement *json_bundles = JsonObjectGetAsArray(json, "bundles");
    {
        JsonElement *main_bundle = JsonArrayGetAsObject(json_bundles, 0);
        int line = JsonPrimitiveGetAsInteger(JsonObjectGet(main_bundle, "line"));
        assert_int_equal(9, line);

        JsonElement *json_promise_types = JsonObjectGetAsArray(main_bundle, "promiseTypes");
        {
            JsonElement *json_reports_type = JsonArrayGetAsObject(json_promise_types, 0);
            line = JsonPrimitiveGetAsInteger(JsonObjectGet(json_reports_type, "line"));
            assert_int_equal(11, line);

            JsonElement *json_contexts = JsonObjectGetAsArray(json_reports_type, "contexts");
            JsonElement *cf_context = JsonArrayGetAsObject(json_contexts, 0);
            JsonElement *cf_context_promises = JsonObjectGetAsArray(cf_context, "promises");
            JsonElement *hello_cf_promise = JsonArrayGetAsObject(cf_context_promises, 0);

            line = JsonPrimitiveGetAsInteger(JsonObjectGet(hello_cf_promise, "line"));
            assert_int_equal(13, line);
            JsonElement *hello_cf_attribs = JsonObjectGetAsArray(hello_cf_promise, "attributes");
            {
                JsonElement *friend_pattern_attrib = JsonArrayGetAsObject(hello_cf_attribs, 0);

                line = JsonPrimitiveGetAsInteger(JsonObjectGet(friend_pattern_attrib, "line"));
                assert_int_equal(14, line);
            }
        }
    }

    JsonElement *json_bodies = JsonObjectGetAsArray(json, "bodies");
    {
        JsonElement *control_body = JsonArrayGetAsObject(json_bodies, 0);
        int line = JsonPrimitiveGetAsInteger(JsonObjectGet(control_body, "line"));
        assert_int_equal(4, line);

        JsonElement *myperms_body = JsonArrayGetAsObject(json_bodies, 1);
        line = JsonPrimitiveGetAsInteger(JsonObjectGet(myperms_body, "line"));
        assert_int_equal(28, line);

        JsonElement *myperms_contexts = JsonObjectGetAsArray(myperms_body, "contexts");
        JsonElement *any_context = JsonArrayGetAsObject(myperms_contexts, 0);
        JsonElement *any_attribs = JsonObjectGetAsArray(any_context, "attributes");
        {
            JsonElement *mode_attrib = JsonArrayGetAsObject(any_attribs, 0);
            line = JsonPrimitiveGetAsInteger(JsonObjectGet(mode_attrib, "line"));
            assert_int_equal(30, line);
        }
    }

    JsonDestroy(json);
}