0

I have implemented Jansson in Android with C and made a function which calculates values from json and that works in C, I tried to use that code in NDK with JNI it builds with no errors, but as i tried to arrange the code to work with JNI it gives me pointer error warning: return from incompatible pointer type. I have read that i need to use jlong for pointers but i cant figure out how that works, it is my first time working in it.

This is my code from C (gives no errors and compiles)

char *doCalc (char *invoice_str) {

json_error_t error;
json_t *invoice = json_loads (invoice_str, JSON_DISABLE_EOF_CHECK, &error);

 ...

 char *result = json_dumps (json_data, JSON_PRESERVE_ORDER);

    return result;

    }

C code Arranged to work with JNI (gives me error warning: return from incompatible pointer type, which if im correct is because of jchar)

JNIEXPORT jchar JNICALL *Java_com_example_test_doCalc (JNIEnv* env, jobject  obj,char const *invoice_str) {

json_error_t error;
json_t *invoice = json_loads (invoice_str, JSON_DISABLE_EOF_CHECK, &error);

...

 char *result = json_dumps (json_data, JSON_PRESERVE_ORDER);

    return result;

    }

Then in my Activity I like to would run doCalc(charJ);, charJ has Json in it. Which would then give me dump of calculated values.

Also I might be looking at this completely wrong, any help is appreciated.

MePo
  • 1,044
  • 2
  • 23
  • 48
  • 1
    use jstring and convert it to char*(inside C code) anyway ... using C for json parsing is ... well .. like shooting to fly from the howitzer – Selvin Jul 23 '14 at 11:04
  • you made me laugh haha... I will try this, the thing is the Calculation is done in C and its working, so i tought it would be better to do it this way rather then redo it in Java (faster), if this wont work i guess i will have to redo it in Java – MePo Jul 23 '14 at 11:25
  • @Selvin could you put this as answer so i can check it, it did the trick got it working – MePo Jul 24 '14 at 09:40

1 Answers1

3

Try to use jstring instead of char*

JNIEXPORT jchar JNICALL * Java_com_example_test_doCalc(JNIEnv * env, jobject obj, jstring invoice_jstring) {

    //convert invoice_jstring to char* link bellow
    json_error_t error;
    json_t * invoice = json_loads(invoice_str, JSON_DISABLE_EOF_CHECK, & error);

    ...

    char * result = json_dumps(json_data, JSON_PRESERVE_ORDER);

    return result;

}

for conversion jstring to char* you can use this answer: JNI converting jstring to char *

Community
  • 1
  • 1
Selvin
  • 6,598
  • 3
  • 37
  • 43