I've cloned a Python project I was working on on MacOS to a new Ubuntu (virtual) machine.
I've managed to get it to run, but the program crashes at the following line:
ujson.dumps(plist_as_file) # crash
The error is:
TypeError: � is not JSON serializable
I have no idea which character that is, nor where it's found. The plist_as_file
is a mac *.plist file, opened with this line:
with open(plist_path, 'rb') as plist_as_file:
It might be that git messed something up, but since both MacOS and Ubuntu are Unix based, I don't really see how.
Any ideas?
I don't think that code will work on both MacOS or on Ubuntu, because Apple's macOS and iOS .plist files are not JSON. They follow more of an XML format, and they even say this in the docs :
The file itself is typically encoded using the Unicode UTF-8 encoding and the contents are structured using XML.
Running your code on a Mac or on Ubuntu:
import ujson
with open("Info.plist", 'r') as plist_as_file:
ujson.dumps(plist_as_file)
will result in:
Traceback (most recent call last):
File "test.py", line 4, in <module>
ujson.dumps(plist_as_file)
TypeError: <_io.BufferedReader name='Info.plist'> is not JSON serializable
If, for some reason, you can successfully open the .plist and you don't get that error, then what you have isn't an actual .plist file. The error is same whether the file open
mode is r
or rb
.
You said you got:
TypeError: is not JSON serializable
and I think that's the same error, but for some reason, it's not printing out correctly. So, ujson
is really not the appropriate tool to use here, and it's not a problem with Git.
Python offers a built-in module for reading/writing .plist files: plistlib .
It has the same dump
/ dumps
and load
/ loads
methods as the json
(or the ujson
) module.
import plistlib
with open("Info.plist", 'rb') as plist_as_file:
plist_data = plistlib.load(plist_as_file)
# The entire contents is stored as a dict
print(plist_data)
# Access specific content as a dict
print(plist_data["CFBundleShortVersionString"])
print(plist_data["UIMainStoryboardFile"])
It turns out that ujson version on MacOS was 1.35, while the one on Linux was 2.0.1. The module was changed for whatever reason and the version 2.0.1 no longer supports serialization of that type.
However, if I write:
ujson.dumps(plist_as_file.readlines())
it works. Since I only need it as an unique identifier, I can use this instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.