minor jaackson example and new streaming logic for large files

This commit is contained in:
eugenp 2014-01-15 19:53:33 +02:00
parent ff141b5afb
commit 0f9fef5337
5 changed files with 128 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,9 +30,9 @@ public class CoreJavaIoIntegrationTest {
}
@Test
public final void givenUsingCommonsIo_whenIteratingAFile_thenCorrect() throws IOException {
final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
// final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
public final void givenUsingCommonsIo_whenIteratingAFileInMemory_thenCorrect() throws IOException {
// final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
logMemory();
FileUtils.readLines(new File(path));
@ -70,6 +71,26 @@ public class CoreJavaIoIntegrationTest {
logMemory();
}
@Test
public final void givenUsingApacheIo_whenStreamingThroughAFile_thenCorrect() throws IOException {
final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
// final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
logMemory();
final LineIterator it = FileUtils.lineIterator(new File(path), "UTF-8");
try {
while (it.hasNext()) {
final String line = it.nextLine();
// do something with line
}
} finally {
LineIterator.closeQuietly(it);
}
logMemory();
}
// utils
private final void logMemory() {

View File

@ -0,0 +1,4 @@
package org.baeldung.jackson.try1;
public interface IEntity {
public int getId();
}

View File

@ -0,0 +1,25 @@
package org.baeldung.jackson.try1;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.dtos.ItemWithSerializer;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDeserializationUnitTest {
@Test
public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":2}";
final ItemWithSerializer readValue = new ObjectMapper().readValue(json, ItemWithSerializer.class);
assertThat(readValue, notNullValue());
}
}

View File

@ -0,0 +1,38 @@
package org.baeldung.jackson.try1;
import java.io.Serializable;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(using = RestLoaderRequestDeserializer.class)
// @Produces(MediaType.APPLICATION_JSON)
public class RestLoaderRequest<T extends IEntity> implements Serializable {
private T entity; // entity to load field to
private String className; // actual class of entity
private String fieldName; // fieldName to lazy REST load
public String getFieldName() {
return fieldName;
}
public void setFieldName(final String fieldName) {
this.fieldName = fieldName;
}
public String getClassName() {
return className;
}
public void setClassName(final String className) {
this.className = className;
}
public T getEntity() {
return entity;
}
public void setEntity(final T entity) {
this.entity = entity;
}
}

View File

@ -0,0 +1,37 @@
package org.baeldung.jackson.try1;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
public class RestLoaderRequestDeserializer extends JsonDeserializer<RestLoaderRequest<IEntity>> {
@Override
public RestLoaderRequest<IEntity> deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
try {
final ObjectCodec objectCodec = jp.getCodec();
final JsonNode node = objectCodec.readTree(jp);
final String className = node.get("className").textValue();
final String fieldName = node.get("fieldName").textValue();
final Class<?> clazz = Class.forName(className);
final JsonNode rawEntityNode = node.get("entity");
// How to deserialize rawEntityNode to T based on className ?
final RestLoaderRequest<IEntity> request = new RestLoaderRequest<IEntity>();
request.setClassName(className);
request.setFieldName(fieldName);
} catch (final ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}