linkhash.c \
printbuf.c
-check_PROGRAMS = test1 test2 test4 test_parse_int64
+check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null
test1_SOURCES = test1.c
test1_LDADD = $(lib_LTLIBRARIES)
test_parse_int64_SOURCES = test_parse_int64.c
test_parse_int64_LDADD = $(lib_LTLIBRARIES)
-TESTS = test1.test test2.test test4.test parse_int64.test
+test_null_SOURCES = test_null.c
+test_null_LDADD = $(lib_LTLIBRARIES)
+
+TESTS = test1.test test2.test test4.test parse_int64.test test_null.test
EXTRA_DIST += $(TESTS)
testsubdir=testSubDir
TESTS_ENVIRONMENT = top_builddir=$(top_builddir)
/* string escaping */
-static int json_escape_str(struct printbuf *pb, char *str)
+static int json_escape_str(struct printbuf *pb, char *str, int len)
{
int pos = 0, start_offset = 0;
unsigned char c;
- do {
+ while (len--) {
c = str[pos];
switch(c) {
- case '\0':
- break;
case '\b':
case '\n':
case '\r':
start_offset = ++pos;
} else pos++;
}
- } while(c);
+ }
if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
return 0;
json_object_object_foreachC(jso, iter) {
if(i) sprintbuf(pb, ",");
sprintbuf(pb, " \"");
- json_escape_str(pb, iter.key);
+ json_escape_str(pb, iter.key, strlen(iter.key));
sprintbuf(pb, "\": ");
if(iter.val == NULL) sprintbuf(pb, "null");
else iter.val->_to_json_string(iter.val, pb);
case json_type_double:
return (jso->o.c_double != 0);
case json_type_string:
- return (strlen(jso->o.c_string) != 0);
+ return (jso->o.c_string.len != 0);
default:
return FALSE;
}
* Parse strings into 64-bit numbers, then use the
* 64-to-32-bit number handling below.
*/
- if (json_parse_int64(jso->o.c_string, &cint64) != 0)
+ if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
return 0; /* whoops, it didn't work. */
o_type = json_type_int;
}
case json_type_boolean:
return jso->o.c_boolean;
case json_type_string:
- if (json_parse_int64(jso->o.c_string, &cint) == 0) return cint;
+ if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
default:
return 0;
}
case json_type_boolean:
return jso->o.c_boolean;
case json_type_string:
- if(sscanf(jso->o.c_string, "%lf", &cdouble) == 1) return cdouble;
+ if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
default:
return 0.0;
}
struct printbuf *pb)
{
sprintbuf(pb, "\"");
- json_escape_str(pb, jso->o.c_string);
+ json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
sprintbuf(pb, "\"");
return 0;
}
static void json_object_string_delete(struct json_object* jso)
{
- free(jso->o.c_string);
+ free(jso->o.c_string.str);
json_object_generic_delete(jso);
}
if(!jso) return NULL;
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
- jso->o.c_string = strdup(s);
+ jso->o.c_string.str = strdup(s);
+ jso->o.c_string.len = strlen(s);
return jso;
}
if(!jso) return NULL;
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
- jso->o.c_string = strndup(s, len);
+ jso->o.c_string.str = malloc(len);
+ memcpy(jso->o.c_string.str, (void *)s, len);
+ jso->o.c_string.len = len;
return jso;
}
if(!jso) return NULL;
switch(jso->o_type) {
case json_type_string:
- return jso->o.c_string;
+ return jso->o.c_string.str;
default:
return json_object_to_json_string(jso);
}
}
+int json_object_get_string_len(struct json_object *jso) {
+ if(!jso) return 0;
+ switch(jso->o_type) {
+ case json_type_string:
+ return jso->o.c_string.len;
+ default:
+ return 0;
+ }
+}
+
/* json_object_array */
--- /dev/null
+/*
+* Tests if binary strings are supported.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+#include "json_inttypes.h"
+#include "json_object.h"
+
+int main() {
+ // this test has a space after the null character. check that it's still included
+ const char *input = " \0 ";
+ const char *expected = "\" \\u0000 \"";
+ struct json_object *string = json_object_new_string_len(input, 3);
+ const char *json = json_object_to_json_string(string);
+
+ int strings_match = !strcmp( expected, json);
+ int retval = 0;
+ if (strings_match) {
+ printf("JSON write result is correct: %s\n", json);
+ printf("PASS\n");
+ } else {
+ printf("JSON write result doesn't match expected string\n");
+ printf("expected string: ");
+ printf("%s\n", expected);
+ printf("parsed string: ");
+ printf("%s\n", json);
+ printf("FAIL\n");
+ retval=1;
+ }
+ json_object_put(string);
+ return retval;
+}