Skip to content

Commit

Permalink
Customizando serializer e desserializer de JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
marotinos committed Dec 21, 2011
1 parent bb812cd commit 535971d
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v6.0"/>
<runtime name="Apache Tomcat v7.0"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/br/com/caelum/cadastro/modelo/Aluno.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import com.thoughtworks.xstream.annotations.XStreamAlias;


@Entity
//@XStreamAlias("aluno")
@XStreamAlias("aluno")
public class Aluno {
@Id @GeneratedValue
private long id;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/br/com/caelum/controllers/AlunoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public class AlunoController {

@Post("/backup")
@Consumes("application/json")
public void backup(List<Aluno> list) {
System.out.println(list);
// result.use(Results.nothing());
result.use(Results.json()).from(list).serialize();
public void backup(List<Aluno> alunos) {
result.use(Results.json()).from(alunos).serialize();
}

@Get("/listaTodosAlunos")
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/br/com/caelum/model/Alunos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package br.com.caelum.model;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

import br.com.caelum.cadastro.modelo.Aluno;

@XStreamAlias("alunos")
public class Alunos {

@XStreamImplicit(itemFieldName="lista")//, keyFieldName="aluno")
private List<Aluno> lista = new ArrayList<Aluno>();

@Override
public String toString() {
return this.lista.toString();
}

public void addAlunos(List<Aluno> alunos) {
this.lista.addAll(alunos);
}

public void setLista(List<Aluno> lista) {
this.lista = lista;
}

public List<Aluno> getLista() {
return lista;
}
}
74 changes: 0 additions & 74 deletions src/main/java/br/com/caelum/models/SincronismoServer.java

This file was deleted.

81 changes: 0 additions & 81 deletions src/main/java/br/com/caelum/models/SincronismoServerPost.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/br/com/caelum/support/AnnotatedXStreamClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package br.com.caelum.support;

import br.com.caelum.cadastro.modelo.Aluno;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class AnnotatedXStreamClasses {
private Class<?>[] types = {Aluno.class};

public Class<?>[] getTypes() {
return types;
}
}
30 changes: 30 additions & 0 deletions src/main/java/br/com/caelum/support/CustomJSONSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.com.caelum.support;

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.ProxyInitializer;
import br.com.caelum.vraptor.serialization.xstream.XStreamJSONSerialization;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

@Component
public class CustomJSONSerializer extends XStreamJSONSerialization {
private AnnotatedXStreamClasses classes;

public CustomJSONSerializer(HttpServletResponse response,
TypeNameExtractor extractor, ProxyInitializer initializer, AnnotatedXStreamClasses classes) {

super(response, extractor, initializer);
this.classes = classes;
}

@Override
protected XStream getXStream() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.processAnnotations(classes.getTypes());
return xStream;
}
}
15 changes: 7 additions & 8 deletions src/main/java/br/com/caelum/support/CustomJsonDeserializer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package br.com.caelum.support;

import java.util.ArrayList;

import br.com.caelum.cadastro.modelo.Aluno;
import br.com.caelum.vraptor.deserialization.JsonDeserializer;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
Expand All @@ -12,15 +9,17 @@

@Component
public class CustomJsonDeserializer extends JsonDeserializer {
public CustomJsonDeserializer(ParameterNameProvider provider, TypeNameExtractor extractor) {
private AnnotatedXStreamClasses classes;

public CustomJsonDeserializer(ParameterNameProvider provider, TypeNameExtractor extractor, AnnotatedXStreamClasses classes) {
super(provider, extractor);
this.classes = classes;
}

public XStream getConfiguredXStream(java.lang.reflect.Method javaMethod, java.lang.Class<?>[] types) {

XStream xStream = super.getConfiguredXStream(javaMethod, types);
xStream.alias("aluno", Aluno.class);
xStream.alias("list", ArrayList.class);

xStream.processAnnotations(classes.getTypes());
return xStream;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public class AlunoControllerTest {

@Test public void fakeTest() {
assertNotNull("put something real.", new AlunoController(null, null, null));
assertNotNull("put something real.", new Object());
}
}
Binary file removed tmp/devdb.lck
Binary file not shown.
14 changes: 3 additions & 11 deletions tmp/devdb.log
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
/*C6*/SET SCHEMA PUBLIC
INSERT INTO ALUNO VALUES(3,'Rua Eca de Queiros 279 Vila Mariana SP','GUI',9.8E0,'aqui','4523452')
COMMIT
/*C5*/SET SCHEMA PUBLIC
/*C33*/SET SCHEMA PUBLIC
DISCONNECT
/*C7*/SET SCHEMA PUBLIC
/*C32*/SET SCHEMA PUBLIC
DISCONNECT
/*C6*/DISCONNECT
/*C9*/SET SCHEMA PUBLIC
DISCONNECT
/*C8*/SET SCHEMA PUBLIC
DISCONNECT
/*C10*/SET SCHEMA PUBLIC
/*C34*/SET SCHEMA PUBLIC
DISCONNECT
2 changes: 1 addition & 1 deletion tmp/devdb.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#HSQL Database Engine 2.2.4
#Mon Dec 19 14:30:43 BRST 2011
#Wed Dec 21 17:04:36 BRST 2011
version=2.2.4
modified=yes
3 changes: 2 additions & 1 deletion tmp/devdb.script
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ALTER USER SA SET LOCAL TRUE
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
SET SCHEMA PUBLIC
CREATE MEMORY TABLE PUBLIC.ALUNO(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,ENDERECO VARCHAR(255),NOME VARCHAR(255),NOTA DOUBLE,SITE VARCHAR(255),TELEFONE VARCHAR(255))
ALTER TABLE PUBLIC.ALUNO ALTER COLUMN ID RESTART WITH 3
ALTER TABLE PUBLIC.ALUNO ALTER COLUMN ID RESTART WITH 4
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
Expand All @@ -48,3 +48,4 @@ INSERT INTO BLOCKS VALUES(0,2147483647,0)
SET SCHEMA PUBLIC
INSERT INTO ALUNO VALUES(1,'Rua Eca de Queiros 279 Vila Mariana SP','Erich',10.0E0,'www.caelum.com.br','82574693')
INSERT INTO ALUNO VALUES(2,'Rua Vergueiro 3128 Vila Mariana SP','Eric',9.0E0,'www.yahoo.com.br','2341241')
INSERT INTO ALUNO VALUES(3,'Rua Eca de Queiros 279 Vila Mariana SP','GUI',9.8E0,'aqui','4523452')

0 comments on commit 535971d

Please sign in to comment.