-
Notifications
You must be signed in to change notification settings - Fork 177
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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()); | ||
|
||
String actualSerialization = objectMapper.writeValueAsString(testClient); | ||
JSONAssert.assertEquals(expectedSerialization, actualSerialization, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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" | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs a newline. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. file name should reflect the unit test it’s associated with: |
There was a problem hiding this comment.
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