cpw/compactsolars — pull request #6 by agaricusb (2013-01-11 06:23:20+00:00)
Title: Fix invalid JSON in mcmod.info

According to http://www.json.org/ - strings can contain "any Unicode character except quote or \ or control character", but mcmod.info contains control characters (newlines) in the strings, violating the specification. This causes some JSON parsers to choke on CompactSolar's mcmod.info, including Python's json library:

>>> import json;json.loads(file("mcmod.info").read())
Traceback (most recent call last):
File "", line 1, in
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 217, in JSONArray
value, end = iterscan(s, idx=end, context=context).next()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 183, in JSONObject
value, end = iterscan(s, idx=end, context=context).next()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 155, in JSONString
return scanstring(match.string, match.end(), encoding, strict)
ValueError: Invalid control character at: line 5 column 128 (char 193)

This PR replaces the literal newlines with \n, allowing the JSON to be parsed successfully:

>>> import json; json.loads(file("mcmod.info").read())
[{u'useDependencyInformation': u'true', u'name': u'Compact Solar Arrays', u'parent': u'', u'url': u'http://forum.industrial-craft.net/index.php?page=Thread&threadID=4827', u'logoFile': u'', u'updateUrl': u'', u'credits': u'By cpw, based on an original idea by Zippinus', u'version': u'@VERSION@', u'modid': u'CompactSolars', u'authors': [u'cpw'], u'requiredMods': [u'IC2@[1.106,)', u'Forge@[4.1,)', u'FML@[3.1.15,)'], u'mcversion': u'1.3.2', u'screenshots': [], u'dependencies': [u'IC2@[1.106,)'], u'description': u'Compact Solar Arrays, an Industrial Craft 2 mod that adds in three new Solar Panel type blocks: Solar Arrays.\nSolar Arrays condense 8 (Low Voltage), 64 (Medium Voltage) or 512 (High Voltage) Industrial Craft 2 solar panels into a single block.\nThis significantly reduces the CPU impact of large amounts of Solar Energy gathering so is a common server enhancement.'}]