Действительно трудно написать десериализатор для недопустимой JSON
полезной нагрузки с ожидаемым результатом во всех случаях. JSON
полезная нагрузка со многими вложенными JSON Objects
и JSON Arrays
может быть разбита разными способами. В вашем случае это довольно простая структура, и она должна быть проста в обслуживании.
Итак, вам нужно реализовать специальный десериализатор List
, который может выглядеть следующим образом:
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./src/main/resources/test.json");
SimpleModule skipInvalidObjectsModule = new SimpleModule();
skipInvalidObjectsModule.addDeserializer(List.class, new ListJsonDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(skipInvalidObjectsModule);
List<Map<String, Object>> result = mapper.readValue(jsonFile, List.class);
System.out.println(result);
}
}
class ListJsonDeserializer extends JsonDeserializer<List<Object>> {
@Override
public List<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
MapType mapType = ctxt.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
JsonDeserializer<Object> mapDeserializer = ctxt.findRootValueDeserializer(mapType);
List<Object> result = new ArrayList<>();
p.nextToken();
while (p.currentToken() != JsonToken.END_ARRAY) {
if (p.currentToken() == JsonToken.START_OBJECT) {
try {
result.add(mapDeserializer.deserialize(p, ctxt));
} catch (JsonParseException e) {
System.err.println(e.getMessage());
}
}
try {
p.nextToken();
} catch (JsonParseException e) {
System.err.println(e.getMessage());
}
}
return result;
}
}
Выше кода печатаетсяпервые ошибки:
Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value
at [Source: (File); line: 6, column: 27]
Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 29]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 30]
Unexpected character ('q' (code 113)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 31]
Unexpected character ('u' (code 117)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 32]
Unexpected character ('a' (code 97)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 33]
Unexpected character ('l' (code 108)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 34]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 35]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 36]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 37]
Unexpected character ('i' (code 105)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 38]
Unexpected character ('g' (code 103)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 39]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 40]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 41]
Unexpected character ('i' (code 105)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 42]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 43]
Unexpected character ('s' (code 115)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 44]
Unexpected character ('t' (code 116)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 45]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 46]
Unexpected character ('a' (code 97)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 47]
Unexpected character ('d' (code 100)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 48]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 49]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 50]
Unexpected character ('f' (code 102)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 51]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 52]
Unexpected character ('c' (code 99)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 53]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 54]
Unexpected character ('l' (code 108)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 55]
Unexpected character ('o' (code 111)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 56]
Unexpected character ('n' (code 110)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 57]
Unexpected character ('_' (code 95)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 58]
Unexpected character ('h' (code 104)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 59]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 60]
Unexpected character ('r' (code 114)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 61]
Unexpected character ('e' (code 101)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 62]
Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (File); line: 6, column: 63]
и, наконец, результат:
[{someField=this_object_seems_to_be_ok}]