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?

0

3 Answers 3

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;
    }
  });
} 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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;
}

1 Comment

Thank for your replay. I'm looking for more generic solution, since those types will appear also on other nodes and not only "removedVertices"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.