Skip to content

Commit 2111417

Browse files
committed
fix: serialize extra param with base64
1 parent ca67553 commit 2111417

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import json
22
import argparse
3-
4-
JSON_DOUBLE_QUOTE_REPLACE = '<!>'
5-
JSON_SINGLE_QUOTE_REPLACE = '<*>'
3+
import base64
64

75

86
def load_extra_params(string):
9-
string = string.replace(JSON_DOUBLE_QUOTE_REPLACE,'"').replace(JSON_SINGLE_QUOTE_REPLACE,"'")
107
try:
11-
return json.loads(string)
12-
except json.JSONDecodeError:
8+
# Decode the base64 string back to JSON string
9+
json_bytes = base64.b64decode(string.encode('utf-8'))
10+
json_str = json_bytes.decode('utf-8')
11+
return json.loads(json_str)
12+
except (base64.binascii.Error, UnicodeDecodeError, json.JSONDecodeError):
1313
raise argparse.ArgumentTypeError(f"Invalid dictionary format: {string}")
1414

15-
def dump_extra_params(d:dict):
16-
return json.dumps(d).replace("'", JSON_SINGLE_QUOTE_REPLACE).replace('"', JSON_DOUBLE_QUOTE_REPLACE)
15+
16+
def dump_extra_params(d: dict):
17+
# Convert dict to JSON string, then encode to base64
18+
json_str = json.dumps(d)
19+
json_bytes = json_str.encode('utf-8')
20+
base64_bytes = base64.b64encode(json_bytes)
21+
return base64_bytes.decode('utf-8')

0 commit comments

Comments
 (0)