com.fasterxml.jackson.module.jsonSchema.factories.ObjectVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module-jsonSchema Show documentation
Show all versions of jackson-module-jsonSchema Show documentation
Add-on module for Jackson (http://jackson.codehaus.org) to support
JSON Schema (http://tools.ietf.org/html/draft-zyp-json-schema-03) version 3 generation.
package com.fasterxml.jackson.module.jsonSchema.factories;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;
import com.fasterxml.jackson.module.jsonSchema.types.ReferenceSchema;
public class ObjectVisitor extends JsonObjectFormatVisitor.Base
implements JsonSchemaProducer, Visitor
{
protected final ObjectSchema schema;
protected SerializerProvider provider;
private WrapperFactory wrapperFactory;
private VisitorContext visitorContext;
/**
* @deprecated Since 2.4; call constructor that takes {@link WrapperFactory}
*/
@Deprecated
public ObjectVisitor(SerializerProvider provider, ObjectSchema schema) {
this(provider, schema, new WrapperFactory());
}
public ObjectVisitor(SerializerProvider provider, ObjectSchema schema, WrapperFactory wrapperFactory) {
this.provider = provider;
this.schema = schema;
this.wrapperFactory = wrapperFactory;
}
/*
/*********************************************************************
/* JsonSchemaProducer
/*********************************************************************
*/
@Override
public ObjectSchema getSchema() {
return schema;
}
/*
/*********************************************************************
/* JsonObjectFormatVisitor impl
/*********************************************************************
*/
@Override
public SerializerProvider getProvider() {
return provider;
}
/**
* @deprecated Construct instances with provider instead
*/
@Deprecated
@Override
public void setProvider(SerializerProvider p) {
provider = p;
}
public WrapperFactory getWrapperFactory() {
return wrapperFactory;
}
/**
* @deprecated Construct instances with provider instead
*/
@Deprecated
public void setWrapperFactory(WrapperFactory wrapperFactory) {
this.wrapperFactory = wrapperFactory;
}
@Override
public void optionalProperty(BeanProperty prop) throws JsonMappingException {
schema.putOptionalProperty(prop, propertySchema(prop));
}
@Override
public void optionalProperty(String name, JsonFormatVisitable handler, JavaType propertyTypeHint)
throws JsonMappingException {
schema.putOptionalProperty(name, propertySchema(handler, propertyTypeHint));
}
@Override
public void property(BeanProperty prop) throws JsonMappingException {
schema.putProperty(prop, propertySchema(prop));
}
@Override
public void property(String name, JsonFormatVisitable handler, JavaType propertyTypeHint)
throws JsonMappingException {
schema.putProperty(name, propertySchema(handler, propertyTypeHint));
}
protected JsonSchema propertySchema(BeanProperty prop)
throws JsonMappingException
{
if (prop == null) {
throw new IllegalArgumentException("Null property");
}
// check if we've seen this argument's sub-schema already and return a reference-schema if we have
String seenSchemaUri = visitorContext.getSeenSchemaUri(prop.getType());
if (seenSchemaUri != null) {
return new ReferenceSchema(seenSchemaUri);
}
SchemaFactoryWrapper visitor = wrapperFactory.getWrapper(getProvider(), visitorContext);
JsonSerializer