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

Serialization + deserialization cycle gives differerent results with multiline comments (whitespace) #35

Closed
Barteks2x opened this issue Feb 1, 2020 · 4 comments

Comments

@Barteks2x
Copy link

Given json object constructed like this:

JsonObject obj = new JsonObject();
JsonPrimitive p = new JsonPrimitive(42);
obj.put("thing", p, "this is a multiline\ncomment");

After converting it to string:

String json = (obj.toJson(JsonGrammar.builder().withComments(true).build()));
System.out.println(json);

I get the following output:

{
	/* this is a multiline
	   comment
	*/
	"thing": 42
}

Now parsing it back and printing again:

System.out.println(Jankson.builder().build().load(json).toJson(JsonGrammar.builder().withComments(true).build()));

I get the following:

{
	/* this is a multiline
	   	   comment
	*/
	"thing": 42
}

The whitespace keeps accumulating each time.

@falkreon
Copy link
Owner

falkreon commented Feb 2, 2020

This is likely to be a problem in the "read" part of the cycle, not trimming off clearly-not-part-of-the-comment leading whitespace on the second line. Should be possible to clean that up.

@Barteks2x
Copy link
Author

This is a minor thing, but is it intended for it to actually strip leading whitespace from comments that are supposed to have it? This can break intentional alignment using spaces or tabs.

@falkreon
Copy link
Owner

falkreon commented Feb 2, 2020

I thought about trying to preserve some whitespace, but ultimately I don't see how that can be practically done without passing judgement on how much whitespace the leading indent must be - I could possibly assume a space for the first line and a tab plus three spaces (or 7 spaces) for the second line, but that's a lot more complicated and still runs into a problem where it "normalizes" the whitespace on the first run, changing the user's comments in some way.

If I can't avoid normalizing some whitespace, I'd rather apply simpler heuristics that are easier to maintain and easier to explain.

@Barteks2x
Copy link
Author

Barteks2x commented Feb 3, 2020

Could there be an option to just create multiple single line comments in that case? (might require first fixing #36)

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

2 participants