Skip to content

Json deserialization of class with no default constructor and referenced field in the provided constructor #874

@larjohn

Description

@larjohn

From @larjohn on July 17, 2015 12:8

Hi,

I am using mysema QueryDSL for persistence-ORM functionality. I would like to store built query objects together in JSON format, so that I can load and reuse them. As most of the classes in QueryDSL are abstract, and I would not modify them, I have used mixins to configure their JSON serialization/deserialization:

My issue is with the PathMetadata mixin. The original PathMetadata class can be seen here:

https://github.com/querydsl/querydsl/blob/master/querydsl-core/src/main/java/com/querydsl/core/types/PathMetadata.java

The mixin that I have created for this class is the following:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathType;

import javax.annotation.Nullable;

public abstract class PathMetadataMixin {

    @JsonProperty
    private Path<?>parent;

    @JsonIgnore
    private Path<?>root;


    @JsonCreator
    public PathMetadataMixin(@JsonProperty("parent")  @Nullable Path<?> parent, 
@JsonProperty("element") Object element, @JsonProperty("type") PathType type ) {

    }

}

When I serialize an instance of this PathMetadata class, everything works as expected and it is outputted either as a whole, or with the ref attribute if it is already there. But when I try to deserialize the object, it throws a crypric exception:

Caused by: java.lang.ArrayStoreException: com.fasterxml.jackson.databind.deser.impl.ObjectIdReferenceProperty

I have found that this exception is fired here:

/com/fasterxml/jackson/core/jackson-databind/2.6.0-rc1/jackson-databind-2.6.0-rc1-sources.jar!/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java:485

Actually, in this line

creatorProps[i] = prop;

creatorProps is an array of CreatorProperty objects (although it is declared more generally) and prop is an ObjectIdReferenceProperty. Thus it cannot be added to the array.

How can I solve this?

Copied from original issue: jsog/jsog-jackson#7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions