ignorieren unbekannte Felder über JSON-Objekte mit jackson

Ich bin mit jackson 2.x für die Serialisierung und Deserialisierung. Ich bin registriert, die objectMapper zu den Nachbrenner Modul und Konfiguration der Objekt-mapper ignorieren unbekannten Eigenschaften

objectMapper.registerModule(new AfterBurnerModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

aber wenn es versucht, ein Objekt serialisieren es nicht mit unbekannten Feld gefunden, für die Attribut-Fehler

Java-Objekt annotiert mit @JsonIgnoreProperties(ignoreUnknown = true)

Kann jemand mir helfen zu verstehen, was vielleicht falsch läuft

Unten finden Sie die Util-Klasse

package example;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

public final class Util {
    private static ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
        objectMapper.registerModule(new AfterburnerModule());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        objectMapper.setDateFormat(sdf);
        objectMapper.setAnnotationIntrospector(AnnotationIntrospector.pair(new JaxbAnnotationIntrospector(objectMapper.getTypeFactory()), new JacksonAnnotationIntrospector()));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(Include.NON_NULL);

    }

    private Util() {
    }

    public static <T> T convertToObject(String jsonString,Class<T> classType){
        T obj = null;
        try {
            obj = objectMapper.readValue(jsonString, classType);
        } catch (Exception e) {

        } 
        return obj;
    }

    public static String convertToString(Object obj)
            throws IOException {
        return objectMapper.writeValueAsString(obj);
    }

}

enum-Klasse NumberEnum

package sample;

public enum NumberEnum {
ONE, TWO
}

Klasse A

package sample;

@JsonIgnoreProperties(ignoreUnknown = true)
public class A {
@JsonProperty
    private NumberEnum number;
}

den code, wo ich bin Deserialisieren ist als unten

A a = Util.convertToObject(str, A.class);

und der string, den ich versuche zu deserailize ist wie folgt:

{
  "number": "Num"
}

Unten ist der Fehler beim Deserialisieren:

com.fasterxml.jackson.databind-Methode.exc.InvalidFormatException: Kann nicht bauen Instanz der Probe.NumberEnum von der String-Wert 'Num': Wert nicht einer der deklarierten Enum-Instanz Namen: [EINS, ZWEI]
bei (durch die Referenz-Kette: Probe.["Ein"]->Probe.NumberEnum["Anzahl"])

die Klasse A ist importiert aus einem Glas und es ist mit jackson-Bibliothek 1.9

Kann Ihnen mehr Informationen über die Fehler sowie ein code-snippet? Ignorieren von unbekannten Eigenschaften ist relevant für Deserialisierung, aber Sie versuchen, ein Objekt serialisieren, also sollte es nicht ins Spiel kommen.
Hinzugefügt mehr details

InformationsquelleAutor java2890 | 2016-02-24

Schreibe einen Kommentar