public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType) { ObjectNode localObjectNode = createSchemaNode("array", true); if (paramType != null) { JavaType localJavaType = paramSerializerProvider.constructType(paramType); if (localJavaType.isArrayType()) { Class localClass = ((ArrayType)localJavaType).getContentType().getRawClass(); if (localClass == Object.class) { localObjectNode.put("items", JsonSchema.getDefaultSchemaNode()); return localObjectNode; } JsonSerializer localJsonSerializer = paramSerializerProvider.findValueSerializer(localClass, this._property); JsonNode localJsonNode; if ((localJsonSerializer instanceof SchemaAware)) localJsonNode = ((SchemaAware)localJsonSerializer).getSchema(paramSerializerProvider, null); else localJsonNode = JsonSchema.getDefaultSchemaNode(); localObjectNode.put("items", localJsonNode); } } return localObjectNode; }
public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType) { ObjectNode localObjectNode1 = createSchemaNode("object", true); if ((paramType instanceof ParameterizedType)) { Type[] arrayOfType = ((ParameterizedType)paramType).getActualTypeArguments(); if (arrayOfType.length == 2) { JavaType localJavaType1 = paramSerializerProvider.constructType(arrayOfType[0]); JavaType localJavaType2 = paramSerializerProvider.constructType(arrayOfType[1]); ObjectNode localObjectNode2 = JsonNodeFactory.instance.objectNode(); for (Enum localEnum : (Enum[])localJavaType1.getRawClass().getEnumConstants()) { JsonSerializer localJsonSerializer = paramSerializerProvider.findValueSerializer(localJavaType2.getRawClass(), this._property); JsonNode localJsonNode; if ((localJsonSerializer instanceof SchemaAware)) localJsonNode = ((SchemaAware)localJsonSerializer).getSchema(paramSerializerProvider, null); else localJsonNode = JsonSchema.getDefaultSchemaNode(); localObjectNode2.put(paramSerializerProvider.getConfig().getAnnotationIntrospector().findEnumValue(localEnum), localJsonNode); } localObjectNode1.put("properties", localObjectNode2); } } return localObjectNode1; }
/** * The method to be called by {@link ObjectMapper} and {@link ObjectWriter} * to generate <a href="http://json-schema.org/">JSON schema</a> for * given type. * * @param type The type for which to generate schema */ public JsonSchema generateJsonSchema(Class<?> type) throws JsonMappingException { if (type == null) { throw new IllegalArgumentException("A class must be provided"); } /* no need for embedded type information for JSON schema generation (all * type information it needs is accessible via "untyped" serializer) */ JsonSerializer<Object> ser = findValueSerializer(type, null); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(this, null) : JsonSchema.getDefaultSchemaNode(); if (!(schemaNode instanceof ObjectNode)) { throw new IllegalArgumentException("Class " + type.getName() + " would not be serialized as a JSON object and therefore has no schema"); } return new JsonSchema((ObjectNode) schemaNode); }
@Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { ObjectNode o = createSchemaNode("array", true); if (typeHint != null) { JavaType javaType = provider.constructType(typeHint); if (javaType.isArrayType()) { Class<?> componentType = ((ArrayType) javaType).getContentType().getRawClass(); // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped? if (componentType == Object.class) { o.put("items", JsonSchema.getDefaultSchemaNode()); } else { JsonSerializer<Object> ser = provider.findValueSerializer(componentType, _property); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(provider, null) : JsonSchema.getDefaultSchemaNode(); o.put("items", schemaNode); } } } return o; }
@SuppressWarnings("unchecked") @Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { ObjectNode o = createSchemaNode("object", true); if (typeHint instanceof ParameterizedType) { Type[] typeArgs = ((ParameterizedType) typeHint).getActualTypeArguments(); if (typeArgs.length == 2) { JavaType enumType = provider.constructType(typeArgs[0]); JavaType valueType = provider.constructType(typeArgs[1]); ObjectNode propsNode = JsonNodeFactory.instance.objectNode(); Class<Enum<?>> enumClass = (Class<Enum<?>>) enumType.getRawClass(); for (Enum<?> enumValue : enumClass.getEnumConstants()) { JsonSerializer<Object> ser = provider.findValueSerializer(valueType.getRawClass(), _property); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(provider, null) : JsonSchema.getDefaultSchemaNode(); propsNode.put(provider.getConfig().getAnnotationIntrospector().findEnumValue((Enum<?>)enumValue), schemaNode); } o.put("properties", propsNode); } } return o; }
/** * Attempt to add the output of the given {@link BeanPropertyWriter} in the given {@link ObjectNode}. * Otherwise, add the default schema {@link JsonNode} in place of the writer's output * * @param propertiesNode Node which the given property would exist within * @param provider Provider that can be used for accessing dynamic aspects of serialization * processing * * {@link BeanPropertyFilter#depositSchemaProperty(BeanPropertyWriter, ObjectNode, SerializerProvider)} * * @since 2.1 */ public void depositSchemaProperty(ObjectNode propertiesNode, SerializerProvider provider) throws JsonMappingException { JavaType propType = getSerializationType(); // 03-Dec-2010, tatu: SchemaAware REALLY should use JavaType, but alas it doesn't... Type hint = (propType == null) ? getGenericPropertyType() : propType.getRawClass(); JsonNode schemaNode; // Maybe it already has annotated/statically configured serializer? JsonSerializer<Object> ser = getSerializer(); if (ser == null) { // nope Class<?> serType = getRawSerializationType(); if (serType == null) { serType = getPropertyType(); } ser = provider.findValueSerializer(serType, this); } boolean isOptional = !isRequired(provider.getAnnotationIntrospector()); if (ser instanceof SchemaAware) { schemaNode = ((SchemaAware) ser).getSchema(provider, hint, isOptional) ; } else { schemaNode = JsonSchema.getDefaultSchemaNode(); } propertiesNode.put(getName(), schemaNode); }
public JsonSchema generateJsonSchema(Class<?> paramClass) { if (paramClass == null) throw new IllegalArgumentException("A class must be provided"); JsonSerializer localJsonSerializer = findValueSerializer(paramClass, null); JsonNode localJsonNode1; if ((localJsonSerializer instanceof SchemaAware)) localJsonNode1 = ((SchemaAware)localJsonSerializer).getSchema(this, null); else localJsonNode1 = JsonSchema.getDefaultSchemaNode(); JsonNode localJsonNode2 = localJsonNode1; if (!(localJsonNode1 instanceof ObjectNode)) throw new IllegalArgumentException("Class " + paramClass.getName() + " would not be serialized as a JSON object and therefore has no schema"); return new JsonSchema((ObjectNode)localJsonNode2); }
public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType) { ObjectNode localObjectNode = createSchemaNode("array", true); Object localObject = null; if (paramType != null) { JavaType localJavaType = paramSerializerProvider.constructType(paramType).getContentType(); localObject = localJavaType; if ((localJavaType == null) && ((paramType instanceof ParameterizedType))) { Type[] arrayOfType = ((ParameterizedType)paramType).getActualTypeArguments(); if (arrayOfType.length == 1) localObject = paramSerializerProvider.constructType(arrayOfType[0]); } } if ((localObject == null) && (this._elementType != null)) localObject = this._elementType; if (localObject != null) { Class localClass = ((JavaType)localObject).getRawClass(); JsonNode localJsonNode = null; if (localClass != Object.class) { JsonSerializer localJsonSerializer = paramSerializerProvider.findValueSerializer((JavaType)localObject, this._property); boolean bool = localJsonSerializer instanceof SchemaAware; localJsonNode = null; if (bool) localJsonNode = ((SchemaAware)localJsonSerializer).getSchema(paramSerializerProvider, null); } if (localJsonNode == null) localJsonNode = JsonSchema.getDefaultSchemaNode(); localObjectNode.put("items", localJsonNode); } return localObjectNode; }
public void depositSchemaProperty(ObjectNode paramObjectNode, SerializerProvider paramSerializerProvider) { JavaType localJavaType = getSerializationType(); Object localObject; if (localJavaType == null) localObject = getGenericPropertyType(); else localObject = localJavaType.getRawClass(); JsonSerializer localJsonSerializer1 = getSerializer(); JsonSerializer localJsonSerializer2 = localJsonSerializer1; if (localJsonSerializer1 == null) { Class localClass1 = getRawSerializationType(); Class localClass2 = localClass1; if (localClass1 == null) localClass2 = getPropertyType(); localJsonSerializer2 = paramSerializerProvider.findValueSerializer(localClass2, this); } boolean bool; if (!isRequired()) bool = true; else bool = false; JsonNode localJsonNode; if ((localJsonSerializer2 instanceof SchemaAware)) localJsonNode = ((SchemaAware)localJsonSerializer2).getSchema(paramSerializerProvider, (Type)localObject, bool); else localJsonNode = JsonSchema.getDefaultSchemaNode(); paramObjectNode.put(getName(), localJsonNode); }
@Test public void exampleCode() throws Exception { ObjectMapper om = new ObjectMapper(); om.configure(SerializationFeature.INDENT_OUTPUT, true); Geocoder geocoder = new Geocoder(); System.out.println(om.writeValueAsString(geocoder.resolve("Rancho Cordova, US"))); System.out.println(om.writeValueAsString(geocoder.resolve("Москва является удивительным"))); JsonSchema schema = om.generateJsonSchema(Location.class); System.out.println(schema.toString()); }
@Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { return (_valueSerializer instanceof SchemaAware) ? ((SchemaAware) _valueSerializer).getSchema(provider, null) : JsonSchema.getDefaultSchemaNode(); }
@Test public void testWriteSchema() throws Exception { JsonSchema jsonSchema = objectMapper.generateJsonSchema(Page.class); }
public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType) { if ((this._valueSerializer instanceof SchemaAware)) return ((SchemaAware)this._valueSerializer).getSchema(paramSerializerProvider, null); return JsonSchema.getDefaultSchemaNode(); }
@Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { /* 15-Jan-2010, tatu: This should probably be rewritten, given that * more information about content type is actually being explicitly * passed. So there should be less need to try to re-process that * information. */ ObjectNode o = createSchemaNode("array", true); JavaType contentType = null; if (typeHint != null) { JavaType javaType = provider.constructType(typeHint); contentType = javaType.getContentType(); if (contentType == null) { // could still be parametrized (Iterators) if (typeHint instanceof ParameterizedType) { Type[] typeArgs = ((ParameterizedType) typeHint).getActualTypeArguments(); if (typeArgs.length == 1) { contentType = provider.constructType(typeArgs[0]); } } } } if (contentType == null && _elementType != null) { contentType = _elementType; } if (contentType != null) { JsonNode schemaNode = null; // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped? if (contentType.getRawClass() != Object.class) { JsonSerializer<Object> ser = provider.findValueSerializer(contentType, _property); if (ser instanceof SchemaAware) { schemaNode = ((SchemaAware) ser).getSchema(provider, null); } } if (schemaNode == null) { schemaNode = JsonSchema.getDefaultSchemaNode(); } o.put("items", schemaNode); } return o; }
/** * Generate <a href="http://json-schema.org/">Json-schema</a> * instance for specified class. * * @param t The class to generate schema for * @return Constructed JSON schema. */ public JsonSchema generateJsonSchema(Class<?> t) throws JsonMappingException { return _serializerProvider(getSerializationConfig()).generateJsonSchema(t); }