Cobol parser and decimal fields

15 views
Skip to first unread message

jake.mccrary

unread,
Sep 15, 2009, 11:59:17 AM9/15/09
to Flapjack Parser
I'm having issues when trying to use decimal cobol field definitions
and the results being mapped properly to floats/doubles. Issue 16
(http://code.google.com/p/flapjack/issues/detail?id=16) seems to
address this and is marked as done. Any suggestions on what I'm doing
incorrectly?

I've put some sample code which shows this issue below.

public class Decimals {
public float price;
public double quantity;
public Double other;
}

public class FlapjackTests {
private static class DecimalsRecordLayout extends
AbstractCobolRecordLayout {
@Override
protected void defineFields() {
cobolField("Price", "9(3)v9(2)");
cobolField("Quantity", "999v99");
cobolField("Other", "9(3)v99");
}
}

private static class DecimalsRecordFactory implements
RecordFactory {
@Override
public Object build() {
return new Decimals();
}
}

@Test
public void shouldShowFlapjackWorks() throws IOException {
ObjectMapping decimalMapping = new ObjectMapping
(Decimals.class);
decimalMapping.add("Price", "price");
decimalMapping.add("Quantity", "quantity");
decimalMapping.add("Other", "other");

ObjectMappingStore objectMappingStore = new ObjectMappingStore
();
objectMappingStore.add(decimalMapping);

RecordParserImpl recordParser = new RecordParserImpl();
recordParser.setRecordLayoutResolver(new
SameRecordLayoutResolver(DecimalsRecordLayout.class));
recordParser.setRecordFactoryResolver(new
SameRecordFactoryResolver(DecimalsRecordFactory.class));
recordParser.setObjectMappingStore(objectMappingStore);
recordParser.setIgnoreUnmappedFields(true);

String records = "123451234512345";

LineRecordReader recordReader = new LineRecordReader(new
ByteArrayInputStream(records.getBytes()));
ParseResult result = recordParser.parse(recordReader);
List results = result.getRecords();
assertEquals(1, results.size());
Decimals dec = (Decimals) results.get(0);
System.out.println(dec.price);
System.out.println(dec.quantity);
System.out.println(dec.other);
assertEquals(123.45, dec.price);
assertEquals(123.45, dec.quantity);
assertEquals(123.45, dec.other);
}

born2snipe

unread,
Sep 15, 2009, 7:21:35 PM9/15/09
to Flapjack Parser

Here are the steps you will need to take:
1. Create a new class that implements the ValueConverter interface.
It's responsibility will be parsing your decimal value into the float/
decimal.
2. When defining your fields on your ObjectMapping you will need to
specify what ValueConverter should be used at the time of data
coversion.
3. You will need to create a TypeConverter and register instances of
your newly created ValueConverters into the TypeConverter, and then
set the type converter on your RecordParser

From your example TestCase given I have modified it to work for your
example: https://gist.github.com/187744

I hope this helps you out.

Thanks and happy parsing! :)

Jake McCrary

unread,
Sep 16, 2009, 11:32:33 AM9/16/09
to flapjac...@googlegroups.com
That does help out.  Thanks.

It seems that except for the added expressiveness 9(3)v9(2) there isn't an advantage of declaring the field that was as opposed to 9(5).  Is that correct or is there some subtlety I'm missing?

born2snipe

unread,
Sep 16, 2009, 12:38:04 PM9/16/09
to Flapjack Parser
I agree with your statement of it just being more expressive.

After this brief discussion, I agree it would be much nicer if it
would convert the COBOL decimal values directly to the float, double,
or BigDecimal. If you would like make this an enhancement that would
be great. Issue list: http://code.google.com/p/flapjack/issues/list

On Sep 16, 10:32 am, Jake McCrary <jake...@gmail.com> wrote:
> That does help out.  Thanks.
>
> It seems that except for the added expressiveness 9(3)v9(2) there isn't an
> advantage of declaring the field that was as opposed to 9(5).  Is that
> correct or is there some subtlety I'm missing?
>

Jake McCrary

unread,
Sep 17, 2009, 10:04:27 AM9/17/09
to flapjac...@googlegroups.com
I'm traveling some this weekend, I'll try to take a look at it.

Jake McCrary

unread,
Sep 19, 2009, 3:37:49 PM9/19/09
to flapjac...@googlegroups.com
Added a TwoDecimalPlacesDoubleTextConverter class and a few tests around it.  Forked on github: http://github.com/jakemcc/flapjack

This doesn't really address the issue of the COBOL definitions not providing more functionality than more expressiveness.   Still need to specify this as the converter to be used. Basically moved over a class I've added to my own personal project into flapjack.

Would like to put some more work into making this an automatic thing if COBOL definitions are used.  Still trying to figure out where something like that would be added and what would need to change to make that happen.

born2snipe

unread,
Oct 27, 2009, 9:05:17 PM10/27/09
to Flapjack Parser
Jake,

I implemented the auto-conversion for the cobol api. I updated the
cobol example (http://github.com/born2snipe/flapjack/blob/master/
flapjack-example/src/test/java/flapjack/example/CobolTest.java) to
take advantage of the auto-conversion. I would appreciate it if you
would try this code out and let me know how well it works for you or
improvements that would make life easier.

Thanks,
Dan

On Sep 19, 2:37 pm, Jake McCrary <jake...@gmail.com> wrote:
> Added a TwoDecimalPlacesDoubleTextConverter class and a few tests around
> it.  Forked on github:http://github.com/jakemcc/flapjack
>
> This doesn't really address the issue of the COBOL definitions not providing
> more functionality than more expressiveness.   Still need to specify this as
> the converter to be used. Basically moved over a class I've added to my own
> personal project into flapjack.
>
> Would like to put some more work into making this an automatic thing if
> COBOL definitions are used.  Still trying to figure out where something like
> that would be added and what would need to change to make that happen.
>
> On Thu, Sep 17, 2009 at 9:04 AM, Jake McCrary <jake...@gmail.com> wrote:
> > I'm traveling some this weekend, I'll try to take a look at it.
>
Reply all
Reply to author
Forward
0 new messages