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.

@JsonProperty on Enum not working as key in Map

See original GitHub issue

I’m using Jackson-Databind 2.8.1 and it seems like Enums that are keys in a Map aren’t respecting their @JsonProperty settings.

Here’s my Enum:

public enum EventType {
  @JsonProperty(value = "user.delete")
  UserDelete,

  @JsonProperty(value = "user.create")
  UserCreate,

  @JsonProperty(value = "user.update")
  UserUpdate,

  @JsonProperty(value = "user.deactivate")
  UserDeactivate,

  @JsonProperty(value = "user.bulk.create")
  UserBulkCreate,

  @JsonProperty(value = "user.reactivate")
  UserReactivate,

  @JsonProperty(value = "user.action")
  UserAction
}

Here’s my object:

    public static class EventConfiguration implements Buildable<EventConfiguration> {
      public Map<EventType, EventConfigurationData> events = new HashMap<>();

      public static class EventConfigurationData {
        public boolean enabled;

        public TransactionType transactionType;

        public EventConfigurationData() {
        }

        public EventConfigurationData(boolean enabled, TransactionType transactionType) {
          this.enabled = enabled;
          this.transactionType = transactionType;
        }
      }
    }

Here’s the ACTUAL and INCORRECT JSON:

{
  "eventConfiguration": {
    "events": {
      "UserDelete": {
        "enabled": true,
        "transactionType": "None"
      },
      "UserCreate": {
        "enabled": true,
        "transactionType": "None"
      },
      "UserUpdate": {
        "enabled": true,
        "transactionType": "None"
      },
      "UserDeactivate": {
        "enabled": true,
        "transactionType": "None"
      },
      "UserBulkCreate": {
        "enabled": true,
        "transactionType": "None"
      },
      "UserReactivate": {
        "enabled": true,
        "transactionType": "None"
      }
    }
  }
}

This is the JSON that comes from this call:

      try {
        body = objectMapper.writeValueAsBytes(request);
      } catch (IOException e) {
        throw new JSONException(e);
      }

This is the EXPECTED and CORRECT JSON that Jackson is NOT generating:

{
  "eventConfiguration": {
    "events": {
      "user.delete": {
        "enabled": true,
        "transactionType": "None"
      },
      "user.create": {
        "enabled": true,
        "transactionType": "None"
      },
      "user.update": {
        "enabled": true,
        "transactionType": "None"
      },
      "user.deactivate": {
        "enabled": true,
        "transactionType": "None"
      },
      "user.bulk.create": {
        "enabled": true,
        "transactionType": "None"
      },
      "user.reactivate": {
        "enabled": true,
        "transactionType": "None"
      }
    }
  }
}

As you can see, when the JSON is generated by Jackson, it is not respecting the @JsonProperty enum values but is instead using the EventType.name() method. Additionally, if I feed the JSON that Jackson generates right back into Jackson, it fails. It should never fail as Jackson should always produce JSON that it can consume back (at least with my code here - I can definitely create code where this isn’t true).

Then when I turn around and parse this JSON using this code it explodes:

objectMapper.reader(EventConfiguration.class).readValue(request.getInputStream());

I get this stack trace:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize Map key of type com.inversoft.passport.domain.event.EventType from String "UserDelete": not a valid representation, problem: Can not deserialize Map key of type com.inversoft.passport.domain.event.EventType from String "UserDelete": not one of values excepted for Enum class: [user.delete, user.create, user.update, user.reactivate, user.bulk.create, user.deactivate, user.action]
 at [Source: org.primeframework.mock.servlet.MockServletInputStream@5a634b91; line: 1, column: 507]
 at [Source: org.primeframework.mock.servlet.MockServletInputStream@5a634b91; line: 1, column: 507] (through reference chain: com.inversoft.passport.domain.api.SystemConfigurationRequest["systemConfiguration"]->com.inversoft.passport.domain.SystemConfiguration["eventConfiguration"]->com.inversoft.passport.domain.EventConfiguration["events"])

Am I missing a configuration option or something that would make this work?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fredgalvaocommented, Sep 13, 2019

I’ll create a new issue and paste the java version. As I said, this is not a kotlin issue, I was able to reproduce it on both sides. I just provided the kotlin test because it’s easier to read.

0reactions
cowtowncodercommented, Sep 13, 2019

Cool thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Map enum to json property - Stack Overflow
These values are like keys inside the json file. I want to map something like [JsonProperty(InstrumentConfig.LowDiskpace.ToString()] . For some ...
Read more >
@JsonProperty enum desirialization. - Google Groups
When i am using @JsonProperty annotation with enum values its working properly on serialization, but when desirialization happens mapper is using ordinal() ...
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 >
How to write custom converters for JSON serialization - .NET
For example, converters for the following types require the factory ... Enum.TryParse(propertyName, ignoreCase: true, out key)) { throw new ...
Read more >
Serializing and Deserializing Enumerations with Json.NET
It may be possible, that the enum values and the JSON string representation of the enum do not match. If that's the case,...
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