From 9dbcd150758624d09670e7e21bfab214d0d3c101 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Wed, 18 Jun 2008 11:45:34 +0000 Subject: Allow strings to contain \0. This is probably incomplete, but works well enough for our purposes. svn path=/trunk/json-c/; revision=4385 --- json-c/json_object.c | 36 ++++++++++++++++++++++++++++-------- json-c/json_object.h | 2 ++ json-c/json_object_private.h | 2 +- json-c/json_tokener.c | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/json-c/json_object.c b/json-c/json_object.c index 228345f..ac17e19 100644 --- a/json-c/json_object.c +++ b/json-c/json_object.c @@ -306,7 +306,7 @@ boolean json_object_get_boolean(struct json_object *this) case json_type_double: return (this->o.c_double != 0); case json_type_string: - if(strlen(this->o.c_string)) return TRUE; + if(strlen(this->o.c_string.data)) return TRUE; default: return TRUE; } @@ -343,7 +343,7 @@ int json_object_get_int(struct json_object *this) case json_type_boolean: return this->o.c_boolean; case json_type_string: - if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint; + if(sscanf(this->o.c_string.data, "%d", &cint) == 1) return cint; default: return 0; } @@ -380,7 +380,7 @@ double json_object_get_double(struct json_object *this) case json_type_boolean: return this->o.c_boolean; case json_type_string: - if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble; + if(sscanf(this->o.c_string.data, "%lf", &cdouble) == 1) return cdouble; default: return 0.0; } @@ -393,14 +393,14 @@ static int json_object_string_to_json_string(struct json_object* this, struct printbuf *pb) { sprintbuf(pb, "\""); - json_escape_str(pb, this->o.c_string); + json_escape_str(pb, this->o.c_string.data); sprintbuf(pb, "\""); return 0; } static void json_object_string_delete(struct json_object* this) { - free(this->o.c_string); + free(this->o.c_string.data); json_object_generic_delete(this); } @@ -410,7 +410,8 @@ struct json_object* json_object_new_string(char *s) if(!this) return NULL; this->_delete = &json_object_string_delete; this->_to_json_string = &json_object_string_to_json_string; - this->o.c_string = strdup(s); + this->o.c_string.data = strdup(s); + this->o.c_string.len = strlen(s); return this; } @@ -420,7 +421,11 @@ struct json_object* json_object_new_string_len(char *s, int len) if(!this) return NULL; this->_delete = &json_object_string_delete; this->_to_json_string = &json_object_string_to_json_string; - this->o.c_string = strndup(s, len); + this->o.c_string.data = malloc(len + 1); + if (!this->o.c_string.data) return NULL; + memcpy(this->o.c_string.data, s, len); + this->o.c_string.data[len] = '\0'; + this->o.c_string.len = len; return this; } @@ -429,12 +434,27 @@ char* json_object_get_string(struct json_object *this) if(!this) return NULL; switch(this->o_type) { case json_type_string: - return this->o.c_string; + return this->o.c_string.data; default: return json_object_to_json_string(this); } } +char* json_object_get_string_len(struct json_object *this, int *len) +{ + char *out; + if (!this) return NULL; + switch(this->o_type) { + case json_type_string: + *len = this->o.c_string.len; + return this->o.c_string.data; + default: + out = json_object_to_json_string(this); + *len = strlen(out); + return out; + } +} + /* json_object_array */ diff --git a/json-c/json_object.h b/json-c/json_object.h index d4fc887..e9bb763 100644 --- a/json-c/json_object.h +++ b/json-c/json_object.h @@ -307,4 +307,6 @@ extern struct json_object* json_object_new_string_len(char *s, int len); */ extern char* json_object_get_string(struct json_object *obj); +extern char* json_object_get_string_len(struct json_object *obj, int *len); + #endif diff --git a/json-c/json_object_private.h b/json-c/json_object_private.h index 35a44f3..3426a86 100644 --- a/json-c/json_object_private.h +++ b/json-c/json_object_private.h @@ -29,7 +29,7 @@ struct json_object int c_int; struct lh_table *c_object; struct array_list *c_array; - char *c_string; + struct { char *data; int len; } c_string; } o; }; diff --git a/json-c/json_tokener.c b/json-c/json_tokener.c index d594569..336918a 100644 --- a/json-c/json_tokener.c +++ b/json-c/json_tokener.c @@ -318,7 +318,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok, case json_tokener_state_string: if(c == tok->quote_char) { - current = json_object_new_string(tok->pb->buf); + current = json_object_new_string_len(tok->pb->buf, tok->pb->bpos); saved_state = json_tokener_state_finish; state = json_tokener_state_eatws; } else if(c == '\\') { -- cgit v1.2.3