Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use toString for enum serialization #2220

Merged
merged 4 commits into from Aug 17, 2022
Merged

Use toString for enum serialization #2220

merged 4 commits into from Aug 17, 2022

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
@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.

@@ -46,6 +46,8 @@ public void serialize(
serializeCollection(writer, logger, Arrays.asList((Object[]) object));
} 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
4 participants