Skip to content

lysdexia/python-json-stream-writer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSONStreamWriter

Stream JSON-serializable python objects as JSON array in file.

Initialize with file object or filename.

Raises a SerializationError if you send it an object json or cjson cannot handle.

Tries to use cjson for serializing objects, because you wouldn't be needing a silly thing like this if you weren't handling big old objects.

Usage:

import JSONStreamWriter.JSONStreamWriter as JSONStreamWriter 
f = "foo.json" # or file-like object
with JSONStreamWriter.ArrayWriter(f) as jstream:
	jstream.write({"this": "that"})
	jstream.write({"the": "other"})
	jstream.write({"hippie": "joe"})
	jstream.write({"facist":{"bullyboy":["me", "you", "them"]}})

... yields:

[
	{
		"this": "that"
	},
	[
		"the",
		"other"
	],
	{
		"hippie": "joe"
	},
	{
		"facist": {
			"bullyboy": [
				"me",
				"you",
				"them"
			]
		}
	},
	[
		"a",
		"b"
	],
	[
		0,
		1,
		2
	]
]