mardi 30 juin 2015

Convert mixed JSON-Number-Array to int, uint, float using lib rapidjson


As I understood this char* is a valid json-string.

const char* json = { "array":[14, -15, 3.17], "array_type": ["uint", "int", "float"] }

All numbers in the array shall be 4 bytes.

How could one loop through the array using rapidjson?

This is my code so far:

int i_val; uint ui_val; float f_val;
Document d;

d.Parse(json);
Value& a = d["array"]; Value& at = d["array_type"];

for(SizeType i=0; i<a.Size();i++) //<-- Crashes here, after accessing the double value
{
   if(a[i].IsInt() && (string)at[i].GetString()=="int")
   {
      i_val=a[i].GetInt();
      //Do anything
   }
   if(a[i].IsUint() && (string)at[i].GetString()=="uint")
   {
      ui_val=a[i].GetUint();
      //Do anything
   }
   if(a[i].IsDouble() && (string)at[i].GetString()=="float")
   {
      f_val=(float) a[i].GetDouble();
      //Do anything
   }
}

I know the last "if" is wrong and thats probably why the program crashes. Double is 8 bytes and the SizeType is 4 bytes by default.

Is there a solution to loop though the array?? If not I also would be good with other libs.. I need to transport these three different types of values by json. By the way the length of the array could be 1 to 500.

(There is no GetFloat() function.)

Thanks for any kind of help. Regards


Aucun commentaire:

Enregistrer un commentaire