I'm using cJSON
in combination with the JNI to pass JSON data back and forth between my Java and C components.
I'm new to C and teaching myself, so bear with me.
cJSON
has a nice function, cJSON->Print
, which converts a JSON structure into a string representation. This is great, except the string is in "JSON Pretty" format, with all the newlines and tab characters intact. I don't need any of those because my application doesn't display JSON, it just uses it for data transfer. So, I'm trying to write a function that will remove those excess characters.
void json_clean(char *buffer, const char *pretty) {
int count = 0;
for(int i = 0; i < strlen(pretty); i++) {
if(pretty[i] == '\n' || pretty[i] == '\t')
continue;
buffer[count] = pretty[i];
count++;
}
int last = strlen(buffer) - 1;
buffer[last] = '\0';
}
This effectively removes the \n
and \t
characters just fine, but sometimes at the end I get some trash characters, like 6
or ?
, which leads me to think this is a null-termination issue.
The string comes out the same way when using printf
in C and printing it out in Java.
I make sure to allocate one more byte than I need for the NUL
character before I call the function:
// get the pretty JSON
const char *pretty = cJSON_Print(out);
// how many newlines and tabs?
int count = 0;
for(int i = 0; i < strlen(pretty); i++) {
if(pretty[i] == '\n' || pretty[i] == '\t')
count++;
}
// make new buffer, excluding these chars
char *newBuf = malloc((strlen(pretty) - count) + 1); // +1 for null term.
json_clean(newBuf, pretty);
// copy into new Java string
jstring retVal = (*env)->NewStringUTF(env, newBuf);
free(newBuf); // don't need this anymore
Any help is greatly appreciated.