Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper Unexpected character - JSON is valid

I am trying to write/read a simple class to a file using Jackson and I can't get read the file after I create it. I get

org.codehaus.jackson.JsonParseException: Unexpected character ('.' (code 46)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.StringReader@f2dec59; line: 1, column: 2]

My object is pretty straightforward; it's basically just a container for a HashMap. Here is the resulting JSON file that I checked out with JSONLint:

{
"quaternions": {
    "10": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 1,
        "identity": true
    },
    "11": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 1,
        "identity": true
    },
    "12": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 0,
        "identity": false
    }
}
}

The code I am using to read the file is as follows:

TypeReference<ZeroQuaternions> typeRef;
typeRef = new TypeReference<ZeroQuaternions>() {};
ZeroQuaternions readQuats = mapper.readValue("./zeroQuatTest.json", typeRef);
like image 444
tomsrobots Avatar asked Nov 10 '15 17:11

tomsrobots


1 Answers

You have this error because jackson tries to deserialize ./zeroQuatTest.json string instead of content of your file. Try to call

TypeReference<ZeroQuaternions> typeRef;
typeRef = new TypeReference<ZeroQuaternions>() {};
ZeroQuaternions readQuats = mapper.readValue(new File("./zeroQuatTest.json"), typeRef);
like image 101
Nikita Magonov Avatar answered Sep 22 '22 05:09

Nikita Magonov