From 8c38e6ca614b22c39ef2fca549d9eb2265304baa Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 6 Jan 2021 09:18:05 +0100 Subject: Better serializer for CIDRs too. --- out/phase-1.yaml | 5 +---- out/vpn0.yaml | 5 +---- src/main/java/io/trygvis/rules/network/Ipv4Cidr.java | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/out/phase-1.yaml b/out/phase-1.yaml index 8c34f1a..b582087 100644 --- a/out/phase-1.yaml +++ b/out/phase-1.yaml @@ -321,10 +321,7 @@ data: --- type: "io.trygvis.rules.network.Ipv4Cidr" data: - network: -1062729216 - netmask: -8 - size: 8 - bits: 29 + value: "192.168.10.0/29" --- type: "io.trygvis.rules.terraform.ScalewayMachine" data: diff --git a/out/vpn0.yaml b/out/vpn0.yaml index 1c88e80..3fc6836 100644 --- a/out/vpn0.yaml +++ b/out/vpn0.yaml @@ -145,7 +145,4 @@ data: --- type: "io.trygvis.rules.network.Ipv4Cidr" data: - network: -1062729216 - netmask: -8 - size: 8 - bits: 29 + value: "192.168.10.0/29" diff --git a/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java b/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java index 34b3a0b..6e1dde9 100644 --- a/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java +++ b/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java @@ -1,10 +1,17 @@ package io.trygvis.rules.network; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; +@JsonSerialize(using = Ipv4Cidr.Serializer.class) public class Ipv4Cidr { public final int network; public final int netmask; @@ -37,4 +44,13 @@ public class Ipv4Cidr { return addresses; } + + public static class Serializer extends JsonSerializer { + @Override + public void serialize(Ipv4Cidr value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeStartObject(); + gen.writeObjectField("value", value.toString()); + gen.writeEndObject(); + } + } } -- cgit v1.2.3