YasserAsmi / jvar

JS inspired Variants and JSON parsing for C++
MIT License
24 stars 11 forks source link

False negative on json parsing #24

Closed pnaulls closed 8 years ago

pnaulls commented 8 years ago

With the following code:

#include "jvar.h"

using namespace jvar;

int main(int argc, char** argv)
{
    const char *test = ""
"{"
"  \"cmd\":\"sdata.json\","
"  \"arg_s\":1,"
"  \"time\":191451698,"
"  \"ybase\":2010,"
"  \"arg_m\":\"3\","
"  \"irms\":[5.8,0.0,0.0,0.0],"
"  \"vrms\":[121.8,0.1,0.1],"
"  \"watt\":[693.6,0.0,0.0],"
"  \"va\":[700.2,0.0,0.0],"
"  \"var_\":[-95.9,0.0,-0.0],"
"  \"power\":693.6,"
"  \"angle\":[172.1,119.9,140.4],"
"  \"period\":16668.0,"
"  \"freq\":59.0,"
"  \"energy\":42562362,"
"  \"watthr\":[42562362,0,0],"
"  \"vahr\":[46962595,0,0],"
"  \"varhr\":[-6319438,0,0],"
"  \"fwatthr\":[42668693,0,0],"
"  \"fvarhr\":[-6211040,0,0],"
"  \"energy\":42562362,"
"  \"emul\":2.608076793215e-03"
"}";

    Variant array;
    array.parseJson(test);

    puts(array.toString().c_str());
}

I get:

Error: jvar/src/var.cpp(559): failed to add property with key 'energy'

Nevertheless, the value is there and correctly displayed/accessed within jvar.

YasserAsmi commented 8 years ago

Hi Just tried this. The property 'energy' is being added for the second time. This is not allowed with addProperty() which doesn't allow duplicates. If you want to update the value of the property that might or might not exist already, please use addOrModifyProperty()

YasserAsmi commented 8 years ago

Additional thought: I don't know if JSON allows duplicate keys in an object. I don't think Javascript does. jvar will simply fail to add the duplicate key entry. However, I am not certain if that is the best behavior.

pnaulls commented 8 years ago

Right. I hadn't spotted that. The problem for me is that this is produced from a device over which I have no control. So ideally I need a way to suppress this message (in this particular case), or have a flag to allow it.

FWIW, the PHP json parser doesn't have a problem with this.

YasserAsmi commented 8 years ago

I checked Javascript behavior. It allows adding duplicate properties without any errors/warnings. It simply updates the value to the latest value. I will change the jvar behavior to match that. I will do it this weekend.