Add a static JsonEncodeString() method.

Right now it just calls fprintf(), but in the future, it will
properly encode a string for JSON output.
This commit is contained in:
Jordan Bancino 2022-07-26 09:10:17 -04:00
parent 02f8c4bb82
commit 644733c74e

View file

@ -248,6 +248,12 @@ JsonValueFree(JsonValue * value)
free(value);
}
static void
JsonEncodeString(const char * str, FILE * out)
{
fprintf(out, "\"%s\"", str);
}
static void
JsonEncodeValue(JsonValue * value, FILE * out)
{
@ -278,7 +284,7 @@ JsonEncodeValue(JsonValue * value, FILE * out)
fputc(']', out);
break;
case JSON_STRING:
fprintf(out, "\"%s\"", value->as.string);
JsonEncodeString(value->as.string, out);
break;
case JSON_INTEGER:
fprintf(out, "%lld", value->as.integer);
@ -328,7 +334,8 @@ JsonEncode(HashMap * object, FILE * out)
index = 0;
while (HashMapIterate(object, &key, (void **) &value))
{
fprintf(out, "\"%s\":", key);
JsonEncodeString(key, out);
fputc(':', out);
JsonEncodeValue(value, out);
if (index < count - 1)