Reputation: 33
I've been working with Jackson to deserialize data. In general it's been a smooth process.
What has me stumped is trying to deserialize something that has a known amount of objects into a custom class when the JSON properties lack names.
Overall, I'm deserializing a map of objectId:fullObject
{
"1411842351335": {"vers": [], "name": "Basic (and reversed card)", "tags": [], "did": 1, "usn": -1, "req": [[0, "all", [0]], [1, "all", [1]]], "flds": [{"size": 20, "name": "Front", "media": [], "rtl": false, "ord": 0, "font": "Arial", "sticky": false}, {"size": 20, "name": "Back", "media": [], "rtl": false, "ord": 1, "font": "Arial", "sticky": false}], "sortf": 0, "latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n", "tmpls": [{"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}", "name": "Card 1", "qfmt": "{{Front}}", "did": null, "ord": 0, "bafmt": "", "bqfmt": ""}, {"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}", "name": "Card 2", "qfmt": "{{Back}}", "did": null, "ord": 1, "bafmt": "", "bqfmt": ""}], "latexPost": "\\end{document}", "type": 0, "id": "1411842351335", "css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n", "mod": 1411842351}}
Each object gets mapped into a class with general structure that follows:
public class AnkiNoteType {
...
private List<AnkiNoteTypeReq> req;
...
}
With the problematic object
package com.openwebsrs.libanki.entities;
public class AnkiNoteTypeReq {
...
private Integer a;
private String b;
private List<Integer> c;
...
}
The problem I've been having here and in other situations is mapping a list of objects without names (that is, no "name:value" just "value") into a specified class.
"req": [[0, "all", [0]], [1, "all", [1]]]
What I've been doing can be seen here in implementation with important bits below:
@Override
public List<AnkiNoteType> getAnkiNoteTypes(String noteTypesString) throws IOException {
Map<Long, AnkiNoteType> noteTypeMap = objectMapper.readValue(noteTypesString, new TypeReference<Map<Long, AnkiNoteType>>() {
});
return new ArrayList(noteTypeMap.values());
}
I don't need a full solution. I just need to get on the right track.
Thanks for your time!
Upvotes: 3
Views: 232
Reputation: 99993
You might be able to use http://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonFormat.Shape.html to ask Jackson to map directly to an array. In this mode, jackson requires you to annotate the order of the properties, and maps from a json array to an object.
Upvotes: 1
Reputation: 29240
If the individual items aren't using name value pairs, then you'll need to write some kind of custom deserializer that understands the relationship between the array offsets in this:
[0, "all", [0]]
And maps them to the properties in the AnkiNoteTypeReq
. I know there is a mechanism in Jackson where you can specify that a given type should be deserialized using a custom class, but I don't remember the exact syntax. As a first pass I would use Jackson to deserialize the JSON string to a JsonNode and do manual deserialization by creating a function like
static AnkiNoteTypeReq fromJsonNode(JsonNode node) {
...
}
Upvotes: 0