Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making VRaptorGsonBuilder a component #561

Merged
merged 8 commits into from
Aug 14, 2013

Conversation

nykolaslima
Copy link
Contributor

We can use VRaptorGsonBuilder to make serialization in "VRaptor way".

e.g.:
vRaptorGsonBuilder.create().toJson(representation);

In order to use VRaptorGsonBuilder in other projects, we need to make it a component to be injected through DI container.

With this changes we are also improving code quality, by receiving VRaptorGsonBuilder as a dependency, removing the responsability of GsonJSONSerialization of build it.

public VraptorGsonBuilder getInstance() {
ExclusionStrategy exclusion = new Exclusions(serializee);

return new VraptorGsonBuilder(serializers.getSerializers(), Arrays.asList(exclusion));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a component factory? Why not make VRaptorGsonBuilder a @PrototypeScoped @Component?

Also, please rename VraptorGsonBuilder => VRaptorGsonBuilder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we inject a List directly in VRaptorGsonBuilder? How can we create a ExclusionStrategy using the serializee to use in VRaptorGsonBuilder without a component factory?

We can make it @PrototypeScopedlike you said.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can put this creation logic inside VRaptorGsonBuilder

@nykolaslima
Copy link
Contributor Author

@lucascs take a look now =)

If you merge it, can you generate a SNAPSHOT for me?

new DefaultJsonSerializers(Collections.<JsonSerializer> emptyList()));
serializee = new Serializee();

VRaptorGsonBuilder builder = new VRaptorGsonBuilder(new DefaultJsonSerializers(Collections.<JsonSerializer> emptyList()), serializee);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use createBuilder here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still can use createBuilder() ;)

@dipold
Copy link
Member

dipold commented Aug 13, 2013

This is cool! Currently I have make a workaround 😞 like this to get an instance of Gson:
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Calendar.class, this.calendarISO8601Deserializer).create();

@nykolaslima
Copy link
Contributor Author

Hey @lucascs, take a look now

@@ -17,7 +20,8 @@
*/

@SuppressWarnings("rawtypes")
public class VraptorGsonBuilder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's missing an @Component here.

@nykolaslima
Copy link
Contributor Author

Sorry, I missed @Component. Now I believe its ok.


GsonJSONSerialization serialization = new GsonJSONSerialization(response, extractor, initializer, new DefaultJsonSerializers(adapters));

GsonJSONSerialization serialization = new GsonJSONSerialization(response, extractor, initializer, createBuilder(new CollectionSerializer()), serializee);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd extract another method here:

JSONSerialization serialization = serializationWithAdapter(new CollectionSerializer());
//...
public JSONSerialization serializationWithAdapter(JsonSerializer adapter) {
    return new GsonJSONSerialization(response, extractor, initializer, createBuilder(adapter), serializee);
}

So we can better express the test intent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes Lucas, I agree with you. I changed it and tests look better. Can you take a look now?

lucascs added a commit that referenced this pull request Aug 14, 2013
…onent

making VRaptorGsonBuilder a component
@lucascs lucascs merged commit 378fe5d into caelum:master Aug 14, 2013
@lucascs
Copy link
Member

lucascs commented Aug 14, 2013

Thanks!

@nykolaslima nykolaslima deleted the VRaptorGsonBuilder_as_a_component branch August 14, 2013 14:35
@nykolaslima
Copy link
Contributor Author

Can you generate a SNAPSHOT with this, please?

@nykolaslima
Copy link
Contributor Author

Thank you!

@dipold
Copy link
Member

dipold commented Aug 16, 2013

@nykolaslima something went wrong here. If I do this:

result.use(json()).from(cliente).include("endereco").serialize();

The class Endereco not longer being serialized =/

@nykolaslima
Copy link
Contributor Author

Any exception or something?

@dipold
Copy link
Member

dipold commented Aug 16, 2013

Nothing..

@nykolaslima
Copy link
Contributor Author

@dipold I have made a test with this situation that you exposes but here the serialization works fine.

Are you using Gson serialization?

@lucascs
Copy link
Member

lucascs commented Aug 16, 2013

We have tests for this... maybe the problem is that Serializee is a component now =(

@dipold
Copy link
Member

dipold commented Aug 16, 2013

I created a new project to be sure about the problem. And the problem persists.

What I found in debug mode, at the moment, is that VRaptorGsonBuilder and GsonJSONSerialization receive different instances of Serializee.

And that does not happen in units tests. I tried to create a unit test without success.

My project configuration:
web.xml

<context-param>
    <param-name>br.com.caelum.vraptor.packages</param-name>
        <param-value>
            br.com.caelum.vraptor.serialization.gson,
            br.com.caelum.vraptor.deserialization.gson
        </param-value>
</context-param>

controller:

@Get("/clientes")
public void get() {
    List<Cliente> clientes = new ArrayList<Cliente>();
    Endereco e1 = new Endereco(1L, "Logradouro 1");
    Cliente c1 = new Cliente(1L, "Nome 1", e1);
    clientes.add(c1);
    result.use(Results.json()).withoutRoot().from(clientes).include("endereco").serialize();
}

@lucascs
Copy link
Member

lucascs commented Aug 16, 2013

@dipold
Copy link
Member

dipold commented Aug 16, 2013

Thanks @lucascs
Works like a charm

@dipold
Copy link
Member

dipold commented Aug 27, 2013

I think there's one more thing to fix.
The class HibernateProxySerializer should not be adapted to use the new VRaptorGsonBuilder?
Because the class HibernateProxySerializer is invoked but is not serializing to the correct instance of Gson, see:
return new Gson().toJsonTree(deProxied);

@lucascs
Copy link
Member

lucascs commented Aug 27, 2013

What is the result of this?

@dipold
Copy link
Member

dipold commented Aug 27, 2013

The final operation result an exception:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class:
org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

But the HibernateProxySerializer run ok:
image

@lucascs
Copy link
Member

lucascs commented Aug 27, 2013

But estado and microRegiao are proxies...

I think that is why it is throwing the exception...

can't you delegate the serialization instead of calling new Gson().toJsonTree?
It is not using all registered adapters.

@dipold
Copy link
Member

dipold commented Aug 27, 2013

You are right!

How I can delegate? I tried change the code to bellow and now works fine:

return ctx.serialize(deProxied);//new Gson().toJsonTree(deProxied);

I think we can do it, given that ctx never invoke on the src object itself since that will cause an infinite loop.

@lucascs
Copy link
Member

lucascs commented Aug 27, 2013

Change this and send a Pull request please =)

garcia-jj added a commit to caelum/vraptor4 that referenced this pull request Sep 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants