Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.insula</groupId>
<artifactId>opes</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.4.1</version>
<name>Projeto Opes</name>
<description>Sugestões de classes imutáveis para Rich Domain Model de casos de uso do Brasil</description>
<organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.insula.opes.json.deserializer;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

import br.com.insula.opes.Cnpj;

public class CnpjJsonDeserializer extends JsonDeserializer<Cnpj> {

@Override
public Cnpj deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Cnpj.fromString(jp.getText());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.insula.opes.json.deserializer;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

import br.com.insula.opes.Cpf;

public class CpfJsonDeserializer extends JsonDeserializer<Cpf> {

@Override
public Cpf deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Cpf.fromString(jp.getText());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.insula.opes.json.serializer;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;

import br.com.insula.opes.Cnpj;

public class CnpjJsonSerializer extends SerializerBase<Cnpj> {

public CnpjJsonSerializer() {
super(Cnpj.class);
}

@Override
public void serialize(Cnpj value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(value.toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package br.com.insula.opes.json.serializer;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;

import br.com.insula.opes.Cpf;

public class CpfJsonSerializer extends SerializerBase<Cpf> {

public CpfJsonSerializer() {
super(Cpf.class);
}

@Override
public void serialize(Cpf value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package br.com.insula.opes.json.deserializer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;

import br.com.insula.opes.Cnpj;

public class CnpjJsonDeserializerTest {

@Test
public void testDeserializeJsonParserDeserializationContext() throws Exception {
ObjectMapper mapper = createObjectMapper();

String json = "{\"cnpj\":\"86266937000129\"}";
Teste teste = mapper.readValue(json, Teste.class);
assertEquals(Cnpj.fromString("86.266.937/0001-29"), teste.getCnpj());
}

private ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null));
module.addDeserializer(Cnpj.class, new CnpjJsonDeserializer());
objectMapper.registerModule(module);
return objectMapper;
}

@Test
public void testDeserializeJsonParserDeserializationContextWithNull() throws Exception {
ObjectMapper mapper = createObjectMapper();

String json = "{\"cnpj\":null}";
Teste teste = mapper.readValue(json, Teste.class);
assertNull(teste.getCnpj());
}

static class Teste {
private Cnpj cnpj;

public Cnpj getCnpj() {
return cnpj;
}

public void setCnpj(Cnpj cnpj) {
this.cnpj = cnpj;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package br.com.insula.opes.json.deserializer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;

import br.com.insula.opes.Cpf;

public class CpfJsonDeserializerTest {

@Test
public void testDeserializeJsonParserDeserializationContext() throws Exception {
ObjectMapper mapper = createObjectMapper();

String json = "{\"cpf\":\"00000000191\"}";
Teste teste = mapper.readValue(json, Teste.class);
assertEquals(Cpf.fromString("000.000.001-91"), teste.getCpf());
}

private ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null));
module.addDeserializer(Cpf.class, new CpfJsonDeserializer());
objectMapper.registerModule(module);
return objectMapper;
}

@Test
public void testDeserializeJsonParserDeserializationContextWithNull() throws Exception {
ObjectMapper mapper = createObjectMapper();

String json = "{\"cpf\":null}";
Teste teste = mapper.readValue(json, Teste.class);
assertNull(teste.getCpf());
}

static class Teste {
private Cpf cpf;

public Cpf getCpf() {
return cpf;
}

public void setCpf(Cpf cpf) {
this.cpf = cpf;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package br.com.insula.opes.json.serializer;

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;

import br.com.insula.opes.Cep;

public class CepJsonSerializerTest {

@Test
public void testSerializeCepJsonGeneratorSerializerProvider() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null));
module.addSerializer(Cep.class, new CepJsonSerializer());
mapper.registerModule(module);

StringWriter writer = new StringWriter();
mapper.writeValue(writer, new Teste());
assertEquals("{\"cep\":\"87030020\"}", writer.toString());
}

static class Teste {
private Cep cep = Cep.fromString("87030020");

public Cep getCep() {
return cep;
}

public void setCep(Cep cpfCnpj) {
this.cep = cpfCnpj;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package br.com.insula.opes.json.serializer;

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;

import br.com.insula.opes.Cnpj;

public class CnpjJsonSerializerTest {

@Test
public void testSerializeCnpjJsonGeneratorSerializerProvider() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null));
module.addSerializer(Cnpj.class, new CnpjJsonSerializer());
mapper.registerModule(module);

StringWriter writer = new StringWriter();
mapper.writeValue(writer, new Teste());
assertEquals("{\"cnpj\":\"86266937000129\"}", writer.toString());
}

static class Teste {
private Cnpj cnpj = Cnpj.fromString("86.266.937/0001-29");

public Cnpj getCnpj() {
return cnpj;
}

public void setCnpj(Cnpj cnpj) {
this.cnpj = cnpj;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package br.com.insula.opes.json.serializer;

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;

import br.com.insula.opes.Cpf;

public class CpfJsonSerializerTest {

@Test
public void testSerializeCpfJsonGeneratorSerializerProvider() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null));
module.addSerializer(Cpf.class, new CpfJsonSerializer());
mapper.registerModule(module);

StringWriter writer = new StringWriter();
mapper.writeValue(writer, new Teste());
assertEquals("{\"cpf\":\"00000000191\"}", writer.toString());
}

static class Teste {
private Cpf cpf = Cpf.fromString("000.000.001-91");

public Cpf getCpf() {
return cpf;
}

public void setCpf(Cpf cpf) {
this.cpf = cpf;
}
}
}