mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Update to permit compilation with gcc -ansi flag
(changed all C++ // comments to C /* */ comments). git-svn-id: http://svn.code.sf.net/p/cjson/code@34 e3330c51-1366-4df0-8b21-3ccf24e3d50e
This commit is contained in:
50
test.c
50
test.c
@ -24,7 +24,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "cJSON.h"
|
||||
|
||||
// Parse text to JSON, then render back to text, and print!
|
||||
/* Parse text to JSON, then render back to text, and print! */
|
||||
void doit(char *text)
|
||||
{
|
||||
char *out;cJSON *json;
|
||||
@ -36,7 +36,7 @@ void doit(char *text)
|
||||
free(out);
|
||||
}
|
||||
|
||||
// Read a file, parse, render back, etc.
|
||||
/* Read a file, parse, render back, etc. */
|
||||
void dofile(char *filename)
|
||||
{
|
||||
FILE *f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET);
|
||||
@ -45,17 +45,17 @@ void dofile(char *filename)
|
||||
free(data);
|
||||
}
|
||||
|
||||
// Used by some code below as an example datatype.
|
||||
/* Used by some code below as an example datatype. */
|
||||
struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; };
|
||||
|
||||
// Create a bunch of objects as demonstration.
|
||||
/* Create a bunch of objects as demonstration. */
|
||||
void create_objects()
|
||||
{
|
||||
cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; // declare a few.
|
||||
cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */
|
||||
|
||||
// Here we construct some JSON standards, from the JSON site.
|
||||
/* Here we construct some JSON standards, from the JSON site. */
|
||||
|
||||
// Our "Video" datatype:
|
||||
/* Our "Video" datatype: */
|
||||
root=cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
|
||||
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
|
||||
@ -65,25 +65,25 @@ void create_objects()
|
||||
cJSON_AddFalseToObject (fmt,"interlace");
|
||||
cJSON_AddNumberToObject(fmt,"frame rate", 24);
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); // Print to text, Delete the cJSON, print it, release the string.
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string.
|
||||
|
||||
// Our "days of the week" array:
|
||||
/* Our "days of the week" array: */
|
||||
const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
|
||||
root=cJSON_CreateStringArray(strings,7);
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
|
||||
|
||||
// Our matrix:
|
||||
/* Our matrix: */
|
||||
int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}};
|
||||
root=cJSON_CreateArray();
|
||||
for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3));
|
||||
|
||||
// cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement"));
|
||||
/* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
|
||||
|
||||
|
||||
// Our "gallery" item:
|
||||
/* Our "gallery" item: */
|
||||
int ids[4]={116,943,234,38793};
|
||||
root=cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject());
|
||||
@ -91,14 +91,14 @@ void create_objects()
|
||||
cJSON_AddNumberToObject(img,"Height",600);
|
||||
cJSON_AddStringToObject(img,"Title","View from 15th Floor");
|
||||
cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject());
|
||||
cJSON_AddStringToObject(thm, "Url", "http://www.example.com/image/481989943");
|
||||
cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
|
||||
cJSON_AddNumberToObject(thm,"Height",125);
|
||||
cJSON_AddStringToObject(thm,"Width","100");
|
||||
cJSON_AddItemToObject(img,"IDs", cJSON_CreateIntArray(ids,4));
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
|
||||
|
||||
// Our array of "records":
|
||||
/* Our array of "records": */
|
||||
struct record fields[2]={
|
||||
{"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"},
|
||||
{"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}};
|
||||
@ -117,35 +117,35 @@ void create_objects()
|
||||
cJSON_AddStringToObject(fld, "Country", fields[i].country);
|
||||
}
|
||||
|
||||
// cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4));
|
||||
/* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
|
||||
|
||||
}
|
||||
|
||||
int main (int argc, const char * argv[]) {
|
||||
// a bunch of json:
|
||||
/* a bunch of json: */
|
||||
char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\": \"rect\", \n\"width\": 1920, \n\"height\": 1080, \n\"interlace\": false,\"frame rate\": 24\n}\n}";
|
||||
char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]";
|
||||
char text3[]="[\n [0, -1, 0],\n [1, 0, 0],\n [0, 0, 1]\n ]\n";
|
||||
char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http://www.example.com/image/481989943\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }";
|
||||
char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http:/*www.example.com/image/481989943\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }";
|
||||
char text5[]="[\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.7668,\n \"Longitude\": -122.3959,\n \"Address\": \"\",\n \"City\": \"SAN FRANCISCO\",\n \"State\": \"CA\",\n \"Zip\": \"94107\",\n \"Country\": \"US\"\n },\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.371991,\n \"Longitude\": -122.026020,\n \"Address\": \"\",\n \"City\": \"SUNNYVALE\",\n \"State\": \"CA\",\n \"Zip\": \"94085\",\n \"Country\": \"US\"\n }\n ]";
|
||||
|
||||
// Process each json textblock by parsing, then rebuilding:
|
||||
/* Process each json textblock by parsing, then rebuilding: */
|
||||
doit(text1);
|
||||
doit(text2);
|
||||
doit(text3);
|
||||
doit(text4);
|
||||
doit(text5);
|
||||
|
||||
// Parse standard testfiles:
|
||||
// dofile("../../tests/test1");
|
||||
// dofile("../../tests/test2");
|
||||
// dofile("../../tests/test3");
|
||||
// dofile("../../tests/test4");
|
||||
// dofile("../../tests/test5");
|
||||
/* Parse standard testfiles:
|
||||
/* dofile("../../tests/test1"); */
|
||||
/* dofile("../../tests/test2"); */
|
||||
/* dofile("../../tests/test3"); */
|
||||
/* dofile("../../tests/test4"); */
|
||||
/* dofile("../../tests/test5"); */
|
||||
|
||||
// Now some samplecode for building objects concisely:
|
||||
/* Now some samplecode for building objects concisely: */
|
||||
create_objects();
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user