Implement a mostly-functional query language for JSON.

This commit is contained in:
Jordan Bancino 2023-03-08 19:49:06 +00:00
parent 7b22fb02a2
commit 50e599f1cd
3 changed files with 140 additions and 11 deletions

View file

@ -28,8 +28,9 @@ Milestone: v0.3.0
[x] Built on HTTP client API [x] Built on HTTP client API
[ ] http man page [ ] http man page
[~] Simple command line tool for working with JSON [~] Simple command line tool for working with JSON
[x] Should pretty-print Json [x] Pretty-print Json
[ ] Query fields for use in shell scripts. [x] Query fields for use in shell scripts.
[ ] Encode user-provided JSON strings
[ ] json man page [ ] json man page
[ ] Move configuration to database [ ] Move configuration to database

View file

@ -83,7 +83,7 @@ main(int argc, char **argv)
if (!method) if (!method)
{ {
fprintf(stderr, "Unknown request method: %s\n", optarg); fprintf(stderr, "Unknown request method: %s\n", optarg);
return -1; return 1;
} }
break; break;
case 'H': case 'H':
@ -107,21 +107,21 @@ main(int argc, char **argv)
break; break;
default: default:
usage(argv[0]); usage(argv[0]);
return -1; return 1;
} }
} }
if (argc - optind < 1) if (argc - optind < 1)
{ {
usage(argv[0]); usage(argv[0]);
return -1; return 1;
} }
uri = UriParse(argv[optind]); uri = UriParse(argv[optind]);
if (!uri) if (!uri)
{ {
fprintf(stderr, "Failed to parse URI: %s\n", argv[optind]); fprintf(stderr, "Failed to parse URI: %s\n", argv[optind]);
return -1; return 1;
} }
if (!uri->port) if (!uri->port)
@ -140,7 +140,7 @@ main(int argc, char **argv)
{ {
fprintf(stderr, "Unknown protocol: %s\n", uri->proto); fprintf(stderr, "Unknown protocol: %s\n", uri->proto);
UriFree(uri); UriFree(uri);
return -1; return 1;
} }
if (strcmp(uri->proto, "https") == 0) if (strcmp(uri->proto, "https") == 0)
@ -154,7 +154,7 @@ main(int argc, char **argv)
{ {
fprintf(stderr, "Failed to connect.\n"); fprintf(stderr, "Failed to connect.\n");
UriFree(uri); UriFree(uri);
return -1; return 1;
} }
while (HashMapIterate(requestHeaders, &key, (void **) &val)) while (HashMapIterate(requestHeaders, &key, (void **) &val))
@ -180,7 +180,7 @@ main(int argc, char **argv)
fprintf(stderr, "Failed to send request.\n"); fprintf(stderr, "Failed to send request.\n");
HttpClientContextFree(cx); HttpClientContextFree(cx);
UriFree(uri); UriFree(uri);
return -1; return 1;
} }
if (flags & FLAG_HEADERS) if (flags & FLAG_HEADERS)

View file

@ -22,7 +22,12 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <Array.h>
#include <HashMap.h>
#include <Memory.h>
#include <Json.h> #include <Json.h>
int int
@ -38,8 +43,131 @@ main(int argc, char **argv)
return 1; return 1;
} }
JsonEncode(json, stdout, JSON_PRETTY); if (argc > 1)
printf("\n"); {
char *select = argv[1];
char *key;
JsonValue *val = JsonValueObject(json);
key = strtok(select, "->");
while (key)
{
char keyName[128];
size_t arrInd;
int expectArr = 0;
int func = 0;
expectArr = (sscanf(key, "%127[^[][%lu]", keyName, &arrInd) == 2);
if (keyName[0] == '@')
{
if (strcmp(keyName + 1, "length") == 0)
{
switch (JsonValueType(val))
{
case JSON_ARRAY:
val = JsonValueInteger(ArraySize(JsonValueAsArray(val)));
break;
case JSON_STRING:
val = JsonValueInteger(strlen(JsonValueAsString(val)));
break;
default:
val = NULL;
break;
}
}
else if (JsonValueType(val) == JSON_OBJECT && strcmp(keyName + 1, "keys") == 0)
{
HashMap *obj = JsonValueAsObject(val);
Array *arr = ArrayCreate();
char *k;
void *v;
while (HashMapIterate(obj, &k, &v))
{
ArrayAdd(arr, JsonValueString(k));
}
val = JsonValueArray(arr);
}
else if (JsonValueType(val) == JSON_STRING && strcmp(keyName + 1, "decode") == 0)
{
printf("%s", JsonValueAsString(val));
val = NULL;
break;
}
else
{
val = NULL;
break;
}
func = 1;
}
else if (keyName[0] == '!')
{
if (JsonValueType(val) == JSON_OBJECT)
{
HashMap *obj = JsonValueAsObject(val);
JsonValueFree(HashMapDelete(obj, keyName + 1));
}
else if (JsonValueType(val) == JSON_ARRAY)
{
size_t i;
sscanf(keyName + 1, "%lu", &i);
JsonValueFree(ArrayDelete(JsonValueAsArray(val), i));
}
else
{
val = NULL;
break;
}
func = 1;
}
if (!func)
{
if (JsonValueType(val) == JSON_OBJECT)
{
val = HashMapGet(JsonValueAsObject(val), keyName);
}
else
{
val = NULL;
break;
}
}
if (expectArr && JsonValueType(val) == JSON_ARRAY)
{
val = ArrayGet(JsonValueAsArray(val), arrInd);
}
if (!val)
{
break;
}
key = strtok(NULL, "->");
}
if (val)
{
JsonEncodeValue(val, stdout, JSON_PRETTY);
}
printf("\n");
}
else
{
JsonEncode(json, stdout, JSON_PRETTY);
JsonFree(json);
}
MemoryFreeAll();
return 0; return 0;
} }