-
Notifications
You must be signed in to change notification settings - Fork 206
Support for Jackson 3 #685
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
base: main
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,10 @@ | ||||||||||||||||||
| package com.maxmind.geoip2; | ||||||||||||||||||
|
|
||||||||||||||||||
| import com.fasterxml.jackson.core.JsonGenerator; | ||||||||||||||||||
| import com.fasterxml.jackson.databind.SerializerProvider; | ||||||||||||||||||
| import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||
| import java.net.InetAddress; | ||||||||||||||||||
| import tools.jackson.core.JacksonException; | ||||||||||||||||||
| import tools.jackson.core.JsonGenerator; | ||||||||||||||||||
| import tools.jackson.databind.SerializationContext; | ||||||||||||||||||
| import tools.jackson.databind.ser.std.StdSerializer; | ||||||||||||||||||
|
Comment on lines
+4
to
+7
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. The
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Serializes InetAddress to its host address string representation. | ||||||||||||||||||
|
|
@@ -18,8 +18,8 @@ public InetAddressSerializer() { | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| public void serialize(InetAddress value, JsonGenerator gen, SerializerProvider provider) | ||||||||||||||||||
| throws IOException { | ||||||||||||||||||
| public void serialize(InetAddress value, JsonGenerator gen, SerializationContext provider) | ||||||||||||||||||
| throws JacksonException { | ||||||||||||||||||
| if (value == null) { | ||||||||||||||||||
| gen.writeNull(); | ||||||||||||||||||
| } else { | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,8 @@ | ||||||||||
| package com.maxmind.geoip2; | ||||||||||
|
|
||||||||||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||||||||||
| import com.fasterxml.jackson.databind.MapperFeature; | ||||||||||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||||||||||
| import com.fasterxml.jackson.databind.json.JsonMapper; | ||||||||||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||||||||||
| import java.io.IOException; | ||||||||||
| import tools.jackson.databind.DeserializationFeature; | ||||||||||
| import tools.jackson.databind.json.JsonMapper; | ||||||||||
|
Comment on lines
+4
to
+5
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. The
Suggested change
|
||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Interface for classes that can be serialized to JSON. | ||||||||||
|
|
@@ -16,16 +13,14 @@ public interface JsonSerializable { | |||||||||
| /** | ||||||||||
| * @return JSON representation of this object. The structure is the same as | ||||||||||
| * the JSON provided by the GeoIP2 web service. | ||||||||||
| * @throws IOException if there is an error serializing the object to JSON. | ||||||||||
| */ | ||||||||||
| default String toJson() throws IOException { | ||||||||||
| default String toJson() { | ||||||||||
| JsonMapper mapper = JsonMapper.builder() | ||||||||||
| .disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS) | ||||||||||
| .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||||||||||
| .addModule(new JavaTimeModule()) | ||||||||||
| .disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) | ||||||||||
| .addModule(new InetAddressModule()) | ||||||||||
| .serializationInclusion(JsonInclude.Include.NON_NULL) | ||||||||||
| .serializationInclusion(JsonInclude.Include.NON_EMPTY) | ||||||||||
| .changeDefaultPropertyInclusion( | ||||||||||
| value -> value.withValueInclusion(JsonInclude.Include.NON_NULL) | ||||||||||
| .withValueInclusion(JsonInclude.Include.NON_EMPTY)) | ||||||||||
| .build(); | ||||||||||
|
|
||||||||||
| return mapper.writeValueAsString(this); | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,12 +1,13 @@ | ||||||||||||||||||||||
| package com.maxmind.geoip2; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import com.fasterxml.jackson.core.JsonParser; | ||||||||||||||||||||||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||||||||||||||||||||||
| import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||||||||||||||||||||||
| import com.maxmind.db.Network; | ||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||
| import java.net.InetAddress; | ||||||||||||||||||||||
| import java.net.UnknownHostException; | ||||||||||||||||||||||
| import tools.jackson.core.JacksonException; | ||||||||||||||||||||||
| import tools.jackson.core.JsonParser; | ||||||||||||||||||||||
| import tools.jackson.databind.DatabindException; | ||||||||||||||||||||||
| import tools.jackson.databind.DeserializationContext; | ||||||||||||||||||||||
| import tools.jackson.databind.deser.std.StdDeserializer; | ||||||||||||||||||||||
|
Comment on lines
+6
to
+10
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. The
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * This class provides a deserializer for the Network class. | ||||||||||||||||||||||
|
|
@@ -17,7 +18,7 @@ public final class NetworkDeserializer extends StdDeserializer<Network> { | |||||||||||||||||||||
| * Constructs a {@code NetworkDeserializer} with no type specified. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| public NetworkDeserializer() { | ||||||||||||||||||||||
| this(null); | ||||||||||||||||||||||
| this(Network.class); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
|
|
@@ -31,16 +32,20 @@ public NetworkDeserializer(Class<?> vc) { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Override | ||||||||||||||||||||||
| public Network deserialize(JsonParser jsonparser, DeserializationContext context) | ||||||||||||||||||||||
| throws IOException { | ||||||||||||||||||||||
| throws JacksonException { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| final var cidr = jsonparser.getValueAsString(); | ||||||||||||||||||||||
| if (cidr == null || cidr.isBlank()) { | ||||||||||||||||||||||
| return null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return parseCidr(cidr); | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| return parseCidr(cidr); | ||||||||||||||||||||||
| } catch (UnknownHostException e) { | ||||||||||||||||||||||
| throw DatabindException.from(jsonparser, "Unknown host in CIDR: " + cidr, e); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private static Network parseCidr(String cidr) throws IOException { | ||||||||||||||||||||||
| private static Network parseCidr(String cidr) throws UnknownHostException { | ||||||||||||||||||||||
| final var parts = cidr.split("/", 2); | ||||||||||||||||||||||
| if (parts.length != 2) { | ||||||||||||||||||||||
| throw new IllegalArgumentException("Invalid CIDR format: " + cidr); | ||||||||||||||||||||||
|
|
@@ -49,12 +54,7 @@ private static Network parseCidr(String cidr) throws IOException { | |||||||||||||||||||||
| final var addrPart = parts[0]; | ||||||||||||||||||||||
| final var prefixPart = parts[1]; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| final InetAddress address; | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| address = InetAddress.getByName(addrPart); | ||||||||||||||||||||||
| } catch (UnknownHostException e) { | ||||||||||||||||||||||
| throw new IOException("Unknown host in CIDR: " + cidr, e); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| final InetAddress address = InetAddress.getByName(addrPart); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| final var prefixLength = parsePrefixLength(prefixPart, cidr); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.