use of org.simpleflatmapper.csv.ParsingContext in project SimpleFlatMapper by arnaudroger.
the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithBoxReader.
@Test
public void testCustomReaderPrimitivesWithBoxReader() throws IOException {
CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Boolean>() {
@Override
public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return true;
}
})).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Byte>() {
@Override
public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 35;
}
})).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Character>() {
@Override
public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 37;
}
})).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Short>() {
@Override
public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 39;
}
})).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Integer>() {
@Override
public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0x76666;
}
})).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Long>() {
@Override
public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0xf76666l;
}
})).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Float>() {
@Override
public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.14f;
}
})).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Double>() {
@Override
public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.1526;
}
})).mapper();
DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
assertEquals(true, object.ispBoolean());
assertEquals(35, object.getpByte());
assertEquals(37, object.getpCharacter());
assertEquals(39, object.getpShort());
assertEquals(0x76666, object.getpInt());
assertEquals(0xf76666l, object.getpLong());
assertEquals(3.14f, object.getpFloat(), 0.0001);
assertEquals(3.1526, object.getpDouble(), 0.000001);
}
use of org.simpleflatmapper.csv.ParsingContext in project SimpleFlatMapper by arnaudroger.
the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithDirectReader.
@Test
public void testCustomReaderPrimitivesWithDirectReader() throws IOException {
CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new BooleanCellValueReader() {
@Override
public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return true;
}
@Override
public boolean readBoolean(char[] chars, int offset, int length, ParsingContext parsingContext) {
return true;
}
})).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new ByteCellValueReader() {
@Override
public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 35;
}
@Override
public byte readByte(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 35;
}
})).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CharCellValueReader() {
@Override
public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 37;
}
@Override
public char readChar(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 37;
}
})).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new ShortCellValueReader() {
@Override
public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 39;
}
@Override
public short readShort(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 39;
}
})).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new IntegerCellValueReader() {
@Override
public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0x76666;
}
@Override
public int readInt(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0x76666;
}
})).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new LongCellValueReader() {
@Override
public long readLong(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0xf76666l;
}
@Override
public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 0xf76666l;
}
})).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new FloatCellValueReader() {
@Override
public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.14f;
}
@Override
public float readFloat(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.14f;
}
})).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new DoubleCellValueReader() {
@Override
public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.1526;
}
@Override
public double readDouble(char[] chars, int offset, int length, ParsingContext parsingContext) {
return 3.1526;
}
})).mapper();
DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
assertEquals(true, object.ispBoolean());
assertEquals(35, object.getpByte());
assertEquals(37, object.getpCharacter());
assertEquals(39, object.getpShort());
assertEquals(0x76666, object.getpInt());
assertEquals(0xf76666l, object.getpLong());
assertEquals(3.14f, object.getpFloat(), 0.0001);
assertEquals(3.1526, object.getpDouble(), 0.000001);
}
use of org.simpleflatmapper.csv.ParsingContext in project SimpleFlatMapper by arnaudroger.
the class CsvMapperCustomReaderTest method testCustomCsvReaderValueFactory.
@Test
public void testCustomCsvReaderValueFactory() throws IOException {
CsvMapper<Tuple2<String, String>> csvMapper = CsvMapperFactory.newInstance().addColumnDefinition(new Predicate<CsvColumnKey>() {
@Override
public boolean test(CsvColumnKey csvColumnKey) {
return true;
}
}, CsvColumnDefinition.customCellValueReaderFactoryDefinition(new CellValueReaderFactory() {
@SuppressWarnings("unchecked")
@Override
public <P> CellValueReader<P> getReader(Type propertyType, final int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder builder) {
return (CellValueReader<P>) new CellValueReader<String>() {
@Override
public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return "g" + index;
}
};
}
})).newMapper(Tuples.typeDef(String.class, String.class));
Tuple2<String, String> value = csvMapper.iterator(new StringReader("b0,b1\nc0,c1")).next();
assertEquals("g0", value.first());
assertEquals("g1", value.second());
}
use of org.simpleflatmapper.csv.ParsingContext in project SimpleFlatMapper by arnaudroger.
the class CsvParserTest method testDSLMapWithCustomDefinitionOnStaticMapper.
@Test
public void testDSLMapWithCustomDefinitionOnStaticMapper() throws Exception {
Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).addMapping("0").addMapping("1", CsvColumnDefinition.customReaderDefinition(new CellValueReader<String>() {
@Override
public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return "c1";
}
})).iterator(new StringReader("0,1\nv0,v1"));
Tuple2<String, String> tuple = iterator.next();
assertEquals("v0", tuple.first());
assertEquals("c1", tuple.second());
}
use of org.simpleflatmapper.csv.ParsingContext in project SimpleFlatMapper by arnaudroger.
the class CsvParserTest method testDSLMapWithCustomDefinition.
// IFJAVA8_END
@Test
public void testDSLMapWithCustomDefinition() throws Exception {
Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).columnDefinition("1", CsvColumnDefinition.customReaderDefinition(new CellValueReader<String>() {
@Override
public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return "c1";
}
})).iterator(new StringReader("0,1\nv0,v1"));
Tuple2<String, String> tuple = iterator.next();
assertEquals("v0", tuple.first());
assertEquals("c1", tuple.second());
}
Aggregations