I have the following two classes:
public class MyChild
{
@JsonProperty
public int x;
}
public class MyTest
{
public void MyChild() {}
@JsonSerialize(using = MapValueSerializer.class)
private Map<String, MyChild> childs = new LinkedHashMap<>();
}
where I want childs to serialize as an array of MyChild
and not as a map (values only). Thus, I use the following custom serializer:
public class MapValueSerializer extends StdSerializer<Map<String, ?>>
{
protected MapValueSerializer()
{
this(null);
}
protected MapValueSerializer(Class<Map<String, ?>> t)
{
super(t);
}
@Override
public void serialize(Map<String, ?> value, JsonGenerator gen, SerializerProvider provider) throws IOException
{
provider.defaultSerializeValue(value.values(), gen);
}
}
When I now use the JsonSchemaGenerator
to generate a schema from MyTest
, I get the following:
{
"type" : "object",
"id" : "urn:jsonschema:com:myclasses:MyTest",
"properties" : {
"childs" : {
"type" : "any"
}
}
}
But childs shouldn't be of type "any" but of type "object". If I remove the serializer, the type is "object". Do I have to add something to make the schema generator aware of that type?
It works after I overwrite acceptJsonFormatVisitor()
in my MapValueSerializer
class with:
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
{
JavaType valueType = ((MapType) typeHint).getContentType();
visitor.getProvider().findValueSerializer(valueType).acceptJsonFormatVisitor(visitor, valueType);
}
Then child is of type "object" and also the subelement "x" is generated in the schema.