forked from akheron/jansson
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org> | ||
* | ||
* Jansson is free software; you can redistribute it and/or modify | ||
* it under the terms of the MIT license. See LICENSE for details. | ||
*/ | ||
|
||
#include <jansson.h> | ||
#include <string.h> | ||
#include "util.h" | ||
|
||
int main() | ||
{ | ||
json_t *json; | ||
char *result; | ||
|
||
/* Encode an empty object/array, add an item, encode again */ | ||
|
||
json = json_object(); | ||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "{}")) | ||
fail("json_dumps failed"); | ||
|
||
json_object_set_new(json, "foo", json_integer(5)); | ||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "{\"foo\": 5}")) | ||
fail("json_dumps failed"); | ||
|
||
json_decref(json); | ||
|
||
json = json_array(); | ||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "[]")) | ||
fail("json_dumps failed"); | ||
free(result); | ||
|
||
json_array_append_new(json, json_integer(5)); | ||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "[5]")) | ||
fail("json_dumps failed"); | ||
free(result); | ||
|
||
json_decref(json); | ||
|
||
/* Construct a JSON object/array with a circular reference: | ||
object: {"a": {"b": {"c": <circular reference to $.a>}}} | ||
array: [[[<circular reference to the $[0] array>]]] | ||
Encode it, remove the circular reference and encode again. | ||
*/ | ||
json = json_object(); | ||
json_object_set_new(json, "a", json_object()); | ||
json_object_set_new(json_object_get(json, "a"), "b", json_object()); | ||
json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c", | ||
json_object_get(json, "a")); | ||
|
||
if(json_dumps(json, 0)) | ||
fail("json_dumps encoded a circular reference!"); | ||
|
||
json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c"); | ||
|
||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "{\"a\": {\"b\": {}}}")) | ||
fail("json_dumps failed!"); | ||
free(result); | ||
|
||
json_decref(json); | ||
|
||
json = json_array(); | ||
json_array_append_new(json, json_array()); | ||
json_array_append_new(json_array_get(json, 0), json_array()); | ||
json_array_append(json_array_get(json_array_get(json, 0), 0), | ||
json_array_get(json, 0)); | ||
|
||
if(json_dumps(json, 0)) | ||
fail("json_dumps encoded a circular reference!"); | ||
|
||
json_array_remove(json_array_get(json_array_get(json, 0), 0), 0); | ||
|
||
result = json_dumps(json, 0); | ||
if(!result || strcmp(result, "[[[]]]")) | ||
fail("json_dumps failed!"); | ||
free(result); | ||
|
||
json_decref(json); | ||
|
||
return 0; | ||
} |