package de.fesere.hypermedia.cj.serialization;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import de.fesere.hypermedia.cj.model.data.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class DataEntrySerializer extends StdSerializer<DataEntry> {
    protected DataEntrySerializer() {
        super(DataEntry.class);
    }

    @Override
    public void serialize(DataEntry value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
        if (value instanceof EmptyDataEntry) {
            return;
        }
        jgen.writeStartObject();

        writeName(value, jgen);
        writeValue(value, jgen);
        writePrompt(value, jgen);

        jgen.writeEndObject();

    }

    private void writePrompt(DataEntry value, JsonGenerator jgen) throws IOException {
        String prompt = value.getPrompt();
        if (StringUtils.isNotBlank(prompt)) {
            jgen.writeStringField("prompt", prompt);
        }
    }

    private void writeName(DataEntry value, JsonGenerator jgen) throws IOException {
        jgen.writeStringField("name", value.getName());
    }

    private void writeValue(DataEntry value, JsonGenerator jgen) throws IOException {
        jgen.writeFieldName("value");
        writeContent(value, jgen);
    }


    private void writeContent(DataEntry value, JsonGenerator jgen) throws IOException {
        if (value instanceof StringDataEntry) {
            StringDataEntry stringEntry = (StringDataEntry) value;
            jgen.writeString(stringEntry.getValue());
        } else if (value instanceof NumberDataEntry) {
            writeNumber((NumberDataEntry) value, jgen);
        } else if (value instanceof BooleanDataEntry) {
            jgen.writeBoolean((boolean) value.getValue());
        } else {
            jgen.writeString((String) value.getValue());
        }
    }

    private void writeNumber(NumberDataEntry value, JsonGenerator jgen) throws IOException {
        Number number = value.getValue();
        if (number instanceof Double) {
            jgen.writeNumber(number.doubleValue());
        } else {
            jgen.writeNumber(number.intValue());
        }
    }
}
@@ -264,24 +264,90 @@
 Serializer;%0A
+import de.fesere.hypermedia.cj.exceptions.SerializationException;%0A
 import de.fe
@@ -522,16 +522,50 @@
 ntry%3E %7B%0A
+%0A    private JsonGenerator jgen;%0A%0A
     prot
@@ -675,37 +675,37 @@
 alize(DataEntry 
-value
+entry
 , JsonGenerator 
@@ -789,37 +789,37 @@
 n %7B%0A        if (
-value
+entry
  instanceof Empt
@@ -858,24 +858,51 @@
 ;%0A        %7D%0A
+        this.jgen = jgen;%0A%0A
         jgen
@@ -941,27 +941,31 @@
 iteName(
-value, jgen
+entry.getName()
 );%0A     
@@ -982,212 +982,52 @@
 lue(
-value, jgen);%0A        writePrompt(value, jgen);%0A%0A        jgen.writeEndObject(
+entry.getValue()
 );%0A
-%0A
     
-%7D%0A%0A
     
-private void writePrompt(DataEntry value, JsonGenerator jgen) throws IOException %7B%0A        String prompt = value
+writePrompt(entry
 .get
@@ -1038,188 +1038,89 @@
 pt()
+)
 ;%0A
-        if (StringUtils.isNotBlank(prompt)) %7B%0A            jgen.writeStringField(%22prompt%22, prompt);%0A        %7D%0A    %7D%0A%0A    private void writeName(DataEntry value, JsonGenerator jgen
+%0A        jgen.writeEndObject();%0A%0A    %7D%0A%0A    private void writeName(String name
 ) th
@@ -1180,23 +1180,12 @@
 e%22, 
-value.getN
+n
 ame
-()
 );%0A 
@@ -1222,43 +1222,20 @@
 lue(
-DataEntry value, JsonGenerator jgen
+Object value
 ) th
@@ -1313,30 +1313,24 @@
 ontent(value
-, jgen
 );%0A    %7D%0A%0A%0A 
@@ -1362,43 +1362,20 @@
 ent(
-DataEntry value, JsonGenerator jgen
+Object value
 ) th
@@ -1432,88 +1432,12 @@
 ring
-DataEntry
 ) %7B%0A
-            StringDataEntry stringEntry = (StringDataEntry) value;%0A
     
@@ -1465,30 +1465,22 @@
 ing(
-s
+(S
 tring
-Entry.getV
+) v
 alue
-()
 );%0A 
@@ -1520,25 +1520,16 @@
 f Number
-DataEntry
 ) %7B%0A    
@@ -1540,49 +1540,70 @@
     
-writeN
+Number n
 umber
-(
+ = 
 (Number
-DataEntry) value, jgen
+) value;%0A            writeNumber(number
 );%0A 
@@ -1644,25 +1644,16 @@
  Boolean
-DataEntry
 ) %7B%0A    
@@ -1689,35 +1689,24 @@
 olean) value
-.getValue()
 );%0A        %7D
@@ -1702,32 +1702,51 @@
 ;%0A        %7D else
+ if (value == null)
  %7B%0A            j
@@ -1758,40 +1758,152 @@
 rite
-String((String) value.getValu
+Null();%0A        %7D%0A        else %7B%0A            throw new SerializationException(%22Unspoorted value of type %22 + value.getClass().getCanonicalNam
 e()
+ 
 );%0A 
@@ -1957,107 +1957,37 @@
 mber
-DataEntry value, JsonGenerator jgen) throws IOException %7B%0A        Number number = value.getValue();
+ number) throws IOException %7B
 %0A   
@@ -2156,10 +2156,191 @@
  %7D%0A    %7D
+%0A%0A    private void writePrompt(String prompt) throws IOException %7B%0A        if (StringUtils.isNotBlank(prompt)) %7B%0A            jgen.writeStringField(%22prompt%22, prompt);%0A        %7D%0A    %7D
 %0A%7D

Clean up the parameters and method names