2015-02-09 21:29:05 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-02-11 04:29:40 +03:00
|
|
|
#include <string.h>
|
2015-02-09 21:29:05 +03:00
|
|
|
#include "cJSON_Utils.h"
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2015-02-13 22:19:26 +03:00
|
|
|
int i;
|
|
|
|
/* JSON Pointer tests: */
|
|
|
|
cJSON *root;
|
2015-02-09 21:29:05 +03:00
|
|
|
const char *json="{"
|
|
|
|
"\"foo\": [\"bar\", \"baz\"],"
|
|
|
|
"\"\": 0,"
|
|
|
|
"\"a/b\": 1,"
|
|
|
|
"\"c%d\": 2,"
|
|
|
|
"\"e^f\": 3,"
|
|
|
|
"\"g|h\": 4,"
|
|
|
|
"\"i\\\\j\": 5,"
|
|
|
|
"\"k\\\"l\": 6,"
|
|
|
|
"\" \": 7,"
|
|
|
|
"\"m~n\": 8"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"};
|
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* JSON Apply Patch tests: */
|
2015-02-11 04:29:40 +03:00
|
|
|
const char *patches[15][3]={
|
|
|
|
{"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"},
|
|
|
|
{"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"},
|
|
|
|
{"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"},
|
|
|
|
{"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"},
|
|
|
|
{"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"},
|
|
|
|
{"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"},
|
|
|
|
{"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"},
|
|
|
|
{"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""},
|
|
|
|
{"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""},
|
|
|
|
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"},
|
|
|
|
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"},
|
|
|
|
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""},
|
|
|
|
{"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""},
|
|
|
|
{"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""},
|
|
|
|
{"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}};
|
2016-05-29 19:39:54 +03:00
|
|
|
|
|
|
|
/* JSON Apply Merge tests: */
|
|
|
|
const char *merges[15][3]={
|
|
|
|
{"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
|
|
|
|
{"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"},
|
|
|
|
{"{\"a\":\"b\"}", "{\"a\":null}", "{}"},
|
|
|
|
{"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"},
|
|
|
|
{"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
|
|
|
|
{"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"},
|
|
|
|
{"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"},
|
|
|
|
{"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"},
|
|
|
|
{"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"},
|
|
|
|
{"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"},
|
|
|
|
{"{\"a\":\"foo\"}", "null", "null"},
|
|
|
|
{"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""},
|
|
|
|
{"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"},
|
|
|
|
{"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"},
|
|
|
|
{"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}};
|
|
|
|
|
2015-02-09 21:29:05 +03:00
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* Misc tests */
|
|
|
|
int numbers[10]={0,1,2,3,4,5,6,7,8,9};
|
|
|
|
const char *random="QWERTYUIOPASDFGHJKLZXCVBNM";
|
|
|
|
char buf[2]={0,0},*before,*after;
|
|
|
|
cJSON *object,*nums,*num6,*sortme;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("JSON Pointer Tests\n");
|
|
|
|
root=cJSON_Parse(json);
|
|
|
|
for (i=0;i<12;i++)
|
|
|
|
{
|
|
|
|
char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i]));
|
|
|
|
printf("Test %d:\n%s\n\n",i+1,output);
|
|
|
|
free(output);
|
|
|
|
}
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
|
|
|
|
2015-02-11 04:29:40 +03:00
|
|
|
printf("JSON Apply Patch Tests\n");
|
2015-02-13 22:19:26 +03:00
|
|
|
for (i=0;i<15;i++)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
|
|
|
cJSON *object=cJSON_Parse(patches[i][0]);
|
|
|
|
cJSON *patch=cJSON_Parse(patches[i][1]);
|
|
|
|
int err=cJSONUtils_ApplyPatches(object,patch);
|
|
|
|
char *output=cJSON_Print(object);
|
|
|
|
printf("Test %d (err %d):\n%s\n\n",i+1,err,output);
|
2015-02-11 04:29:40 +03:00
|
|
|
free(output);cJSON_Delete(object);cJSON_Delete(patch);
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* JSON Generate Patch tests: */
|
2015-02-11 04:29:40 +03:00
|
|
|
printf("JSON Generate Patch Tests\n");
|
2015-02-13 22:19:26 +03:00
|
|
|
for (i=0;i<15;i++)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2015-02-13 22:19:26 +03:00
|
|
|
cJSON *from,*to,*patch;char *out;
|
2015-02-11 04:29:40 +03:00
|
|
|
if (!strlen(patches[i][2])) continue;
|
2015-02-13 22:19:26 +03:00
|
|
|
from=cJSON_Parse(patches[i][0]);
|
|
|
|
to=cJSON_Parse(patches[i][2]);
|
|
|
|
patch=cJSONUtils_GeneratePatches(from,to);
|
|
|
|
out=cJSON_Print(patch);
|
2015-02-11 04:29:40 +03:00
|
|
|
printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out);
|
|
|
|
free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* Misc tests: */
|
2015-02-11 04:29:40 +03:00
|
|
|
printf("JSON Pointer construct\n");
|
2015-02-13 22:19:26 +03:00
|
|
|
object=cJSON_CreateObject();
|
|
|
|
nums=cJSON_CreateIntArray(numbers,10);
|
|
|
|
num6=cJSON_GetArrayItem(nums,6);
|
2015-02-11 04:29:40 +03:00
|
|
|
cJSON_AddItemToObject(object,"numbers",nums);
|
2015-10-14 03:43:18 +03:00
|
|
|
char *temp=cJSONUtils_FindPointerFromObjectTo(object,num6);
|
|
|
|
printf("Pointer: [%s]\n",temp);
|
|
|
|
free(temp);
|
|
|
|
temp=cJSONUtils_FindPointerFromObjectTo(object,nums);
|
|
|
|
printf("Pointer: [%s]\n",temp);
|
|
|
|
free(temp);
|
|
|
|
temp=cJSONUtils_FindPointerFromObjectTo(object,object);
|
|
|
|
printf("Pointer: [%s]\n",temp);
|
|
|
|
free(temp);
|
|
|
|
cJSON_Delete(object);
|
2015-02-13 22:19:26 +03:00
|
|
|
|
|
|
|
/* JSON Sort test: */
|
|
|
|
sortme=cJSON_CreateObject();
|
|
|
|
for (i=0;i<26;i++)
|
|
|
|
{
|
|
|
|
buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1));
|
|
|
|
}
|
2015-02-13 22:53:27 +03:00
|
|
|
before=cJSON_PrintUnformatted(sortme);
|
2015-02-13 22:19:26 +03:00
|
|
|
cJSONUtils_SortObject(sortme);
|
2015-02-13 22:53:27 +03:00
|
|
|
after=cJSON_PrintUnformatted(sortme);
|
2015-02-13 22:19:26 +03:00
|
|
|
printf("Before: [%s]\nAfter: [%s]\n\n",before,after);
|
|
|
|
free(before);free(after);cJSON_Delete(sortme);
|
2016-05-29 19:39:54 +03:00
|
|
|
|
|
|
|
/* Merge tests: */
|
|
|
|
printf("JSON Merge Patch tests\n");
|
|
|
|
for (i=0;i<15;i++)
|
|
|
|
{
|
|
|
|
cJSON *object=cJSON_Parse(merges[i][0]);
|
|
|
|
cJSON *patch=cJSON_Parse(merges[i][1]);
|
|
|
|
char *before=cJSON_PrintUnformatted(object);
|
|
|
|
char *patchtext=cJSON_PrintUnformatted(patch);
|
|
|
|
printf("Before: [%s] -> [%s] = ",before,patchtext);
|
|
|
|
object=cJSONUtils_MergePatch(object,patch);
|
|
|
|
char *after=cJSON_PrintUnformatted(object);
|
|
|
|
printf("[%s] vs [%s] (%s)\n",after,merges[i][2],strcmp(after,merges[i][2])?"FAIL":"OK");
|
|
|
|
|
|
|
|
free(before);free(patchtext);free(after);cJSON_Delete(object);cJSON_Delete(patch);
|
|
|
|
}
|
2016-05-29 20:16:19 +03:00
|
|
|
|
|
|
|
/* Generate Merge tests: */
|
|
|
|
for (i=0;i<15;i++)
|
|
|
|
{
|
|
|
|
cJSON *from=cJSON_Parse(merges[i][0]);
|
|
|
|
cJSON *to=cJSON_Parse(merges[i][2]);
|
|
|
|
cJSON *patch=cJSONUtils_GenerateMergePatch(from,to);
|
|
|
|
from=cJSONUtils_MergePatch(from,patch);
|
|
|
|
char *patchtext=cJSON_PrintUnformatted(patch);
|
|
|
|
char *patchedtext=cJSON_PrintUnformatted(from);
|
|
|
|
printf("Patch [%s] vs [%s] = [%s] vs [%s] (%s)\n",patchtext,merges[i][1],patchedtext,merges[i][2],strcmp(patchedtext,merges[i][2])?"FAIL":"OK");
|
|
|
|
cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);free(patchtext);free(patchedtext);
|
|
|
|
}
|
2016-05-29 19:39:54 +03:00
|
|
|
|
2015-02-09 21:29:05 +03:00
|
|
|
}
|