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

Add new JsonWriteFeature.ESCAPE_FORWARD_SLASHES #1197

Merged
merged 7 commits into from
Jan 23, 2024

Conversation

JooHyukKim
Copy link
Member

resolves #507

@JooHyukKim
Copy link
Member Author

I figured we should do #1198 separately.

*
* @since 2.17
*/
protected int[] escapeForwardSlash(int[] escapes) {
Copy link
Member

Choose a reason for hiding this comment

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

Should not be added here since this is not JSON-specific type despite name. So rather add in JsonGeneratorImpl, shared base class for concrete ones

*
* @since 2.17
*/
public boolean isEnabled(JsonWriteFeature f) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's not add this here-- there is a reason it is missing (for legacy reasons). Basically, naming is bit confusing but JsonWriteFeature is strictly JSON-specific whereas JsonGenerator is not. So latter should not refer to former.

But we can add this new method instead in JsonGeneratorImpl base class, which is JSON-specific.
(alternative would be to do what code WriterBasedJsonGenerator currently does and instead use existing isEnabled(JsonGenerator.Feature), but that requires use of deprecated JsonGenerator.Feature constants).

* @since 2.17
*/
protected int[] escapeForwardSlash(int[] escapes) {
int[] esc = Arrays.copyOf(escapes, escapes.length);
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be optimized a bit by checking if any change is actually needed -- and only making defensive copy in that case.

However, this leads to my main concern here: copying of the array for every generator instance adds overhead, which can be non-trivial in case of writing small documents.
So I wonder if there's any way to avoid this overhead by pre-creating escaping table in CharTypes.

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

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

I think this is a good starting point: I can make changes I think are needed wrt placement of methods. The remaining question then is about avoiding overhead of copying the _outputEscapes encoding array for every generator.

@cowtowncoder
Copy link
Member

Ok, I changed things I wanted to change.

Looking at how escape-table is created, I don't think it is easy to try to make it happen by JsonFactory so let's forget about that wrt 2.x.

But it might still make sense to add an overload for CharTypes.get7BitOutputEscapes() (one that takes second argument, if forward slash is to be escaped), and be able to use eagerly constructed table unless (except if non-standard quote char is specified).

@cowtowncoder
Copy link
Member

@JooHyukKim Ok, I rewrote this a bit and I think it might be ready for merging. Thank you for providing the base & let me know if you think some changes are needed. Otherwise (or after changing) if you can convert it from draft to regular PR I can go ahead and merge it.

Thank you again!

@JooHyukKim JooHyukKim marked this pull request as ready for review January 22, 2024 04:32
@JooHyukKim
Copy link
Member Author

JooHyukKim commented Jan 22, 2024

@cowtowncoder seems good now 👍🏼
I just did a small minor test code deduplication via b348b5a.

@cowtowncoder
Copy link
Member

Thank you @JooHyukKim. I'll go ahead and merge this then.

@cowtowncoder cowtowncoder merged commit 25b9353 into FasterXML:2.17 Jan 23, 2024
5 checks passed
@cowtowncoder
Copy link
Member

Follow-up wrt 3.0: #1200

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.

Add JsonWriteFeature.ESCAPE_FORWARD_SLASHES to allow escaping of '/' for String values
2 participants