Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Jackson databind module for simplified deserialization. #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<targetPath>ua_parser</targetPath>
<directory>${basedir}/uap-core</directory>
Expand All @@ -67,6 +70,9 @@
</resources>

<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<targetPath>ua_parser</targetPath>
<directory>${basedir}/uap-core/test_resources</directory>
Expand Down Expand Up @@ -159,6 +165,12 @@
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -171,5 +183,17 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
62 changes: 62 additions & 0 deletions src/main/java/ua_parser/ClientModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ua_parser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
* Class that registers capability of serializing objects with Jackson core.
*
* This module can be manually registered with Jackson:
* <pre>
* ObjectMapper mapper = new ObjectMapper();
* mapper.registerModule(new ClientModule());
* </pre>
*
* This module also supports auto configuration as an SPI via the Java ServiceLoader api:
* <pre>
* ObjectMapper mapper = new ObjectMapper();
* mapper.findAndRegisterModules();
*
* </pre>
*
* @author Adam J. Weigold
*/
public class ClientModule extends SimpleModule {

public ClientModule() {
setMixInAnnotation(UserAgent.class, UserAgentMixin.class);
setMixInAnnotation(OS.class, OSMixin.class);
setMixInAnnotation(Device.class, DeviceMixin.class);
setMixInAnnotation(Client.class, ClientMixin.class);
}

abstract static class UserAgentMixin {
@JsonCreator
public UserAgentMixin(@JsonProperty("family") String family,
@JsonProperty("major") String major,
@JsonProperty("minor") String minor,
@JsonProperty("patch") String patch) {}
}

abstract static class OSMixin {
@JsonCreator
public OSMixin(@JsonProperty("family") String family,
@JsonProperty("major") String major,
@JsonProperty("minor") String minor,
@JsonProperty("patch") String patch,
@JsonProperty("patchMinor") String patchMinor) {}
}

abstract static class DeviceMixin {
@JsonCreator
public DeviceMixin(@JsonProperty("family") String family) {}
}

abstract static class ClientMixin {
@JsonCreator
public ClientMixin(@JsonProperty("userAgent") UserAgent userAgent,
@JsonProperty("os") OS os,
@JsonProperty("device") Device device) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ua_parser.ClientModule
43 changes: 43 additions & 0 deletions src/test/java/ua_parser/JacksonModuleDatabindingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ua_parser;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.io.IOException;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class JacksonModuleDatabindingTest {

@Test
public void testJacksonModuleSpiRegistration() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
Class<?> clientMixin = objectMapper.findMixInClassFor(Client.class);
assertTrue(clientMixin.isAssignableFrom(ClientModule.ClientMixin.class));
}

@Test
public void testJacksonSerializationAndDeserialization() throws IOException, JSONException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

Client testClient = new Client(new UserAgent("Firefox", "3", "5", "5"),
new OS("Mac OS X", "10", "4", null, null),
new Device("Other"));

String expectedSerialization = IOUtils.resourceToString(
"ua_parser/testClient.json", Charset.defaultCharset(), getClass().getClassLoader());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can read the full file into a string using java nio Files. Don’t need the dependency on commons io


String actualSerialization = objectMapper.writeValueAsString(testClient);
JSONAssert.assertEquals(expectedSerialization, actualSerialization, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you be open to just using string equality for this check rather than pulling in another library just for one line of one unit test?


Client deserializedClient = objectMapper.readValue(actualSerialization, Client.class);
assertEquals(testClient, deserializedClient);
}
}
18 changes: 18 additions & 0 deletions src/test/resources/ua_parser/testClient.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"userAgent": {
"family": "Firefox",
"major": "3",
"minor": "5",
"patch": "5"
},
"os": {
"family": "Mac OS X",
"major": "10",
"minor": "4",
"patch": null,
"patchMinor": null
},
"device": {
"family": "Other"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a newline.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file name should reflect the unit test it’s associated with:
jackson-module-databinding-test.json