Skip to content

fail to dump json if Parameter contains a bytes object (e.g. a string encoded to bytes) #41

@srio

Description

@srio

json_tricks does not support bytes objects. It may be useful in some cases, thus this is a potential problem.

from libpyvinyl.Parameters.Collections import CalculatorParameters


p = CalculatorParameters()
p.new_parameter("str1")
p["str1"] = 'customized string1'
print(p)
# this is OK
p.to_json("test_bytes1.json")


# now the string is encoded to a bytes object
p = CalculatorParameters()
p.new_parameter("str2")
p["str2"] = 'customized string2'.encode('UTF-8') # or just  p["str2"] = b'customized string'
print(p)
# this fails
p.to_json("test_bytes2.json")

also a numpy bytes fails:

# now numpy bytes object
import numpy
p = CalculatorParameters()
p.new_parameter("test3")
p["test3"] = numpy.array([1,2,3]).tobytes()
print(p)
# this fails
p.to_json("test_bytes3.json")

I see that json_tricks.np.dump() and json_tricks.nonp.dump() have a keywork conv_str_byte=False. Activating this may solve the problem for strings, but may not work for generic bytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions