0

I am using the following code for serialization:

public class JsonTransformer implements ResponseTransformer {

    private Gson gson = new Gson();

    @Override
    public String render(Object model) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gson.toJson(model);
    }
}

I get error:

java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: com.soul.seeker.models.Post. Forgot to register a type adapter?

How do I serialize a list a return it in the below code?

get("/data_on_page_load", "application/json", (Request request, Response response) -> {
    List<Post> list = Post.findAll();
    return list;
}, new JsonTransformer());

pojo class:

public class Post extends Model{

   private String title;
   private String details;
   private String username;
   private String userImage;
   private String url;
   private List categories;

   //Getters and Setters removed for brevity
}

update:

When I tried to use it the below way:

public class JsonTransformer implements ResponseTransformer {

    private Gson gson = new Gson();

    @Override
    public String render(Object model) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory()).create();
        return gson.toJson(model);
    }
}

public class ClassTypeAdapterFactory implements TypeAdapterFactory {
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
        if(!Class.class.isAssignableFrom(typeToken.getRawType())) {
            return null;
        }
        return (TypeAdapter<T>) new ClassTypeAdapter();
    }
}

it returns unnecessary information in json object when I pass it back to client side:

Array[2]
0:Object
attributes:Object
cachedChildren:Object
cachedParents:Object
compositeKeyPersisted:false
dirtyAttributeNames:Array[0]
errors:Object
frozen:false
manageTime:true
metaModelLocal:Object
modelRegistryLocal:Object
__proto__:
Object1:Object
length:2
__proto__:Array[0]

It makes sense that ClassTypeAdapter is where I have modify the code to return proper list object as json and exclude irrelevant information, but I am still new to serialization.

kittu
  • 6,662
  • 21
  • 91
  • 185
  • Why do you need to serialize `Class`? It's necessary for very rare specialized cases only that I don't believe is relevant here. Does it come from the inherited `Model` class? – Lyubomyr Shaydariv Mar 26 '17 at 08:12
  • @LyubomyrShaydariv I want to return only list object as json. I copied the code of classtypeAdapter from an example. Model is not irrelevant here. Model is from activeJdbc lib – kittu Mar 26 '17 at 08:14
  • You have posted unrelated JavaScript object in-debug-console dump and POJO. Anyway, where does the `Class` come from and why are you going to (de)serialize it? See more for trivial lists serialization: http://stackoverflow.com/questions/5813434/trouble-with-gson-serializing-an-arraylist-of-pojos – Lyubomyr Shaydariv Mar 26 '17 at 08:36
  • @LyubomyrShaydariv posted a new question with more clarity: http://stackoverflow.com/questions/43032414/how-to-serialize-list-object-in-gson I will delete this question. – kittu Mar 26 '17 at 17:52
  • 2
    Mate, the new question is just a rewording of this one. The issue I do believe you're facing with is that you're deserializing ActiveJDBC `Model`, and it does **not** seem to be suitable for serialization: http://grepcode.com/file/repo1.maven.org/maven2/org.javalite/activejdbc/1.4/org/javalite/activejdbc/Model.java - that is what I was asking about: where does the `Class` come from. This is where it's from. You should not serialize it. – Lyubomyr Shaydariv Mar 26 '17 at 21:49
  • 1
    What you need is just separate the active records and data transfer objects (see Wikipedia): just create a `Person` DTO, and then just map the active record objects to data transfer objects. That's all. The golden rule is: don't mix concepts that may be in a very conflict state due their technical details. Of course, if your person DTO is not inherited from Model (and it must never be like that), then you never face with serializing a whatever library components internals. You just have not to mix the concepts and keep them apart. – Lyubomyr Shaydariv Mar 26 '17 at 21:54

0 Answers0