Skip to content

Use toString for enum serialization#2220

Merged
adinauer merged 4 commits intomainfrom
fix/enum-serialization
Aug 17, 2022
Merged

Use toString for enum serialization#2220
adinauer merged 4 commits intomainfrom
fix/enum-serialization

Conversation

@adinauer
Copy link
Member

@adinauer adinauer commented Aug 16, 2022

📜 Description

Use toString() for enum entries being serialized.

💡 Motivation and Context

JsonReflectionObjectSerializer would consume lots of memory when serializing an enum entry where the enum has more than just a few entries.

Fixes #2219

💚 How did you test it?

Unit Test

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

🔮 Next steps

@adinauer adinauer requested a review from romtsn as a code owner August 16, 2022 08:53
@adinauer adinauer requested a review from marandaneto August 16, 2022 09:03
@codecov-commenter
Copy link

codecov-commenter commented Aug 16, 2022

Codecov Report

Merging #2220 (a869430) into main (bf18c0d) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##               main    #2220      +/-   ##
============================================
+ Coverage     80.61%   80.64%   +0.02%     
- Complexity     3355     3357       +2     
============================================
  Files           240      240              
  Lines         12327    12331       +4     
  Branches       1634     1636       +2     
============================================
+ Hits           9938     9944       +6     
+ Misses         1782     1781       -1     
+ Partials        607      606       -1     
Impacted Files Coverage Δ
.../src/main/java/io/sentry/JsonObjectSerializer.java 83.33% <100.00%> (+0.57%) ⬆️
...java/io/sentry/JsonReflectionObjectSerializer.java 91.04% <100.00%> (+0.27%) ⬆️
...n/java/io/sentry/transport/ReusableCountLatch.java 88.23% <0.00%> (+5.88%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@rsolci
Copy link

rsolci commented Aug 16, 2022

Hey @adinauer, really thanks for the quick response. But I am thinking if this solution wouldn't be better in the JsonObjectSerializer class since there is already a conditional checking the type of the object to serialize and only calls the reflection one as a fallback for unknown types.

@adinauer
Copy link
Member Author

better in the JsonObjectSerializer class since there is already a conditional checking the type of the object to serialize and only calls the reflection one as a fallback for unknown types.

As an optimmization it could be added to both. But it should be in JsonReflectionObjectSerializer to also cover fields of complex objects being serialized.

} else if (object instanceof Map) {
serializeMap(writer, logger, (Map<?, ?>) object);
} else if (object.getClass().isEnum()) {
writer.value(object.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHat's about object..name()? Does it return the same thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to use reflection to invoke .name(). By default .toString() should be the same as .name(). It also allows overriding the value that is serialized.

@adinauer adinauer merged commit c450270 into main Aug 17, 2022
@adinauer adinauer deleted the fix/enum-serialization branch August 17, 2022 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding an Enum in Breadcrumb setData causes a weird behaviour in serialization leading to a huge heap space allocation

4 participants