0

I have an existing json structure, and I'm trying to add a 2D array of doubles to a new Value in the json tree. Here's the function showing what I've tried to do, with the commented out option of defining a <vector> instead of a 2D array of doubles (both types give me the same incorrect output):

bool My_class::input_2D_array (Value& object_list, geom_object *obj_f) {
  Value v;

  // n_verts is the number of "vertices[]" in the for loop below
  double vert_list[obj_f->n_verts][3];
  //std::vector<std::array<double, 3>> vert_list(obj_f->n_verts);

  for(int i=0; i < obj_f->n_verts; i++) {
    vert_list[i][0] = obj_f->vertices[i]->x;
    vert_list[i][1] = obj_f->vertices[i]->y;
    vert_list[i][2] = obj_f->vertices[i]->z;
  }
  v["vertex_list"] = vert_list;
  //v["vertex_list"] = vert_list.data();

  object_list.append(v);

  return true;
}

This is what the output is:

"object_list" : 
   {
    "vertex_list" : true
   }

And this is what I'd like the output to be:

"object_list" : 
  {
    "vertex_list": [
      [
       0.0,
       0.0,
       -0.625
      ],
      [
       0.4522545635700226,
       -0.32857829332351685,
       -0.2795122265815735
      ],
      [
       -0.17274247109889984,
       -0.5316557288169861,
       -0.2795124053955078
      ]
    ]
  }

What am I missing here?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Otherness
  • 385
  • 2
  • 16
  • Dynamically sized arrays are a compiler extension, so you probably shouldn't be using them in the first place. try: `std::vector> vert_list(obj_f->n_verts);` instead. –  May 01 '20 at 16:29
  • I take your point about changing the type, but that doesn't fix my issue. My issue is that I have a 2D array that I want to put into a Json tree, and all I see to be able to do is send the pointer address (which Json reads as "true"). – Otherness May 01 '20 at 16:37
  • Arrays (the language ones, not `std::array`) decay to pointers when passed to a function unless there's specific template code in place to handle them. It's just what they do. –  May 01 '20 at 16:51
  • Okay, and do vectors not do that then? – Otherness May 01 '20 at 16:53
  • Nope, they do not, you have to explicitely call `.data()` on a vector to get a pointer to the data. It's not done implicitely. –  May 01 '20 at 16:54
  • Okay, I've edited my question to include what I think you mean, but I'm still getting the same incorrect output of ' "vertex_list" : true ' instead of the actual data. – Otherness May 01 '20 at 17:18

0 Answers0