diff --git a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java new file mode 100644 index 000000000..21b6d8f42 --- /dev/null +++ b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/management/PatternTypeAdapter.java @@ -0,0 +1,26 @@ +package com.telefonica.iot.cygnus.management; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.util.regex.Pattern; + +public class PatternTypeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, Pattern pattern) throws IOException { + if (pattern == null) { + out.nullValue(); + return; + } + out.value(pattern.pattern()); + } + + @Override + public Pattern read(JsonReader in) throws IOException { + String patternString = in.nextString(); + return Pattern.compile(patternString); + } +}