Home > OS >  JSON-ify string
JSON-ify string

Time:02-28

Are there any faster ways to JSON-ify a string? I first escaped manually, then tried json.dumps as suggested below, but can I somehow use repr() too?

import timeit, json

def escape_str(string):
    return string.replace("\\", "\\\\").replace('"', '\\"').replace("\b", "\\b").replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")

data = "hello\nworld"
print(data)
print('"'   escape_str(data)   '"', timeit.timeit('''\'"\'   escape_str(data)   \'"\'''', globals=globals(), number=1000000))
print(repr(data), timeit.timeit('''json.dumps(data)''', globals=globals(), number=1000000))
print(repr(data), timeit.timeit('''repr(data)''', globals=globals(), number=1000000))

#Output:
#hello
#world
#"hello\nworld" 0.6119729009999999
#'hello\nworld' 0.490987237
#'hello\nworld' 0.12092886899999988

CodePudding user response:

Just use json.dumps(). It's faster than fiddling with it yourself.

Trying to make repr() output the same, I added a method escape_repr():

import timeit
import json

def escape_str(string):
    return string.replace("\\", "\\\\").replace('"', '\\"').replace("\b", "\\b").replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")

def escape_repr(string):
    return string.replace("\"","\\\"").replace("'", "\"").replace("\\x08", "\\b").replace("\\x0c", "\\f")

data = "hello \n_\"_\\_\b_\f world"
print(data)
print(json.dumps(data), timeit.timeit('''json.dumps(data)''', globals=globals(), number=1000000))
print('"'   escape_str(data)   '"', timeit.timeit('''\'"\'   escape_str(data)   \'"\'''', globals=globals(), number=1000000))
print(escape_repr(repr(data)), timeit.timeit('''escape_repr(repr(data))''', globals=globals(), number=1000000))

Results:

"hello \n_\"_\\_\b_\f world" 0.5502998
"hello \n_\"_\\_\b_\f world" 0.9209655000000001
"hello \n_\"_\\_\b_\f world" 0.6323072999999999

So json.dumps() is not only the correct way of doing it, but also the fastest.

  • Related