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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using toString() on Serialization Fail. #4800

Closed
lucky8987 opened this issue Nov 13, 2024 · 9 comments
Closed

Using toString() on Serialization Fail. #4800

lucky8987 opened this issue Nov 13, 2024 · 9 comments

Comments

@lucky8987
Copy link

Is your feature request related to a problem? Please describe.

I encountered a problem, for example, when serializing: org.springframework.http.HttpMethod, it currently throws an exception because it does not provide a get method for the property, but provides toString().

Describe the solution you'd like

I hope to serialize objects that do not provide property methods, without throwing exceptions, but by calling the toString method to complete the processing. I was wondering if it's possible to add the FAIL_USING_TO_STRING attribute in: com. fasterxml. jakson. databind. serializationFeature to solve this problem?

Usage example

No response

Additional context

No response

@lucky8987 lucky8987 added the to-evaluate Issue that has been received but not yet evaluated label Nov 13, 2024
@pjfanning
Copy link
Member

pjfanning commented Nov 13, 2024

Jackson is highly configurable. You could register a custom serializer. The advantage of using existing features and existing configurability - you don't have to wait for code changes and a new release.

We can't just keep adding extra features just because users don't want to try the existing features.

@JooHyukKim
Copy link
Member

I hope to serialize objects that do not provide property methods, without throwing exceptions, but by calling the toString method to complete the processing.

Similarly, there is SerializationFeature.WRITE_ENUMS_USING_TO_STRING for enums, but it "eagerly" serializes Enums using toString() not like here being asked for

try normal serialization first then before failing, try toString

I think that's huge difference and seems way more complex.

So yeah, since we know for which object (HttpMethod) we want to achieve the behavior, custom serializer would be a great option.

@lucky8987
Copy link
Author

I hope to serialize objects that do not provide property methods, without throwing exceptions, but by calling the toString method to complete the processing.

Similarly, there is SerializationFeature.WRITE_ENUMS_USING_TO_STRING for enums, but it "eagerly" serializes Enums using toString() not like here being asked for

try normal serialization first then before failing, try toString

I think that's huge difference and seems way more complex.

So yeah, since we know for which object (HttpMethod) we want to achieve the behavior, custom serializer would be a great option.

Thank you for your suggestion. Indeed, custom serialization can effectively solve this problem.

@lucky8987
Copy link
Author

Jackson is highly configurable. You could register a custom serializer. The advantage of using existing features and existing configurability - you don't have to wait for code changes and a new release.

We can't just keep adding extra features just because users don't want to try the existing features.

OK,I will follow your suggestion and solve this problem through custom serialization.

@lucky8987 lucky8987 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2024
@cowtowncoder
Copy link
Member

In this case, why not use Mix-in Annotations to annotate toString() method with @JsonValue? That would serialize using return value of toString() method.

Having to use custom serializer should really be the last resort, fwtw.

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Nov 13, 2024
@yihtserns
Copy link
Contributor

LOL someone changed HttpMethod from Enum to Class.

@yihtserns
Copy link
Contributor

yihtserns commented Nov 13, 2024

In this case, why not use Mix-in Annotations to annotate toString() method with @JsonValue?

Had the same thought, was going to suggest:

public abstract class EnumLikeMixin {

    @JsonValue
    public abstract String name();
}

...
objectMapper.addMixIn(HttpMethod.class, EnumLikeMixin.class)

Also, maybe spring-projects/spring-framework#33870 can & should do that internally, seeing how they've already done similar thing before: https://github.com/spring-projects/spring-framework/blob/37b110a181baa2249c75d028dfea3de58a17a2f0/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java#L767 (assuming your usage is via Spring's HttpMessageConverter mecha).

@cowtowncoder
Copy link
Member

@yihtserns Good suggestion! name() is probably better method to annotated indeed.

@lucky8987
Copy link
Author

In this case, why not use Mix-in Annotations to annotate toString() method with @JsonValue?

Had the same thought, was going to suggest:

public abstract class EnumLikeMixin {

    @JsonValue
    public abstract String name();
}

...
objectMapper.addMixIn(HttpMethod.class, EnumLikeMixin.class)

Also, maybe spring-projects/spring-framework#33870 can & should do that internally, seeing how they've already done similar thing before: https://github.com/spring-projects/spring-framework/blob/37b110a181baa2249c75d028dfea3de58a17a2f0/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java#L767 (assuming your usage is via Spring's HttpMessageConverter mecha).

Wow, your plan looks very elegant and can solve this type of problem.👍

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

No branches or pull requests

5 participants