ishkin / Proton

IBM Proactive Technology Online
24 stars 21 forks source link

Number values enclosed by quotes in JSONNgsiFormatter #43

Closed gioadami closed 7 years ago

gioadami commented 7 years ago

Hi, it seems that there's a problem in the JSONNgsiFormatter formatInstance method. When generating the json, all the attrValues are enclosed by ", regardless of their actual types. The problem arise when Orion receive a PATCH request containing an attribute whose value is enclosed by quotes, as it overrides its original type and set it to String.

I made this change on my local version and it seems to fix the problem:

Proton/IBM Proactive Technology Online/ProtonJ2SE/src/com/ibm/hrl/proton/adapters/formatters/JSONNgsiFormatter.java line 105

if (attrValue != null && !attrValue.isEmpty()){
    boolean isDate = ((value instanceof Long) && (instance.getFieldMetaData(attrName).getType().equals(AttributeTypesEnum.DATE.toString())));
    if (isDate)
    {
        //convert this long to Date using formatter's date format
        attrValue = formatTimestamp((Long)value);
    }
    if (firstAttribute == false) jsonString = jsonString.concat(", \n");
    boolean isNumber = value instanceof Number && !isDate;
    String jsonAttr = "\""+attrName + "\" : { \n"
            +  "\"value\" : " + (isNumber?"":"\"") + attrValue + (isNumber?"":"\"") + "\n"
            +  "} \n";
    jsonString = jsonString.concat(jsonAttr);
    firstAttribute = false;
}

(I'm not sure it fixes json generation for all types as I didn't run a test for all of them)

Thank you.

ishkin commented 7 years ago

Hi, and thanks! In general this is the convention with Proton... However this particular formatter was created for usage with Orion... I wasn't aware of Orion's treatment of such strings. So this fix makes complete sense, if you create a pull request I can merge it into stream.