Support CSV Deserialization to EnumMaps
See original GitHub issueDuring the last years I did a lot of CSV reading (with and without Jackson). When parsing this type of data with Jackson, I often serialize rows to maps, when there is some sort of dynamics in the data that is impractical to handle with POJO mappers.
This is a typical example:
CsvSchema headerSchema = CsvSchema.emptySchema().withHeader();
MappingIterator<Map<String, String>> it = mapper
.readerForMapOf(String.class)
.with(schema)
.readValues(input);
while (it.hasNext()) {
Map<String, String> row = it.nextValue();
String xVal = row.get("x");
String yVal = row.get("y");
String temperature = row.get("temperature");
// do something with the values
...
}
The usual situation is that all the valid header values stem from a fixed set of strings, so there is an immediate association with Java Enums. I would like to do something like this:
enum MyEnum {
X, Y, TEMPERATURE, HUMIDITY, ... ;
}
static MyEnum parse(String s) {
... // a separate method or an implementation detail of the enum class
}
...
CsvSchema headerSchema = CsvSchema.emptySchema().withHeader();
MappingIterator<EnumMap<MyEnum, String>> it = mapper
.readerForMapOf(MyEnum.class, h -> parse(h), String) // specify enum type, mapping function header->enum, value class
.with(schema)
.readValues(input);
while (it.hasNext()) {
EnumMap<MyEnum, String> row = it.nextValue();
String xVal = row.get(X);
String yVal = row.get(Y);
String temperature = row.get(TEMPERATURE);
// do something with the values
...
}
Besides rigour, Enum headers / EnumMaps have other advantages, such as internal optimizations for speed and memory footprint (compared to HashMap
or LinkedHashMap
).
Would that proposal be something that is within the scope of this library?
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (8 by maintainers)
Top Results From Across the Web
How to (de)serialize an EnumMap using Jackson and default ...
Defines a deserializer for each and any used map class, as there is no inheritence support ind eserialization * @return */ @Produces public ......
Read more >How To Serialize and Deserialize Enums with Jackson
In this quick tutorial, we'll learn how to control the way Java Enums are serialized and deserialized with Jackson 2.
Read more >Serialized Form (jackson-databind 2.4.2 API) - javadoc.io
Factory used for constructing JsonNode instances. Class com.fasterxml.jackson.databind.DeserializationContext extends DatabindContext implements Serializable.
Read more >dojo/data/ItemFileReadStore — The Dojo Toolkit - Reference ...
This is a JavaScript Object that defines how to deserialize custom types. For more information on custom types, please refer to the section...
Read more >IBM Content Collector 4.0.1.10 Interim Fix 12
With this fix, VERITY will not be supported with ICC as VERITY ... ExchangeOnlineMigratedUsers.csv file has double quotes (“ ”).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok, so what I mean is that would adding of
@JsonCreator
next toparse
method work – it should, I think. And for serialization,@JsonValue
next to methodcode()
.In fact, I think that use of
@JsonValue
should handle both cases, if external representation is the value that method returns.This aspect of Enums is not well documented as improvements have been added piece by piece over time.
Finally, also notice that you can use
@JsonProperty
on enum entries: value specified is the external value (one included in JSON) to use, and would overrideEnum.name()
.At this point, this issue would properly belong in
jackson-databind
. Could you file it there? I thinkSKIP_UNKNOWN_MAP_KEYS
would make sense (it is slightly redundant as onlyMap
s have keys, but it seems reasonable to me wrt existing feature naming).