Search in sources :

Example 11 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project jsonschema2pojo by joelittlejohn.

the class SchemaGenerator method simpleTypeSchema.

private ObjectNode simpleTypeSchema(JsonNode exampleValue) {
    try {
        Object valueAsJavaType = this.objectMapper.treeToValue(exampleValue, Object.class);
        SerializerProvider serializerProvider = new DefaultSerializerProvider.Impl().createInstance(this.objectMapper.getSerializationConfig(), BeanSerializerFactory.instance);
        if (valueAsJavaType == null) {
            SchemaAware valueSerializer = NullSerializer.instance;
            return (ObjectNode) valueSerializer.getSchema(serializerProvider, null);
        } else if (valueAsJavaType instanceof Long) {
            // longs are 'integers' in schema terms
            SchemaAware valueSerializer = (SchemaAware) serializerProvider.findValueSerializer(Integer.class, null);
            ObjectNode schema = (ObjectNode) valueSerializer.getSchema(serializerProvider, null);
            schema.put("minimum", Long.MAX_VALUE);
            return schema;
        } else {
            Class<? extends Object> javaTypeForValue = valueAsJavaType.getClass();
            SchemaAware valueSerializer = (SchemaAware) serializerProvider.findValueSerializer(javaTypeForValue, null);
            return (ObjectNode) valueSerializer.getSchema(serializerProvider, null);
        }
    } catch (JsonProcessingException e) {
        throw new GenerationException("Unable to generate a schema for this json example: " + exampleValue, e);
    }
}
Also used : SchemaAware(com.fasterxml.jackson.databind.jsonschema.SchemaAware) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultSerializerProvider(com.fasterxml.jackson.databind.ser.DefaultSerializerProvider) GenerationException(org.jsonschema2pojo.exception.GenerationException) DefaultSerializerProvider(com.fasterxml.jackson.databind.ser.DefaultSerializerProvider) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 12 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project dhis2-core by dhis2.

the class CustomLastUpdatedUserSerializerTest method serializeXml.

@Test
void serializeXml() throws Exception {
    Writer jsonWriter = new StringWriter();
    ToXmlGenerator jsonGenerator = new XmlFactory().createGenerator(jsonWriter);
    SerializerProvider serializerProvider = new ObjectMapper().getSerializerProvider();
    jsonGenerator.setNextName(new QName("urn:test", "lastUpdatedBy"));
    new CustomLastUpdatedUserSerializer().serialize(user, jsonGenerator, serializerProvider);
    jsonGenerator.flush();
    assertEquals("<wstxns1:lastUpdatedBy xmlns:wstxns1=\"urn:test\" id=\"jshfdkd323\" name=\"Peter Brown\"/>", jsonWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) QName(javax.xml.namespace.QName) XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) ToXmlGenerator(com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 13 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project sts4 by spring-projects.

the class JSON method pathAsJson.

private static SimpleModule pathAsJson() {
    SimpleModule m = new SimpleModule();
    m.addSerializer(Path.class, new JsonSerializer<Path>() {

        @Override
        public void serialize(Path path, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            gen.writeString(path.toString());
        }
    });
    m.addDeserializer(Path.class, new JsonDeserializer<Path>() {

        @Override
        public Path deserialize(JsonParser parse, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            return Paths.get(parse.getText());
        }
    });
    return m;
}
Also used : Path(java.nio.file.Path) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 14 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project cxf by apache.

the class JAXRSNonSpringJaxrsServletTest method testFeatureOnResourceClassUsingApplication.

@Test
public void testFeatureOnResourceClassUsingApplication() throws Exception {
    final JsonSerializer<Object> defaultNullKeySerializer = Json.mapper().getSerializerProvider().getDefaultNullKeySerializer();
    try {
        // Swagger Core v3 does not interpret FormParam("") properly, sets property key as 'null' and fails the
        // serialization with "Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)"
        Json.mapper().getSerializerProvider().setNullKeySerializer(new JsonSerializer<Object>() {

            @Override
            public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                gen.writeFieldName("");
            }
        });
        String address = "http://localhost:" + PORT2 + "/bookstore/;JSESSIONID=xxx";
        WebClient wc = WebClient.create(address);
        Book book = wc.get(Book.class);
        assertEquals(124L, book.getId());
        assertEquals("root", book.getName());
        // Check OpenAPI feature is working correctly
        wc = WebClient.create("http://localhost:" + PORT2 + "/openapi.json");
        Response openAPIResponse = wc.get();
        assertEquals(200, openAPIResponse.getStatus());
    } finally {
        if (defaultNullKeySerializer != null) {
            Json.mapper().getSerializerProvider().setNullKeySerializer(defaultNullKeySerializer);
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.systest.jaxrs.Book) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 15 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.

the class DescriptionFactoryWrapper method expectAnyFormat.

@Override
public JsonAnyFormatVisitor expectAnyFormat(JavaType type) throws JsonMappingException {
    SerializerProvider p = getProvider();
    JsonSerializer<Object> s = p.findValueSerializer(type);
    s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(this, getJsonPointer(), p), type);
    return super.expectAnyFormat(type);
}
Also used : SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Aggregations

SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)26 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)20 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)11 IOException (java.io.IOException)9 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Version (com.fasterxml.jackson.core.Version)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)4 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)3 Test (org.junit.Test)3 Page (com.day.cq.wcm.api.Page)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)2 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 JavaType (com.fasterxml.jackson.databind.JavaType)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SimpleDate (com.google.api.server.spi.types.SimpleDate)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.jupiter.api.Test)2