如何在C中使用jq检查字符串是否为有效的JSON。

huangapple go评论96阅读模式
英文:

how to check if a string is valid json in c using jq

问题

基本上,我想在C语言中创建一个名为bool json_is_valid(char* input)的函数。由于我在处理JSON的其他部分时使用了jq,我希望也能在这里使用它,但是我无法找到一个合适的方法来实现它。除此之外,我尝试了以下方法:

bool json_is_valid(char* input){
        jq_state* state = jq_init();
        jv compiled = jq_compile_args(state, ""."", NULL);

        return !(jv_is_valid(compiled));
}

然而,这仍然不起作用。如何修复这个问题?

英文:

basically I want to have a function bool json_is_valid(char* input) in c. As I use jq for the rest of my json stuff, I want to use it for this as well, however I am unable to find a proper way of doing it. Among other things I've tried the following:

bool json_is_valid(char* input){
        jq_state* state = jq_init();
        jv compiled = jq_compile_args(state, ".", NULL);

        return !(jv_is_valid(compiled));
}

However, this still doesn't work. How could this be fixed?

答案1

得分: 1

根据我了解,jv_parse 可用于验证 JSON 字符串。

它在 src/jv.h 中声明如下:

jv jv_parse(const char* string);

将此函数的结果传递给 jv_is_valid 看起来可以完成任务。jv_is_validsrc/jv.h 中定义如下:

static int jv_is_valid(jv x) {
    return jv_get_kind(x) != JV_KIND_INVALID;
}

一个简单的示例:

#include <jq.h>
#include <stdio.h>
#include <string.h>

int json_is_valid(const char *json)
{
    jv ele = jv_parse(json);
    int valid = jv_is_valid(ele);
    jv_free(ele);

    return valid;
}

int main(void)
{
    while (1) {
        char buf[512];

        if (!fgets(buf, sizeof buf, stdin))
            break;

        buf[strcspn(buf, "\n")] = '\0';

        printf("<<%s>> Valid? %s.\n", buf, json_is_valid(buf) ? "Yes" : "No");
    }
}
{}
<<{}>> Valid? Yes.
[]
<<[]>> Valid? Yes.
[{}]
<<[{}]>> Valid? Yes.
[][]
<<[][]>> Valid? No.
"foo"
<<"foo">> Valid? Yes.
"foo":"bar"
<<"foo":"bar">> Valid? No.
51
<<51>> Valid? Yes.
{42: []}
<<{42: []}>> Valid? No.
{"42":[1, 2, 3]}
<<{"42":[1, 2, 3]}>> Valid? Yes.
'qux'
<<'qux'>> Valid? No.

jq_compile (jq_compile_args) 的问题在于它似乎解析了手册中描述的 jq 过滤语言

可能有一种更正确的方法,但是缺乏适当的 C API 文档使得研究变得不方便。

英文:

As far as I can tell, jv_parse can be used to validate strings of JSON.

Its declared in src/jv.h as:

jv jv_parse(const char* string);

Passing the result of this function to jv_is_valid appears to get the job done. jv_is_valid is defined in src/jv.h as:

static int jv_is_valid(jv x) {
    return jv_get_kind(x) != JV_KIND_INVALID;
}

A cursory example:

#include <jq.h>
#include <stdio.h>
#include <string.h>

int json_is_valid(const char *json)
{
    jv ele = jv_parse(json);
    int valid = jv_is_valid(ele);
    jv_free(ele);

    return valid;
}

int main(void)
{
    while (1) {
        char buf[512];

        if (!fgets(buf, sizeof buf, stdin))
            break;

        buf[strcspn(buf, "\n")] = '\0';

        printf("<<%s>> Valid? %s.\n", buf, json_is_valid(buf) ? "Yes" : "No");
    }
}
{}
<<{}>> Valid? Yes.
[]
<<[]>> Valid? Yes.
[{}]
<<[{}]>> Valid? Yes.
[][]
<<[][]>> Valid? No.
"foo"
<<"foo">> Valid? Yes.
"foo":"bar"
<<"foo":"bar">> Valid? No.
51
<<51>> Valid? Yes.
{42: []}
<<{42: []}>> Valid? No.
{"42":[1, 2, 3]}
<<{"42":[1, 2, 3]}>> Valid? Yes.
'qux'
<<'qux'>> Valid? No.

The problem with jq_compile (jq_compile_args) is that it would seem to parse the jq filtering language described in the manual.

There might be a more correct method, but the lack of a proper C API documentation makes this an unwieldy problem to research.

huangapple
  • 本文由 发表于 2023年7月4日 22:25:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定