summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2008-06-18 11:45:34 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2008-06-18 11:45:34 +0000
commit9dbcd150758624d09670e7e21bfab214d0d3c101 (patch)
tree693d3f6759bdd219dc52ab80ebf9d99dfbe93fb2
parentf5b08ded5898003ba8d81fc50f2231f4a49bffda (diff)
downloadjson-c-9dbcd150758624d09670e7e21bfab214d0d3c101.tar.gz
json-c-9dbcd150758624d09670e7e21bfab214d0d3c101.tar.bz2
Allow strings to contain \0. This is probably incomplete, but works well enough for our purposes.
svn path=/trunk/json-c/; revision=4385
-rw-r--r--json-c/json_object.c36
-rw-r--r--json-c/json_object.h2
-rw-r--r--json-c/json_object_private.h2
-rw-r--r--json-c/json_tokener.c2
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 == '\\') {