List of usage examples for com.fasterxml.jackson.core Version Version
public Version(int major, int minor, int patchLevel, String snapshotInfo, String groupId, String artifactId)
From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableModule.java
/** * Determines the version or return {@link Version#unknownVersion() unknownVersion} if this could not be determined. * * @param groupId The group ID of the maven dependency to determine the version for. * @param artifactId The artifact ID of the maven dependency to determine the version for. * @return The version of the dependency. *//* w w w. j a v a 2 s. c o m*/ private static Version determineVersion(String groupId, String artifactId) { Version version = Version.unknownVersion(); MavenVersion mavenVersion = MavenVersion.forDependency(groupId, artifactId); if (mavenVersion != null) { version = new Version(mavenVersion.getMajor(), mavenVersion.getMinor(), mavenVersion.getIncrement(), mavenVersion.getSuffix(), groupId, artifactId); } return version; }
From source file:synapticloop.scaleway.api.ScalewayApiClient.java
/** * Instantiate a new API Client for the Scaleway API Provider, each API * client points to a specific Region. Once this region is set, it cannot * be changed. Instead instantiate a new API client for the new region, * * @param accessToken the access token that is authorised to invoke the API - * see the credentials section: <a href="https://cloud.scaleway.com/#/credentials">https://cloud.scaleway.com/#/credentials</a> * for access tokens/*from w w w . j av a 2s .c om*/ * @param region the scaleway datacentre region that this API points to (see * {@link Region} for all of the regions available at the moment) */ public ScalewayApiClient(String accessToken, Region region) { this.accessToken = accessToken; this.region = region; this.computeUrl = String.format(Constants.COMPUTE_URL, region); HttpClientBuilder httpBuilder = HttpClients.custom(); httpBuilder.setUserAgent(Constants.USER_AGENT); this.httpclient = httpBuilder.build(); Version currentJacksonVersion = new ObjectMapper().version(); Version earliestSupportedVersion = new Version(2, 8, 7, null, currentJacksonVersion.getGroupId(), currentJacksonVersion.getArtifactId()); int versionCompared = currentJacksonVersion.compareTo(earliestSupportedVersion); if (versionCompared < 0) { LOGGER.error("Jackson version: {}, version compared: {}", currentJacksonVersion.toString(), versionCompared); throw new RuntimeException( "Sorry, scaleway-api-client requires Jackson version 2.8.7 due to bugs in earlier versions."); } }
From source file:com.google.api.server.spi.response.ServletResponseResultWriter.java
private static SimpleModule getWriteLongAsStringModule() { JsonSerializer<Long> longSerializer = new JsonSerializer<Long>() { @Override/*from w w w. j ava 2s. c om*/ public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.toString()); } }; SimpleModule writeLongAsStringModule = new SimpleModule("writeLongAsStringModule", new Version(1, 0, 0, null, null, null)); writeLongAsStringModule.addSerializer(Long.TYPE, longSerializer); // long (primitive) writeLongAsStringModule.addSerializer(Long.class, longSerializer); // Long (class) return writeLongAsStringModule; }
From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.two.MappingTest2.java
@Test public void testPolymorphicJacksonSerializationAndDeserialization() { ObjectMapper mapper = new ObjectMapper(); SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null)) .addDeserializer(QueryFilter.class, new QueryFilterDeserializer()) .addSerializer(LogicalFilter2.class, new LogicalFilter2Serializer()); mapper.registerModule(testModule);//w w w . ja v a2s.c o m // Verifying that we can pass in a custom Mapper and create a new JsonUtil JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper); String testFixture = "/jsonUtils/testdomain/two/queryFilter-realAndLogical2.json"; // TEST JsonUtil and our deserialization logic QueryFilter queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter>() { }); // Make sure the hydrated QFilter looks right AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter2); AssertJUnit.assertEquals(QueryParam.AND, queryFilter.getQueryParam()); AssertJUnit.assertTrue(queryFilter.isLogical()); AssertJUnit.assertEquals(3, queryFilter.getFilters().size()); AssertJUnit.assertNotNull(queryFilter.getFilters().get(QueryParam.OR)); // Make sure one of the top level RealFilters looks right QueryFilter productIdFilter = queryFilter.getFilters().get(QueryParam.PRODUCTID); AssertJUnit.assertTrue(productIdFilter.isReal()); AssertJUnit.assertEquals(QueryParam.PRODUCTID, productIdFilter.getQueryParam()); AssertJUnit.assertEquals("Acme-1234", productIdFilter.getValue()); // Make sure the nested OR looks right QueryFilter orFilter = queryFilter.getFilters().get(QueryParam.OR); AssertJUnit.assertTrue(orFilter.isLogical()); AssertJUnit.assertEquals(QueryParam.OR, orFilter.getQueryParam()); AssertJUnit.assertEquals(2, orFilter.getFilters().size()); // Make sure nested AND looks right QueryFilter nestedAndFilter = orFilter.getFilters().get(QueryParam.AND); AssertJUnit.assertTrue(nestedAndFilter.isLogical()); AssertJUnit.assertEquals(QueryParam.AND, nestedAndFilter.getQueryParam()); AssertJUnit.assertEquals(2, nestedAndFilter.getFilters().size()); // SERIALIZE TO STRING to test serialization logic String unitTestString = jsonUtil.toJsonString(queryFilter); // LOAD and Diffy the plain vanilla JSON versions of the documents Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString); Map<String, Object> expected = JsonUtils.classpathToMap(testFixture); // Diffy the vanilla versions Diffy.Result result = diffy.diff(expected, actual); if (!result.isEmpty()) { AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected) + "\n actual: " + JsonUtils.toJsonString(result.actual)); } }
From source file:org.springframework.hateoas.hal.Jackson2HalModule.java
public Jackson2HalModule() { super("c4-ext-json-hal-module", new Version(1, 0, 0, null, "org.springframework.hateoas", "spring-hateoas")); setMixInAnnotation(Link.class, LinkMixin.class); setMixInAnnotation(ResourceSupport.class, ResourceSupportMixin.class); setMixInAnnotation(Resources.class, ResourcesMixin.class); }
From source file:com.proofpoint.json.ObjectMapperProvider.java
@Override public ObjectMapper get() { ObjectMapper objectMapper = new ObjectMapper(); // ignore unknown fields (for backwards compatibility) objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // use ISO dates objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // skip fields that are null instead of writing an explicit json null value objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // disable auto detection of json properties... all properties must be explicit objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS); objectMapper.disable(MapperFeature.AUTO_DETECT_FIELDS); objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS); objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS); objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS); objectMapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS); objectMapper.disable(MapperFeature.INFER_PROPERTY_MUTATORS); objectMapper.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS); if (jsonSerializers != null || jsonDeserializers != null || keySerializers != null || keyDeserializers != null) { SimpleModule module = new SimpleModule(getClass().getName(), new Version(1, 0, 0, null, null, null)); if (jsonSerializers != null) { for (Entry<Class<?>, JsonSerializer<?>> entry : jsonSerializers.entrySet()) { addSerializer(module, entry.getKey(), entry.getValue()); }/*from w ww. java 2 s . c o m*/ } if (jsonDeserializers != null) { for (Entry<Class<?>, JsonDeserializer<?>> entry : jsonDeserializers.entrySet()) { addDeserializer(module, entry.getKey(), entry.getValue()); } } if (keySerializers != null) { for (Entry<Class<?>, JsonSerializer<?>> entry : keySerializers.entrySet()) { addKeySerializer(module, entry.getKey(), entry.getValue()); } } if (keyDeserializers != null) { for (Entry<Class<?>, KeyDeserializer> entry : keyDeserializers.entrySet()) { module.addKeyDeserializer(entry.getKey(), entry.getValue()); } } modules.add(module); } for (Module module : modules) { objectMapper.registerModule(module); } return objectMapper; }
From source file:org.elasticsoftware.elasticactors.base.serialization.ObjectMapperBuilder.java
public final ObjectMapper build() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // scan everything for META-INF/elasticactors.properties Set<String> basePackages = new HashSet<>(); try {//from ww w . j a v a2s .c om Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(RESOURCE_NAME); while (resources.hasMoreElements()) { URL url = resources.nextElement(); Properties props = new Properties(); props.load(url.openStream()); basePackages.add(props.getProperty("basePackage")); } } catch (IOException e) { logger.warn(String.format("Failed to load elasticactors.properties"), e); } // add other base packages if (this.basePackages != null && !"".equals(this.basePackages)) { String[] otherPackages = this.basePackages.split(","); basePackages.addAll(Arrays.asList(otherPackages)); } ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); for (String basePackage : basePackages) { configurationBuilder.addUrls(ClasspathHelper.forPackage(basePackage)); } Reflections reflections = new Reflections(configurationBuilder); registerSubtypes(reflections, objectMapper); SimpleModule jacksonModule = new SimpleModule("org.elasticsoftware.elasticactors", new Version(1, 0, 0, null, null, null)); registerCustomSerializers(reflections, jacksonModule); registerCustomDeserializers(reflections, jacksonModule); objectMapper.registerModule(jacksonModule); if (useAfterBurner) { // register the afterburner module to increase performance // afterburner does NOT work correctly with jackson version lower than 2.4.5! (if @JsonSerialize annotation is used) AfterburnerModule afterburnerModule = new AfterburnerModule(); //afterburnerModule.setUseValueClassLoader(false); //afterburnerModule.setUseOptimizedBeanDeserializer(false); objectMapper.registerModule(afterburnerModule); } return objectMapper; }
From source file:com.google.api.server.spi.response.ServletResponseResultWriter.java
private static SimpleModule getWriteDateAndTimeAsStringModule() { JsonSerializer<DateAndTime> dateAndTimeSerializer = new JsonSerializer<DateAndTime>() { @Override// w w w . j a va 2 s .c o m public void serialize(DateAndTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.toRfc3339String()); } }; SimpleModule writeDateAsStringModule = new SimpleModule("writeDateAsStringModule", new Version(1, 0, 0, null, null, null)); writeDateAsStringModule.addSerializer(DateAndTime.class, dateAndTimeSerializer); return writeDateAsStringModule; }
From source file:org.kiji.rest.KijiRESTService.java
/** * Registers custom serializers with the Jackson ObjectMapper via DropWizard's * ObjectMapperFactory. This is used by both the service initialization and the test * setup method to ensure consistency between test and production. * * @param mapperFactory is the ObjectMapperFactory. *//*from w w w . j a va 2 s .c om*/ public static void registerSerializers(ObjectMapperFactory mapperFactory) { // TODO: Add a module to convert btw Avro's specific types and JSON. The default // mapping seems to throw an exception. SimpleModule module = new SimpleModule("KijiRestModule", new Version(1, 0, 0, null, "org.kiji.rest", "serializers")); module.addSerializer(new AvroToJsonStringSerializer()); module.addSerializer(new Utf8ToJsonSerializer()); module.addSerializer(new TableLayoutToJsonSerializer()); module.addSerializer(new SchemaOptionToJson()); module.addDeserializer(SchemaOption.class, new JsonToSchemaOption()); module.addSerializer(new KijiRestEntityIdToJson()); module.addDeserializer(KijiRestEntityId.class, new JsonToKijiRestEntityId()); mapperFactory.registerModule(module); }
From source file:org.brutusin.json.impl.JacksonCodec.java
@Override public void registerStringFormat(Class clazz, String format) { this.schemaFactory.registerStringFormat(clazz, format); SimpleModule testModule = new SimpleModule("json-provider-module:" + format, new Version(1, 0, 0, null, "org.brutusin", "json-provider:" + format)); testModule.addSerializer(new StdSerializer(clazz) { @Override// w ww .ja va 2 s. c o m public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeString(value.toString()); } }); mapper.registerModule(testModule); }