False negative on json parsing #24

Closed
pnaulls opened this Issue Jan 25, 2016 · 4 comments

Projects

None yet

2 participants

@pnaulls
pnaulls commented Jan 25, 2016

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 YasserAsmi self-assigned this Jan 28, 2016
@YasserAsmi
Owner

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 YasserAsmi closed this Jan 30, 2016
@YasserAsmi
Owner

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
pnaulls commented Feb 1, 2016

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 YasserAsmi reopened this Feb 7, 2016
@YasserAsmi
Owner

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.

@YasserAsmi YasserAsmi added a commit that referenced this issue Feb 7, 2016
@YasserAsmi Fix issue #24 a41b76b
@YasserAsmi YasserAsmi closed this Feb 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment