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

feat: Update tool.py 支持所有 JSON 形式和普通文字 #648

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bluelovers
Copy link

增强后的正则表达式以支持所有 JSON 形式和普通文字
Enhanced regular expression to support all JSON formats and plain text

https://onecompiler.com/python/42fcba277

增强后的正则表达式以支持所有 JSON 形式和普通文字
Enhanced regular expression to support all JSON formats and plain text

https://onecompiler.com/python/42fcba277
@zanllp
Copy link
Owner

zanllp commented Jun 5, 2024

我看到了好几个json.loads,解析成json的意义是什么?在服务端这一侧这些值后续都是当成字符串在处理的

想要支持的话还得处理下其他软件的数据。
image

re_param_code = r'\s*([\w ]+):\s*("(?:\\"[^,]|\\"|\\|[^\"])+"|[^,]*)(?:,|$)'
# 增强后的正则表达式以支持所有 JSON 形式和普通文字
# Enhanced regular expression to support all JSON formats and plain text
re_param_code = r'\s*(\w[\w \-/]+):\s*({.*?}|\[.*?\]|"(?:\\.|[^\\"])*"|[^,]*)(?:,|$)'
Copy link
Owner

@zanllp zanllp Jun 5, 2024

Choose a reason for hiding this comment

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

这个正则式一般是和sd-webui的保持一致。IIB是为了要独立运行所以才自己搞了一个没有直接import
image

Copy link
Author

Choose a reason for hiding this comment

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

因為原版也有這個BUG 無法完整分析

Copy link
Owner

Choose a reason for hiding this comment

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

因為原版也有這個BUG 無法完整分析

能提供下图片源文件(会发生bug的那个)吗?

我这边有个文件夹专门收集这些边缘case

image

@bluelovers
Copy link
Author

image

減少一行代碼 但閱讀性差了一點
@bluelovers
Copy link
Author

bluelovers commented Jun 5, 2024

我看到了好几个json.loads,解析成json的意义是什么?在服务端这一侧这些值后续都是当成字符串在处理的

因為就算弄一個函數出來 也不會讓代碼變得更少
可以精簡一下 但代碼不會比較好讀

想要支持的话还得处理下其他软件的数据。 image

這看起來是用了非標準的json 因為他用的是 '

@bluelovers
Copy link
Author

bluelovers commented Jun 5, 2024

def unquote(text):
if len(text) == 0 or text[0] != '"' or text[-1] != '"':
return text
try:
return json.loads(text)
except Exception:
return text

原版本來就是把這些值用json處理的

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.

2 participants