Daestrum Expert Licensed User Longtime User Mar 25, 2021 #1 I think I have exhausted all the ways I can think of doing this. I need to return an Array to B4R from a library wrapper routine. The current code: B4X: B4R::Array* rJson::getArray(B4R::B4RString* key,B4R::Object* o) { JsonArray data = obj[(char*)key->data].as<JsonArray>(); B4R::Array* arr = (B4R::Array*)o->data.PointerField; double d[(int)arr->length]; int a = 0; for(JsonVariant v : data) { d[a++] = v.as<double>(); ::Serial.printf("%3.6f \r\n",d[a-1]); } arr->wrap(d,(unsigned int)2); o = (B4R::Object*)arr; return arr; } The serial.print confirms that d[] does contain the values I want. in the B4R code its called by B4X: dim d[2] As Double Json.getArray("data",d) ' I also tried d = Json.getArray("data",d) with the same results logging d(0) and d(1) always returns 2 and 3.1309 even though the 'data' is 48.756080 and 2.302038
I think I have exhausted all the ways I can think of doing this. I need to return an Array to B4R from a library wrapper routine. The current code: B4X: B4R::Array* rJson::getArray(B4R::B4RString* key,B4R::Object* o) { JsonArray data = obj[(char*)key->data].as<JsonArray>(); B4R::Array* arr = (B4R::Array*)o->data.PointerField; double d[(int)arr->length]; int a = 0; for(JsonVariant v : data) { d[a++] = v.as<double>(); ::Serial.printf("%3.6f \r\n",d[a-1]); } arr->wrap(d,(unsigned int)2); o = (B4R::Object*)arr; return arr; } The serial.print confirms that d[] does contain the values I want. in the B4R code its called by B4X: dim d[2] As Double Json.getArray("data",d) ' I also tried d = Json.getArray("data",d) with the same results logging d(0) and d(1) always returns 2 and 3.1309 even though the 'data' is 48.756080 and 2.302038
Erel B4X founder Staff member Licensed User Longtime User Mar 26, 2021 #2 Your code cannot work as you are creating an array on the stack. The memory is released when method ends. It will be much simpler, and will require less memory, to pass the array to this method and fill it.
Your code cannot work as you are creating an array on the stack. The memory is released when method ends. It will be much simpler, and will require less memory, to pass the array to this method and fill it.
Daestrum Expert Licensed User Longtime User Mar 26, 2021 #3 Sorted thank you. I had passed the array to the function, but didn't know how to add items to it. Final working code in case anyone else needs to do similar. B4X: B4R::Array* rJson::getArray(B4R::B4RString* key,B4R::Object* o) { JsonArray data = obj[(char*)key->data].as<JsonArray>(); B4R::Array* arr = (B4R::Array*)B4R::Object::toPointer(o); int a = 0; for(JsonVariant v : data) { ((Double*)arr->getData((UInt) (a++)))[B4R::Array::staticIndex] = (double)v.as<double>(); } o = (B4R::Object*)arr; return arr; }
Sorted thank you. I had passed the array to the function, but didn't know how to add items to it. Final working code in case anyone else needs to do similar. B4X: B4R::Array* rJson::getArray(B4R::B4RString* key,B4R::Object* o) { JsonArray data = obj[(char*)key->data].as<JsonArray>(); B4R::Array* arr = (B4R::Array*)B4R::Object::toPointer(o); int a = 0; for(JsonVariant v : data) { ((Double*)arr->getData((UInt) (a++)))[B4R::Array::staticIndex] = (double)v.as<double>(); } o = (B4R::Object*)arr; return arr; }