C/C++ Question [Solved] B4R::Array from double[]

Daestrum

Expert
Licensed User
Longtime User
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
 

Daestrum

Expert
Licensed User
Longtime User
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;
    }
 
Top