Wie Deserialisieren JSON-null zu einem NullNode anstelle von Java null?

Hinweis: Jackson 2.1.x.

Das problem ist ganz einfach, aber ich konnte keine Lösung finden bisher. Ich habe durchgestöbert vorhandene Dokumentation etc. und konnte nicht eine Antwort finden.

Die Basisklasse ist:

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op")

@JsonSubTypes({
    @Type(name = "add", value = AddOperation.class),
    @Type(name = "copy", value = CopyOperation.class),
    @Type(name = "move", value = MoveOperation.class),
    @Type(name = "remove", value = RemoveOperation.class),
    @Type(name = "replace", value = ReplaceOperation.class),
    @Type(name = "test", value = TestOperation.class)
})

public abstract class JsonPatchOperation
{
    /*
     * Note: no need for a custom deserializer, Jackson will try and find a
     * constructor with a single string argument and use it
     */
    protected final JsonPointer path;

    protected JsonPatchOperation(final JsonPointer path)
    {
        this.path = path;
    }

    public abstract JsonNode apply(final JsonNode node)
        throws JsonPatchException;

    @Override
    public String toString()
    {
        return "path = \"" + path + '"';
    }
}

Und die problematische Klasse ist das denn:

public abstract class PathValueOperation
    extends JsonPatchOperation
{
    protected final JsonNode value;

    protected PathValueOperation(final JsonPointer path, final JsonNode value)
    {
        super(path);
        this.value = value.deepCopy();
    }

    @Override
    public String toString()
    {
        return super.toString() + ", value = " + value;
    }
}

Wenn ich versuche zu Deserialisieren:

{ "op": "add", "path": "/x", "value": null }

Ich würde gerne den Wert null deserialisiert werden als NullNode, nicht Java-null. Und ich konnte nicht einen Weg finden, es zu tun bisher.

Wie wollen Sie das erreichen?

(Hinweis: alle Konstruktoren der konkreten Klassen sind @JsonCreators mit den entsprechenden @JsonProperty Anmerkungen -- Sie arbeiten ohne problem, mein problem ist nur JSON-Behandlung von null)

InformationsquelleAutor fge | 2013-03-08
Schreibe einen Kommentar