Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Frozen json_setf

EmmanuelEmmanuel France
edited March 11 in Mongoose OS

Hi,

I'm trying to use Frozen, and got weird results.
I'm calling multiple json_setf to update my JSON string,
It seems that my JSON_OUT_BUF s_out string output is not cleared out after multiple calls.

Below the code.

char s_in[255], s_out[255];
strcpy( s_in, "{\"a\":1,\"b\":2}" );
printf( "Initial : %s\n", s_in ); // Got {"a":1,"b":2} => OK

struct json_out out = JSON_OUT_BUF( s_out, sizeof( s_out ) );
json_setf( s_in, sizeof( s_in ), &out, ".a", "7" );
strcpy( s_in, s_out );
printf( "Step 1 : %s\n", s_in ); // Got {"a":7,"b":2} => OK

//memset( s_out, 0, sizeof( s_out ) );
json_setf( s_in, sizeof( s_in ), &out, ".b", "8" );
strcpy( s_in, s_out );
printf( "Step 2 : %s\n", s_in ); // Got {"a":7,"b":2}{"a":7,"b":8} => should be {"a":7,"b":8}

json_setf( s_in, sizeof( s_in ), &out, ".c", "108" );
strcpy( s_in, s_out );
printf( "Step 3 : %s\n", s_in );// Got {"a":7,"b":2}{"a":7,"b":8}{"a":7,"b":2,"c":108}{"a":7,"b":8} => should be {"a":7,"b":2,"c":108}

I tried to reset it, with memset. However, the json_setf call after memset (step2) will return s_out empty.

Any clue ?

Comments

  • nliviunliviu Romania

    You have to reset the len member of the json_out structure
    2 ways to do it:

        char s_in[255], s_out[255];
        strcpy(s_in, "{\"a\":1,\"b\":2}");
        printf("---- Initial : %s\n", s_in); // Got {"a":1,"b":2} => OK
    
        struct json_out out = JSON_OUT_BUF(s_out, sizeof (s_out));
        json_setf(s_in, sizeof (s_in), &out, ".a", "7");
        strcpy(s_in, s_out);
        printf("----  Step 1 : %s\n", s_in); // Got {"a":7,"b":2} => OK
    
        out.u.buf.len = 0;
        json_setf(s_in, sizeof (s_in), &out, ".b", "8");
        strcpy(s_in, s_out);
        printf("----  Step 2 : %s\n", s_in); // Got {"a":7,"b":2}{"a":7,"b":8} => should be {"a":7,"b":8}
    
        {
            struct json_out out = JSON_OUT_BUF(s_out, sizeof (s_out));
            json_setf(s_in, sizeof (s_in), &out, ".c", "108");
            strcpy(s_in, s_out);
            printf("----  Step 3 : %s\n", s_in); // Got {"a":7,"b":2}{"a":7,"b":8}{"a":7,"b":2,"c":108}{"a":7,"b":8} => should be {"a":7,"b":2,"c":108}
        }
    

    I prefer the 2nd one, because the internal strucure of struct json_out might change.

  • EmmanuelEmmanuel France
    edited March 11

    Thanks, it's now working !

    Now I'm struggling with json_path (key) with more that 1 character, using json_setf to add key/value.
    strcpy( s_in, "{\"Hello\":1,\"b\":2}" );
    printf( "Initial : %s\n", s_in ); // {"Hello":1,"b":2} => OK

    struct json_out out = JSON_OUT_BUF( s_out, sizeof( s_out ) );
    json_setf( s_in, sizeof( s_in ), &out, ".Hello", "7" );
    strcpy( s_in, s_out );
    printf( "Step 1 : %s\n", s_in ); // {"Hello":7,"b":2} => OK

    out.u.buf.len = 0;
    json_setf( s_in, sizeof( s_in ), &out, ".b", "8" );
    strcpy( s_in, s_out );
    printf( "Step 2 : %s\n", s_in ); // {"Hello":7,"b":8} => OK

    out.u.buf.len = 0;
    json_setf( s_in, sizeof( s_in ), &out, ".Goodbye", "10" ); // {"Hello":7,"b":8,"G":10} => G instead of Goodbye
    strcpy( s_in, s_out );
    printf( "Step 3 : %s\n", s_in );

    It is working OK when the key already exists. If not, only the first character of the key is inserted.

  • nliviunliviu Romania

    It looks like a bug.
    You might want to open an issue

    Thanked by 1Emmanuel
Sign In or Register to comment.