2 * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
12 static void file_not_found()
18 json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
20 fail("json_load_file returned non-NULL for a nonexistent file");
22 fail("json_load_file returned an invalid line number");
24 /* The error message is locale specific, only check the beginning
25 of the error message. */
27 pos = strchr(error.text, ':');
29 fail("json_load_file returne an invalid error message");
33 if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
34 fail("json_load_file returned an invalid error message");
37 static void reject_duplicates()
41 if(json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
42 fail("json_loads did not detect a duplicate key");
43 check_error("duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
46 static void disable_eof_check()
51 const char *text = "{\"foo\": 1} garbage";
53 if(json_loads(text, 0, &error))
54 fail("json_loads did not detect garbage after JSON text");
55 check_error("end of file expected near 'garbage'", "<string>", 1, 18, 18);
57 json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
59 fail("json_loads failed with JSON_DISABLE_EOF_CHECK");
64 static void decode_any()
69 json = json_loads("\"foo\"", JSON_DECODE_ANY, &error);
70 if (!json || !json_is_string(json))
71 fail("json_load decoded any failed - string");
74 json = json_loads("42", JSON_DECODE_ANY, &error);
75 if (!json || !json_is_integer(json))
76 fail("json_load decoded any failed - integer");
79 json = json_loads("true", JSON_DECODE_ANY, &error);
80 if (!json || !json_is_true(json))
81 fail("json_load decoded any failed - boolean");
84 json = json_loads("null", JSON_DECODE_ANY, &error);
85 if (!json || !json_is_null(json))
86 fail("json_load decoded any failed - null");
90 static void load_wrong_args()
95 json = json_loads(NULL, 0, &error);
97 fail("json_loads should return NULL if the first argument is NULL");
99 json = json_loadb(NULL, 0, 0, &error);
101 fail("json_loadb should return NULL if the first argument is NULL");
103 json = json_loadf(NULL, 0, &error);
105 fail("json_loadf should return NULL if the first argument is NULL");
107 json = json_load_file(NULL, 0, &error);
109 fail("json_loadf should return NULL if the first argument is NULL");
112 static void position()
115 size_t flags = JSON_DISABLE_EOF_CHECK;
118 json = json_loads("{\"foo\": \"bar\"}", 0, &error);
119 if(error.position != 14)
120 fail("json_loads returned a wrong position");
123 json = json_loads("{\"foo\": \"bar\"} baz quux", flags, &error);
124 if(error.position != 14)
125 fail("json_loads returned a wrong position");
129 static void run_tests()