Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Allow specifying custom String placeholder to use when a POJO would serialize as Object #85

Closed
MattFriedmanAfilias opened this issue Jul 20, 2015 · 5 comments

Comments

@MattFriedmanAfilias
Copy link

When an object is encountered when writing CSV, the library throws an exception.

Problem: The model might be changed and an object field could be introduced. In this case, the developer will not necessarily update any tests to include the new field.

If that happens the error may only be detected at runtime. We want to detect such problems sooner, or avoid them altogether.

It should be possible to detect the error sooner, or to introduce a setting that would cause the exception to not be thrown and to provide instead some default behavior.

Such behavior might be to simply print "Object" or "n/a" when an object is encountered, rather than throwing an exception; a log message warning about this might be useful too.

The developer should be in control of this setting IMO. The library should make it possible for the developer to decide on the behavior rather than only throwing an exception for this scenario.

Thanks :)

@cowtowncoder
Copy link
Member

I think that what you want to use is:

csvFactory.disable(JsonGenerator.Feature.IGNORE_UNKNOWN);

(despite term JsonGenerator.Feature, these are features available for other dataformats too, name is due to historical reasons)

which should behave such that write is effectively ignored.

I am not sure exactly how your definition is mismatching (perhaps it is difference between interface used for constructing CsvSchema vs. actual implementation class?), but in general it may not be possible to determine incompatibility before writes. Csv encoder only has CsvSchema to determine mapping from logical name to column index; and databind (which converts POJOs to event stream) has no knowledge of dataformat-specific handling.

@cowtowncoder
Copy link
Member

One other feature that may prove useful for CSV as well as other formats is to define so-called "any setter" (see @JsonAnySetter annotation): if such method is defined, it will be called with unknown values. I usually use it to collect information on problematic usage without (necessarily) failing processing; it can help with forward/backward compatibility.

@cowtowncoder
Copy link
Member

Hmmh. Look at that: decided to check IGNORE_UNKNOWN, and turns out that while it works for simple scalar values (numbers, Strings), there's still a failure for Map/List structures.
So I'll at least fix that one first.

@cowtowncoder
Copy link
Member

I think it is still a good idea to allow use of a placeholder value, similar to have null value can be set to a String like "N/A". But due to timing, this will have to wait until 2.8.

In the meantime, skipping of unknown properties, including Object-valued (Structured POJOs) will work with 2.7.0.

@cowtowncoder cowtowncoder changed the title Request for default CSV behavior setting when an object is encountered (instead of only throwing an exception) [2.8] Allow specifying custom String placeholder to use when a POJO would serialize as Object Jan 5, 2016
@cowtowncoder cowtowncoder changed the title [2.8] Allow specifying custom String placeholder to use when a POJO would serialize as Object Allow specifying custom String placeholder to use when a POJO would serialize as Object Mar 20, 2017
@cowtowncoder
Copy link
Member

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants