gitea-comment icon indicating copy to clipboard operation
gitea-comment copied to clipboard

Data from file is not escaped for json

Open aqos156 opened this issue 3 years ago • 2 comments

As the title says, data from the file are not formatted for json. Any double quotes or backslashes will break the json formatting. I have to do this with sed manually for each logfile.

sed 's/\\\\/\\\\\\\\/g' file.log | sed 's/"/\\\\"/g' > file-out.log

The formatting of sed backslashes is weird because of drone.yml also escapes backslashes.

Complete working escaping step:

- name: Escape json
  image: alpine
  commands:
    - sed 's/\\\\/\\\\\\\\/g' output.log | sed 's/"/\\\\"/g' > output-out.log

I would suggest to use a kind of json escaping library, I do not know if my solution of replacing just \ with \\ and " with \" has any edge cases.

aqos156 avatar Oct 27 '21 07:10 aqos156

@DefinitelyADev would System.Web.HttpUtility.JavaScriptStringEncode be a good solution? It's a one line change that would be very helpful.

aqos156 avatar Dec 20 '22 19:12 aqos156

I'll take a look at it, sorry I have not been very active. I am preparing v2 that will solve those issues and be more input agnostic. This version of the plugin was made as a necessity for a specific use case.

DefinitelyADev avatar Dec 28 '22 08:12 DefinitelyADev