package com.getbase.serializer;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.getbase.exceptions.SerializationException;

public abstract class JsonSerializer {
    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();

        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        mapper.registerModule(new JodaModule());
    }

    private JsonSerializer(){
    }

    public static String serialize(Object entity, Class<?> viewClass) {
        String data = null;
        try {
            data = mapper.writerWithView(viewClass).writeValueAsString(entity);
        } catch (JsonProcessingException e) {
            throw new SerializationException(e);
        }
        return String.format("{\"data\":%s}", data);
    }

    public static String serialize(Object entity) {
        String data = null;
        try {
            data = mapper.writeValueAsString(entity);
        } catch (JsonProcessingException e) {
            throw new SerializationException(e);
        }
        return String.format("{\"data\":%s}", data);
    }
}

@@ -79,16 +79,66 @@
 nclude;%0A
+import com.fasterxml.jackson.annotation.JsonView;%0A
 import c
@@ -991,36 +991,8 @@
 ) %7B%0A
-        String data = null;%0A
     
@@ -1005,38 +1005,38 @@
 y %7B%0A            
-data =
+return
  mapper.writerWi
@@ -1064,39 +1064,53 @@
 teValueAsString(
+new Envelope(
 entity)
+)
 ;%0A        %7D catc
@@ -1204,61 +1204,8 @@
   %7D%0A
-        return String.format(%22%7B%5C%22data%5C%22:%25s%7D%22, data);%0A
     
@@ -1263,36 +1263,8 @@
 ) %7B%0A
-        String data = null;%0A
     
@@ -1289,14 +1289,14 @@
     
-data =
+return
  map
@@ -1318,23 +1318,37 @@
 sString(
+new Envelope(
 entity)
+)
 ;%0A      
@@ -1454,56 +1454,177 @@
     
+%7D%0A%0A
     
-return String.format(%22%7B%5C%22data%5C%22:%25s%7D%22, data);
+static class Envelope %7B%0A        @JsonView(Views.ReadWrite.class)%0A        Object data;%0A%0A        Envelope(Object entity) %7B%0A            this.data = entity;%0A        %7D
 %0A   

use private static envelope object instead of ugly String.format method