jackson work

This commit is contained in:
eugenp 2013-12-21 16:49:46 +02:00
parent 6f9026a183
commit 134248ac94
2 changed files with 79 additions and 60 deletions

View File

@ -1,72 +1,25 @@
package org.baeldung.jackson;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.google.common.collect.Lists;
public class JacksonUnitTest {
// tests - single entity to json
@Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new FooDto());
assertThat(dtoAsString, not(containsString("intValue")));
System.out.println(dtoAsString);
}
@Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final FooDto dtoObject = new FooDto();
dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@Test
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final BarDto dtoObject = new BarDto();
dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
// tests - multiple entities to json
@Test
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
final ObjectMapper mapper = new ObjectMapper();
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
System.out.println(dtosAsString);
}
public class JacksonDeserializationUnitTest {
// tests - json to single entity
@ -120,14 +73,14 @@ public class JacksonUnitTest {
assertThat(readValue.getStringValue(), equalTo("a"));
assertThat(readValue.isBooleanValue(), equalTo(true));
assertThat(readValue.getIntValue(), equalTo(1));
}
// tests - json to multiple entities
mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException {
return super.handleUnknownProperty(ctxt, jp, deserializer, beanOrClass, propertyName);
}
});
}
}
/*
Article Ideas:
- Deserializing with a custom JsonParser
- Jackson Ignore: ignore specific fields at field level, at class level,
*/

View File

@ -0,0 +1,66 @@
package org.baeldung.jackson;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
public class JacksonSerializationUnitTest {
// tests - single entity to json
@Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new FooDto());
assertThat(dtoAsString, not(containsString("intValue")));
System.out.println(dtoAsString);
}
@Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final FooDto dtoObject = new FooDto();
dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@Test
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final BarDto dtoObject = new BarDto();
dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
// tests - multiple entities to json
@Test
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
final ObjectMapper mapper = new ObjectMapper();
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
System.out.println(dtosAsString);
}
}