Skip to content

Commit

Permalink
usando o TypeNameExtractor do vraptor
Browse files Browse the repository at this point in the history
  • Loading branch information
renanreismartins committed Jun 9, 2012
1 parent ca84c46 commit ec2b0ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
Expand Up @@ -4,6 +4,7 @@

import javax.servlet.http.HttpServletResponse;

import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.serialization.JSONSerialization;
import br.com.caelum.vraptor.serialization.NoRootSerialization;
Expand All @@ -20,8 +21,11 @@ public class GsonSerialization implements JSONSerialization {

private boolean indented;

public GsonSerialization(HttpServletResponse response) {
private TypeNameExtractor extractor;

public GsonSerialization(HttpServletResponse response, TypeNameExtractor extractor) {
this.response = response;
this.extractor = extractor;
this.withoutRoot = false;
}

Expand Down Expand Up @@ -55,7 +59,8 @@ public JSONSerialization indented() {

protected SerializerBuilder getSerializer() {
try {
return new GsonSerializer(response.getWriter(), indented, withoutRoot, response.getLocale());
return new GsonSerializer(response.getWriter(), indented, withoutRoot, extractor,
response.getLocale());
} catch (IOException e) {
throw new ResultException("Unable to serialize data", e);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.Map.Entry;

import net.vidageek.mirror.dsl.Mirror;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
import br.com.caelum.vraptor.serialization.Serializer;
import br.com.caelum.vraptor.serialization.SerializerBuilder;
import br.com.caelum.vraptor.view.ResultException;
Expand All @@ -41,8 +42,13 @@ public class GsonSerializer implements SerializerBuilder {

private boolean withoutRoot = false;

public GsonSerializer(Writer writer, boolean indented, boolean withoutRoot, Locale locale) {
private TypeNameExtractor extractor;

public GsonSerializer(Writer writer, boolean indented, boolean withoutRoot, TypeNameExtractor extractor,
Locale locale) {
this.writer = writer;
this.extractor = extractor;

this.treeFields = new NamedTreeNode(null, null);

String pattern = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, locale))
Expand Down Expand Up @@ -74,16 +80,6 @@ private static Class<?> getTypeOf(Object obj) {
return obj.getClass();
}

private static String getFieldName(Class<?> type) {
String fieldName = type.getSimpleName();
if (fieldName == null || "".equals(fieldName)) {
return null;
}
fieldName = Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1);

return fieldName;
}

private static boolean isPrimitive(Class<?> type) {
return type.isPrimitive() || type.isEnum() || Number.class.isAssignableFrom(type)
|| type.equals(String.class) || Date.class.isAssignableFrom(type)
Expand Down Expand Up @@ -299,7 +295,7 @@ public <T> Serializer from(T object, String alias) {

if (alias == null) {
Class<?> type = getTypeOf(object);
String name = getFieldName(type);
String name = extractor.nameFor(type);

if (isCollection(object.getClass())) {
name = "list";
Expand All @@ -326,6 +322,4 @@ public <T> Serializer from(T object, String alias) {

return this;
}
}

// TODO quando serializo collections ele ignora o withouroot
}
Expand Up @@ -25,6 +25,7 @@
import org.junit.Ignore;
import org.junit.Test;

import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.gson.GsonSerialization;

import com.google.common.collect.ForwardingCollection;
Expand All @@ -47,7 +48,7 @@ public void setup() throws Exception {

when(response.getLocale()).thenReturn(new Locale("en", "US"));

this.serialization = new GsonSerialization(response);
this.serialization = new GsonSerialization(response, new DefaultTypeNameExtractor());
}

public static class Address {
Expand Down Expand Up @@ -190,8 +191,8 @@ public BasicOrder(Client client, double price, String comments, Type type) {

@Test
public void shouldSerializeEnumFields() {
Order order = new BasicOrder(new Client("guilherme silveira"), 15.0,
"pack it nicely, please", Type.basic);
Order order = new BasicOrder(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
Type.basic);
serialization.from(order).serialize();
String result = result();
assertThat(result, containsString("\"type\":\"basic\""));
Expand All @@ -206,7 +207,6 @@ public void shouldSerializeCollection() {
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please");
serialization.from(Arrays.asList(order, order)).serialize();
assertThat(result(), is(equalTo(expectedResult)));

}

@Test
Expand All @@ -221,8 +221,8 @@ public void shouldSerializeCollectionWithPrefixTag() {

@Test
public void shouldExcludeNonPrimitiveFieldsFromACollection() {
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
new Item("name", 12.99));
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"name", 12.99));
serialization.from(Arrays.asList(order, order), "orders").exclude("price").serialize();

assertThat(result(), not(containsString("\"items\"")));
Expand Down Expand Up @@ -279,17 +279,17 @@ public void shouldOptionallyExcludeFields() {

@Test
public void shouldOptionallyIncludeFieldAndNotItsNonPrimitiveFields() {
Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")),
15.0, "pack it nicely, please");
Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0,
"pack it nicely, please");
serialization.from(order).include("client").serialize();
assertThat(result(), containsString("\"name\":\"guilherme silveira\""));
assertThat(result(), not(containsString("R. Vergueiro")));
}

@Test
public void shouldOptionallyIncludeChildField() {
Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")),
15.0, "pack it nicely, please");
Order order = new Order(new Client("guilherme silveira", new Address("R. Vergueiro")), 15.0,
"pack it nicely, please");
serialization.from(order).include("client", "client.address").serialize();
assertThat(result(), containsString("\"street\":\"R. Vergueiro\""));
}
Expand All @@ -306,8 +306,8 @@ public void shouldOptionallyExcludeChildField() {

@Test
public void shouldOptionallyIncludeListChildFields() {
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
new Item("any item", 12.99));
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"any item", 12.99));
serialization.from(order).include("items").serialize();
assertThat(result(), containsString("\"items\""));
assertThat(result(), containsString("\"name\":\"any item\""));
Expand All @@ -316,8 +316,8 @@ public void shouldOptionallyIncludeListChildFields() {

@Test
public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() {
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
new Item("any item", 12.99));
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"any item", 12.99));
serialization.from(order).include("items").exclude("items.price").serialize();
assertThat(result(), containsString("\"items\""));
assertThat(result(), containsString("\"name\":\"any item\""));
Expand All @@ -326,8 +326,8 @@ public void shouldOptionallyExcludeFieldsFromIncludedListChildFields() {

@Test
public void shouldOptionallyRemoveRoot() {
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
new Item("any item", 12.99));
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"any item", 12.99));
serialization.withoutRoot().from(order).include("items").exclude("items.price").serialize();
assertThat(result(), containsString("\"items\""));
assertThat(result(), containsString("\"name\":\"any item\""));
Expand All @@ -338,8 +338,8 @@ public void shouldOptionallyRemoveRoot() {
@Test
public void shouldOptionallyRemoveRootIdented() {
String expected = "{\n \"price\": 15.0,\n \"items\": [\n {\n \"name\": \"any item\"\n }\n ],\n \"comments\": \"pack it nicely, please\"\n}";
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please",
new Item("any item", 12.99));
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"any item", 12.99));

serialization.indented().withoutRoot().from(order).include("items").exclude("items.price")
.serialize();
Expand Down Expand Up @@ -376,7 +376,6 @@ public Object writeReplace() {

}

// TODO: Serializando proxy hibernate
@Test
public void shouldRunHibernateLazyInitialization() throws Exception {
String expected = "{\"client\":{\"name\":\"my name\",\"aField\":\"abc\"}}";
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.junit.Before;
import org.junit.Test;

import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.gson.GsonSerialization;
import br.com.caelum.vraptor.test.model.gson.Address;
import br.com.caelum.vraptor.test.model.gson.Customer;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void setup() throws Exception {
when(response.getWriter()).thenReturn(new PrintWriter(output));
when(response.getLocale()).thenReturn(new Locale("pt", "BR"));

this.gsonSerialization = new GsonSerialization(response);
this.gsonSerialization = new GsonSerialization(response, new DefaultTypeNameExtractor());
this.currentDate = new Date();
this.currentDateAsStr = sdf.format(currentDate);
}
Expand Down

0 comments on commit ec2b0ed

Please sign in to comment.