question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support CSV Deserialization to EnumMaps

See original GitHub issue

During 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:closed
  • Created 2 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jun 18, 2021

Ok, so what I mean is that would adding of @JsonCreator next to parse method work – it should, I think. And for serialization, @JsonValue next to method code().

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 override Enum.name().

0reactions
cowtowncodercommented, Jun 25, 2021

At this point, this issue would properly belong in jackson-databind. Could you file it there? I think SKIP_UNKNOWN_MAP_KEYS would make sense (it is slightly redundant as only Maps have keys, but it seems reasonable to me wrt existing feature naming).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found