简体   繁体   English

BeanIO-在封送处理以固定宽度文件时禁用填充或跳过字段(如果为null)

[英]BeanIO - Disable padding or skip field if null when marshaling to fix width file

I'm trying to write a fixed-width file using BeanIO library. 我正在尝试使用BeanIO库编写固定宽度的文件。 Here's the record in question: 这是有问题的记录:

@Record
open class KeySegment(
    @Field(at = 0, length = 1, required = true) var recordType: String = "",
    @Field(at = 1, length = 6, required = true) var primaryCorpId: String = "",
    @Field(at = 7, length = 16, minOccurs = 0) var creditCardAcc: String? = null,
    @Field(at = 7, length = 8, minOccurs = 0) var companyId: String? = null,
    @Field(at = 15, length = 8, minOccurs = 0) var sublevelId: String? = null,
    @Field(at = 23, length = 8, required = true) var fileCreateDate: String = "",
    @Field(at = 31, length = 8) var sourceId: String = "",
    @Field(at = 39, length = 816) var filler: String = ""
)

Notice that creditCardAcc and companyId + sublevelId hold the same positions in the file. 请注意, creditCardAcccreditCardAcc companyId + sublevelId creditCardAcc在文件中保持相同位置。 Depending on the use case, we either set creditCardAcc field or companyId and sublevelId . 根据不同的使用情况下,我们要么设置creditCardAcc场或companyIdsublevelId Now for my use case, I want to set creditCardAcc , but the problem is that companyId and sublevelId are padded with space and overwrite the creditCardAcc field even if they are set to null . 现在,我使用的情况下,我想设置creditCardAcc ,但问题是, companyIdsublevelId填充为空间和覆盖creditCardAcc即使它们被设置为场null

One solution is to pull those fields into two subclasses extending KeySegment , and marshal subclass instead. 一种解决方案是将这些字段放入扩展KeySegment两个子类中,并KeySegment类。 But, I was wondering if there's a better native solution that I can use to accomplish this. 但是,我想知道是否有更好的本机解决方案可以用来完成此任务。 For example, is there a way to disable the padding if the field is null? 例如,如果该字段为null,是否可以禁用填充?

Thanks. 谢谢。

There is no way to disable the padding when the field is null . 当该字段为null时,无法禁用填充。 The padding attribute has the following description here padding属性在此处具有以下描述

If padding is enabled, the required field attribute has some control over the marshalling and unmarshalling of null values. 如果启用了填充,则必填字段属性可以控制空值的编组和解组。

When unmarshalling a field consisting of all spaces in a fixed length stream, if required is false, the field is accepted regardless of the padding character. 当解组一个由固定长度流中的所有空格组成的字段时,如果要求为false,则不管填充字符如何,都接受该字段。 If required is true, a required field validation error is triggered. 如果requiredment为true,则触发必填字段验证错误。 And when marshalling a null field value, if required is false, the field text is formatted as spaces regardless of the configured padding character. 并且,当编组一个空字段值时,如果required为false,则无论配置的填充字符如何,字段文本都将格式化为空格。

The last sentence of the quote is exactly what you are asking about. 引用的最后一句话正是您要问的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM