Skip to content
Permalink
Browse files

Add String to types supported by JSON list render.

All types should be supported eventually but for now this answers the need.
  • Loading branch information...
dwsteele committed Jun 4, 2019
1 parent 0ab6f3b commit aca11b2fa19337f934fc095e33f014d5e6df71f3
Showing with 15 additions and 6 deletions.
  1. +11 −4 src/common/type/json.c
  2. +4 −2 test/src/module/common/typeJsonTest.c
@@ -942,16 +942,23 @@ jsonFromVar(const Variant *var, unsigned int indent)
// Add the indent formatting
strCatFmt(jsonStr, "[%s", strPtr(indentDepth));

// Currently only KeyValue list is supported
// Currently only KeyValue and String lists are supported
for (unsigned int vlIdx = 0; vlIdx < varLstSize(vl); vlIdx++)
{
// If going to add another key, append a comma and format for the next line
if (vlIdx > 0)
strCatFmt(jsonStr, ",%s", strPtr(indentDepth));

// Update the depth before processing the contents of the list element
strCat(indentDepth, strPtr(indentSpace));
strCat(jsonStr, strPtr(jsonFromKvInternal(varKv(varLstGet(vl, vlIdx)), indentSpace, indentDepth)));
Variant *varSub = varLstGet(vl, vlIdx);

if (varType(varSub) == varTypeKeyValue)
{
// Update the depth before processing the contents of the list element
strCat(indentDepth, strPtr(indentSpace));
strCat(jsonStr, strPtr(jsonFromKvInternal(varKv(varSub), indentSpace, indentDepth)));
}
else
jsonFromStrInternal(jsonStr, varStr(varSub));
}

// Decrease the depth
@@ -287,11 +287,12 @@ testRun(void)
TEST_RESULT_STR(strPtr(json), "[]", " empty list no pretty print");

TEST_ASSIGN(varListOuter, varNewVarLst(varLstNew()), "new variant list with keyValues");
varLstAdd(varVarLst(varListOuter), varNewStrZ("ASTRING"));
varLstAdd(varVarLst(varListOuter), keyValue);

TEST_ASSIGN(json, jsonFromVar(varListOuter, 0), "VariantList - no indent");
TEST_RESULT_STR(strPtr(json),
"[{\"backup-info-size-delta\":1982702,\"backup-prior\":\"20161219-212741F_20161219-212803I\","
"[\"ASTRING\",{\"backup-info-size-delta\":1982702,\"backup-prior\":\"20161219-212741F_20161219-212803I\","
"\"backup-reference\":[\"20161219-212741F\",\"20161219-212741F_20161219-212803I\",null],"
"\"backup-timestamp-start\":1482182951,\"checksum-page-error\":[1],"
"\"section\":{\"escape\":\"\\\"\\\\/\\b\\n\\r\\t\\f\",\"key1\":\"value1\",\"key2\":null,\"key3\":\"value2\"}}]",
@@ -302,7 +303,7 @@ testRun(void)

TEST_ASSIGN(json, jsonFromVar(varListOuter, 0), "VariantList - no indent - multiple elements");
TEST_RESULT_STR(strPtr(json),
"[{\"backup-info-size-delta\":1982702,\"backup-prior\":\"20161219-212741F_20161219-212803I\","
"[\"ASTRING\",{\"backup-info-size-delta\":1982702,\"backup-prior\":\"20161219-212741F_20161219-212803I\","
"\"backup-reference\":[\"20161219-212741F\",\"20161219-212741F_20161219-212803I\",null],"
"\"backup-timestamp-start\":1482182951,\"checksum-page-error\":[1],"
"\"section\":{\"escape\":\"\\\"\\\\/\\b\\n\\r\\t\\f\",\"key1\":\"value1\",\"key2\":null,\"key3\":\"value2\"}},"
@@ -315,6 +316,7 @@ testRun(void)
TEST_ASSIGN(json, jsonFromVar(varListOuter, 2), "VariantList - indent 2 - multiple elements");
TEST_RESULT_STR(strPtr(json),
"[\n"
" \"ASTRING\",\n"
" {\n"
" \"backup-info-size-delta\" : 1982702,\n"
" \"backup-prior\" : \"20161219-212741F_20161219-212803I\",\n"

0 comments on commit aca11b2

Please sign in to comment.
You can’t perform that action at this time.