package de.fesere.hypermedia.cj.serialization;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.fesere.hypermedia.cj.exceptions.SerializationException;
import de.fesere.hypermedia.cj.model.Collection;
import de.fesere.hypermedia.cj.model.Template;

import java.io.IOException;

public class Serializer {

    private final ObjectMapper mapper;

    public Serializer() {
        this(new ObjectMapperConfig().getConfiguredObjectMapper());
    }

    protected Serializer(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public <T> T deserialize(String input, Class<T> responseClass) {
        if(responseClass.equals(Collection.class)){
            return (T) deserializeCollection(input);
        }

        if(responseClass.equals(Template.class)){
            return (T) deseriliazeTemplate(input);
        }

        try {
            return mapper.readValue(input, responseClass);
        } catch (IOException e) {
            throw new SerializationException("Could not deseriliaze input to " + responseClass.getName(), e);
        }
    }

    public String serialize(Object obj) {

        if(obj instanceof Collection ) {
            return serialize((Collection) obj);
        }
        if(obj instanceof Template) {
            return serialize((Template) obj);
        }

        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new SerializationException("Could not serilize " + obj.getClass().getName() + " to string", e);
        }
    }

    private Template deseriliazeTemplate(String givenJson) {
        return (Template) deserialize(givenJson, Wrapper.class).getElement();
    }

    private Collection deserializeCollection(String giveJson) {
        return (Collection) deserialize(giveJson, Wrapper.class).getElement();
    }

    private String serialize(Collection collection) {
        return serialize(new Wrapper<>(collection));
    }

    private String serialize(Template template) {
        return serialize(new Wrapper<>(template));
    }
}
@@ -906,234 +906,68 @@
     
-try %7B%0A            return mapper.readValue(input, responseClass);%0A        %7D catch (IOException e) %7B%0A            throw new S
+return defaultDes
 eriali
-z
+s
 ation
-Exception(%22Could not deseriliaze input to %22 + responseClass.getName(), e);%0A        %7D
+(input, responseClass);
 %0A    %7D%0A%0A
     
@@ -954,32 +954,33 @@
 eClass);%0A    %7D%0A%0A
+%0A
     public Strin
@@ -1192,32 +1192,135 @@
 bj);%0A        %7D%0A%0A
+        return defaultSerialization(obj);%0A    %7D%0A%0A    private String defaultSerialization(Object obj) %7B%0A
         try %7B%0A  
@@ -1561,284 +1561,547 @@
 ate 
-Template de
+String 
 seri
+a
 li
-a
 ze
-Template(String givenJson) %7B%0A        return (Template) deserialize(givenJson, Wrapper.class).getElement();%0A    %7D%0A%0A    private Collection deserializeCollection(String giveJson) %7B%0A        return (Collection) deserialize(giveJson, Wrapper.c
+(Collection collection) %7B%0A        return defaultSerialization(new Wrapper%3C%3E(collection));%0A    %7D%0A%0A    private String serialize(Template template) %7B%0A        return defaultSerialization(new Wrapper%3C%3E(template));%0A    %7D%0A%0A    private %3CT%3E T defaultDeserialisation(String input, Class%3CT%3E responseClass) %7B%0A        try %7B%0A            return mapper.readValue(input, responseClass);%0A        %7D catch (IOException e) %7B%0A            throw new SerializationException(%22Could not deseriliaze input to %22 + responseC
 lass
-)
 .get
-Element();
+Name(), e);%0A        %7D
 %0A   
@@ -2120,208 +2120,304 @@
 ate 
-String 
+Template de
 seri
-a
 li
+a
 ze
-(Collection collection) %7B%0A        return serialize(new Wrapper%3C%3E(collection));%0A    %7D%0A%0A    private String serialize(Template template) %7B%0A        return serialize(new Wrapper%3C%3E(template)
+Template(String givenJson) %7B%0A        return (Template) defaultDeserialisation(givenJson, Wrapper.class).getElement();%0A    %7D%0A%0A    private Collection deserializeCollection(String giveJson) %7B%0A        return (Collection) defaultDeserialisation(giveJson, Wrapper.class).getElement(
 );%0A 

Rename methods for clearer structure