
Research
/Security News
60 Malicious Ruby Gems Used in Targeted Credential Theft Campaign
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Write JSON-serializable python objects to file as JSON array.
Raises a SerializationError if you send it an object that cannot be serialized by whatever json module you are using..
Tries to use python-cjson (python2.x only) or UltraJSON for serializing objects, because you wouldn't be needing a silly thing like this if you weren't handling big old objects.
Objects successfully sent to the writer are always contained in an array. JSONAutoArray will always attempt to close the array upon file closure, regardless of any exceptions which may lead to a premature end of your process.
Suppose you are making with the ETL, and are pulling objects from a janky stream. Should your stream close prematurely, or send you some sort of madness, JSONAutoArray will throw out the bad object, and close the array before closing the file.
Install using pip:
pip install JSONAutoArray
import random
import logging
from tempfile import TemporaryDirectory
from json_autoarray import JSONAutoArray
from pathlib import Path
objects = [
{"this": "that"},
["the", "other"],
{"hippie": "joe"},
{"facist": {"bullyboy": ["me", "you", "them"]}},
list(set(["a", "a", "b"])),
list(range(3)),
]
# open with pathlib.Path
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "foo.json")) as writer:
for obj in objects:
writer.write(obj)
# open with string
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(os.path.join(tempdir, "foo.json")) as writer:
for obj in objects:
writer.write(obj)
# will fail in python3 if file opened as binary!
logging.warning("Expect TypeError for 'wb'")
with TemporaryDirectory() as tempdir:
f = open(Path(tempdir, "bar.json"), "wb")
try:
with JSONAutoArray.ArrayWriter(f) as writer:
for obj in objects:
writer.write(obj)
except TypeError as error:
logging.warning(error)
# use encoding
with TemporaryDirectory() as tempdir:
f = open(Path(tempdir, "bar.json", encoding="utf-8"), "w")
try:
with JSONAutoArray.ArrayWriter(f) as writer:
for obj in objects:
writer.write(obj)
except TypeError as error:
logging.warning(error)
logging.warning("Expect Serialization Error for lambda object.")
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "foo.json")) as writer:
try:
writer.write(lambda x: "foo")
except JSONAutoArray.SerializationError as error:
logging.warning(error)
# write ten thousand random flabberdabs
with TemporaryDirectory() as tempdir:
with JSONAutoArray.ArrayWriter(Path(tempdir, "flabberdabs.json")) as writer:
try:
for i in range(10000):
writer.write([{"flabberdab": random.randint(1, 1000)}])
except JSONAutoArray.SerializationError as error:
logging.warning(error)
# close array on StopIteration error
def rando_gen():
for i in range(100):
yield {
"".join(
[chr(random.randint(1, 100)) for i in range(5)]
): random.randint(1, 100)
}
logging.warning("Expect uncaught StopIteration")
with TemporaryDirectory() as tempdir:
quux = Path(tempdir, "quux.json")
with JSONAutoArray.ArrayWriter(quux) as writer:
try:
rg = rando_gen()
while True:
if sys.version_info[0] < 3:
writer.write(rg.next())
else:
writer.write(next(rg))
except JSONAutoArray.SerializationError as s_error:
logging.warning(s_error)
except StopIteration as i_error:
logging.warning("%s - %s", i_error, "Hello StopIteration")
FAQs
Write array to self-closing JSON file
We found that JSONAutoArray demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.