Skip to content

Search for a key with . and / in key #92

@git001

Description

@git001

I try to search for that key kubernetes.io/serviceaccount/namespace, is this possible with this lib?

Here my naive sample code

#include <core_json.h>
#include <stdio.h>

int main()
{
    // Variables used in this example.
    JSONStatus_t result;
    char buffer[] = "{\"iss\":\"kubernetes/serviceaccount\",\"kubernetes.io/serviceaccount/namespace\":\"openshift-logging\",\"foo\":\"abc\",\"bar\":{\"foo\":\"xyz\"}}";
    size_t bufferLength = sizeof(buffer) - 1;
    // char query[] = "kubernetes.io/serviceaccount/namespace";
    char query[] = "iss";
    size_t queryLength = sizeof(query) - 1;
    char *value;
    size_t valueLength;

    // Calling JSON_Validate() is not necessary if the document is guaranteed to be valid.
    result = JSON_Validate(buffer, bufferLength);
    if (result == JSONSuccess)
    {
        result = JSON_Search(buffer, bufferLength, query, queryLength,
                             &value, &valueLength);
        if (result == JSONSuccess)
        {
            // The pointer "value" will point to a location in the "buffer".
            char save = value[valueLength];
            // After saving the character, set it to a null byte for printing.
            value[valueLength] = '\0';
            // "Found: bar.foo -> xyz" will be printed.
            printf("Found: %s -> %s\n", query, value);
            // Restore the original character.
            value[valueLength] = save;
        } else {
            printf("NOT JSON_Search Found: %d\n", result);
        }
    }
    else
    {
        printf("NOT JSON_Validate Found: %s\n", query);
    }
}

build with this command

# kubernetes.io/serviceaccount/namespace
git-repos/coreJSON$ gcc -Isource/include/ source/core_json.c test/jwt/my-jwt.c -o test/jwt/my-jwt && test/jwt/my-jwt
# iss
gcc -Isource/include/ source/core_json.c test/jwt/my-jwt.c -o test/jwt/my-jwt && test/jwt/my-jwt
Found: iss -> kubernetes/serviceaccount

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions