0

The server I'm working with changed the REST format from plain JSON:

    {           
        "removedVertices": [
            {
                "id": "1",
                "info": {
                    "host": "myhost",
                    "port": "1111"
                },
                "name": "Roy",
                "type": "Worker"
            }
        ],
        "id": "2",
        "time": 1481183401573
    }

To Jackson format:

    {
          "removedVertices": [
          "java.util.ArrayList",
           [
                   {
                         "id": "1",
                          "info": [
                                "java.util.HashMap",
                                {
                                    "host": "myhost",
                                    "port": "1111"
                                }
                         ]
                         "name": "Roy",
                         "type": "Worker",                             
                    }                       
            ]
            "id": "2",
            "time": 1482392323858
    }

How can I parse it the way it was before in Angular/Javascript?

Amir Katz
  • 1,027
  • 1
  • 10
  • 24

3 Answers3

1

If the api should be restful, then the server should not return none plain json results. I think the server site need to fix that.

I think it is because the server enabled the Polymorphic Type Handling feature. Read Jackson Default Typing for object containing a field of Map and JacksonPolymorphicDeserialization.

Disable the feature and you will get result identical to plain json.

Community
  • 1
  • 1
bresai
  • 369
  • 5
  • 18
1

Assuming only arrays are affected, I would use underscore.js and write a recursive function to remove the Jackson type.

function jackson2json(input) {
  return _.mapObject(input, function(val, key) {
    if (_.isArray(val) && val.length > 1) {
      // discard the Jackson type and keep the 2nd element of the array
      return val[1];   
    }
    else if (_.isObject(val)) {
      // apply the transformation recursively
      return jackson2json(val);
    }
    else {
      // keep the value unchanged (i.e. primitive types)
      return val;
    }
  });
} 
M. F.
  • 1,654
  • 11
  • 16
0

The main difference i see is that in arrays you have an additional string element at index 0.

If you always get the same structure you can do like this:

function jacksonToJson(jackson) {
    jackson.removedVertices.splice(0, 1);

    jackson.removedVertices.forEach((rmVert) => {
      rmVert.info.splice(0, 1);
    });

    return jackson;
}
pietrovismara
  • 6,102
  • 5
  • 33
  • 45
  • Thank for your replay. I'm looking for more generic solution, since those types will appear also on other nodes and not only "removedVertices" – Amir Katz Dec 22 '16 at 10:08