Skip to content

Instantly share code, notes, and snippets.

@ptirador
Last active April 5, 2018 18:29
Jackson serialization test
package com.example.jackson;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class AccessModel
implements Serializable
{
private Map<String, Collection<String>> repositoryPrivileges;
public AccessModel()
{
repositoryPrivileges = new HashMap<>();
}
public Map<String, Collection<String>> getRepositoryPrivileges()
{
return repositoryPrivileges;
}
public void setRepositoryPrivileges(Map<String, Collection<String>> repositoryPrivileges)
{
this.repositoryPrivileges = repositoryPrivileges;
}
}
package com.example.jackson;
import java.util.LinkedHashMap;
public class CustomMap<T> extends LinkedHashMap<Object, T>
{
}
package com.example.jackson;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.logging.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
public class JacksonTest
{
private static final Logger LOGGER = Logger.getLogger(JacksonTest.class.getName());
@Test
public void testSerialization()
throws Exception
{
Map<String, Collection<String>> repoPrivilegesMap = new CustomMap();
String key = "/storages/storage0/releases";
Collection<String> values = new HashSet<>();
values.add("ARTIFACTS_RESOLVE");
repoPrivilegesMap.put(key, values);
AccessModel accessModel = new AccessModel();
accessModel.setRepositoryPrivileges(repoPrivilegesMap);
ObjectMapper mapper = new ObjectMapper();
LOGGER.info("Jackson version: " + mapper.version());
String jsonStr = mapper.writeValueAsString(accessModel);
LOGGER.info("JSON: " + jsonStr);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jackson</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<version.jackson>2.9.5</version.jackson>
<version.junit>4.12</version.junit>
</properties>
<dependencies>
<!-- Jackson family -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment