Skip to content

to json -r not removing whitespaces fix - #11948

Merged
sholderbach merged 8 commits into
nushell:mainfrom
dannou812:dannou812/json-whitespaces
Mar 20, 2024
Merged

sholderbach merged 8 commits into
nushell:mainfrom
dannou812:dannou812/json-whitespaces

Conversation

@dannou812

Copy link
Copy Markdown
Contributor

fixes #11900

Description

Replaced the iteration through each json-line applying str::trim with an iteration through each character. The iteration now checks whether a character is a whitespace (ignores whitespaces in double quotes) and filters accordingly.

If you have any performance concerns please let me know, because I could not think of any.

User-Facing Changes

The problem described in the issue now no longer persists.

Tests + Formatting

I corrected all Tests that were affected by this change.

Comment thread crates/nu-json/src/ser.rs Outdated
let mut inside_quotes = false;
let mut last_char = 'p'; // 'p' is just a placeholder, can be anything except for '\\'
let output = string
.chars()

@fdncred fdncred Feb 23, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if chars() could be an issue with some wide unicode characters. Would graphemes() be better here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my understanding it shouldn't be a problem, since the only character that is modified is the whitespace which is checked for with the .is_whitespace() function that also considers unicode characters.

@dannou812 dannou812 Feb 23, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you think graphemes() is better, I am happy to change it, but right now I have no time left.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The only whitespace that should be removed is outside of quotes/strings, which means that it is whitespace added by the encoder. This should only be ever be spaces, tabs, etc. which is covered by char::is_whitespace.
So, I think graphemes is unnecessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In fact, could you change it to is_ascii_whitespace instead? The encoder only emits whitespace characters covered by this. Thanks!

@IanManske

Copy link
Copy Markdown
Member

On another note, there is a subtle bug regarding escape sequence handling. E.g.,

{a: '\', b: 'some text'} | to json -r

gives

{"a":"\\",
  "b": "sometext"
}

@IanManske

Copy link
Copy Markdown
Member

Also, we recently added from json --raw which uses serde_json. So, I think it would be fine to use serde_json::to_string for to json -r if you think that would make it easier/simpler.

@sholderbach

Copy link
Copy Markdown
Member

Reupping this point:

Also, we recently added from json --raw which uses serde_json. So, I think it would be fine to use serde_json::to_string for to json -r if you think that would make it easier/simpler.

While great that you figured out a way to strip more of the whitespace back out, this all feels like a needless hack if we could have a code path that never emits the whitespace in the first place.

The question would be if serde_json and nu-json behave the same on Nushell-specific datatypes and their representation as a string. (anything not following the basic serde model?)

@IanManske

Copy link
Copy Markdown
Member

The question would be if serde_json and nu-json behave the same on Nushell-specific datatypes and their representation as a string.

to json first converts Nushell Values to nu_json::Value (a simple JSON data model), so this common type can be serialized using serde_json or nu-json. Both serde_json and nu-json should return valid JSON from this, and the only differences should be in whitespace. Although, there could also be differences in string escaping and serializing float values.

@dannou812

Copy link
Copy Markdown
Contributor Author

Hi, could you tell me what I should do so this PR can be merged? Should I just use the serde_json functions to convert?

@IanManske

Copy link
Copy Markdown
Member

Should I just use the serde_json functions to convert?

Sorry for the confusion @dannou812 , yeah I think the serde_json method would be best, since it should be simpler and more performant. serde_json::to_string is the function you want, and this will return a different error type compared to the nu-json functions. But you can just ignore/discard all these errors (e.g., with .ok()), since we return a generic error later anyways.

@fdncred

fdncred commented Mar 13, 2024

Copy link
Copy Markdown
Contributor

What's the status on this PR?

@dannou812

Copy link
Copy Markdown
Contributor Author

I'm sorry I didn't have much time the last two weeks. I will get to it in the next 3 days. Thanks for the patience.

@dannou812

Copy link
Copy Markdown
Contributor Author

I wrapped the call to serde_json::to_string() but there actually are tests failing now which seems to be caused by unicode characters. The characters themselves are correct but the notation is different (e.g. u001b instead of u{1b}). Can I just change the tests to the new notation or do you think this could cause more extensive problems with backwards compatibility?

@sholderbach

sholderbach commented Mar 20, 2024 •

Copy link
Copy Markdown
Member

\u001b seems to be the conventional form so this is good with me.

We just need to remark on the breaking change

@sholderbach sholderbach added the notes:breaking-changes Noted in Breaking Changes section label Mar 20, 2024

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your work and patience on this!

@sholderbach
sholderbach merged commit 8237d15 into nushell:main Mar 20, 2024
@hustcer hustcer added this to the v0.92.0 milestone Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"to json --raw" does not “remove all whitespace” as documented

5 participants