jstyleson-0.0.2/0000755000076600000240000000000012732250570013452 5ustar ljkstaff00000000000000jstyleson-0.0.2/jstyleson.egg-info/0000755000076600000240000000000012732250570017176 5ustar ljkstaff00000000000000jstyleson-0.0.2/jstyleson.egg-info/dependency_links.txt0000644000076600000240000000000112732250570023244 0ustar ljkstaff00000000000000 jstyleson-0.0.2/jstyleson.egg-info/PKG-INFO0000644000076600000240000000077012732250570020277 0ustar ljkstaff00000000000000Metadata-Version: 1.1 Name: jstyleson Version: 0.0.2 Summary: Library to parse JSON with js-style comments. Home-page: https://github.com/linjackson78/jstyleson Author: linjackson Author-email: linjackson78@gmail.com License: MIT Description: UNKNOWN Keywords: json,comment,javascript,parse Platform: UNKNOWN Classifier: Development Status :: 3 - Alpha Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.5 jstyleson-0.0.2/jstyleson.egg-info/SOURCES.txt0000644000076600000240000000024312732250570021061 0ustar ljkstaff00000000000000jstyleson.py setup.cfg setup.py jstyleson.egg-info/PKG-INFO jstyleson.egg-info/SOURCES.txt jstyleson.egg-info/dependency_links.txt jstyleson.egg-info/top_level.txtjstyleson-0.0.2/jstyleson.egg-info/top_level.txt0000644000076600000240000000001212732250570021721 0ustar ljkstaff00000000000000jstyleson jstyleson-0.0.2/jstyleson.py0000644000076600000240000000747512732246037016076 0ustar ljkstaff00000000000000import json def dispose(json_str): """Clear all comments in json_str. Clear JS-style comments like // and /**/ in json_str. Accept a str or unicode as input. Args: json_str: A json string of str or unicode to clean up comment Returns: str: The str without comments (or unicode if you pass in unicode) """ result_str = list(json_str) escaped = False normal = True sl_comment = False ml_comment = False quoted = False a_step_from_comment = False a_step_from_comment_away = False # The depth of array or object we are in array_stack = 0 object_stack = 0 former_index = None for index, char in enumerate(json_str): if escaped: # We have just met a '\' escaped = False continue if a_step_from_comment: # We have just met a '/' if char != '/' and char != '*': a_step_from_comment = False normal = True continue if char == '"': if normal and not escaped: # We are now in a string quoted = True normal = False elif quoted and not escaped: # We are now out of a string quoted = False normal = True elif char == '\\': # '\' should not take effect in comment if normal or quoted: escaped = True elif char == '/': if a_step_from_comment: # Now we are in single line comment a_step_from_comment = False sl_comment = True normal = False former_index = index - 1 elif a_step_from_comment_away: # Now we are out of comment a_step_from_comment_away = False normal = True ml_comment = False for i in range(former_index, index + 1): result_str[i] = "" elif normal: # Now we are just one step away from comment a_step_from_comment = True normal = False elif char == '*': if a_step_from_comment: # We are now in multi-line comment a_step_from_comment = False ml_comment = True normal = False former_index = index - 1 elif ml_comment: a_step_from_comment_away = True elif char == '\n': if sl_comment: sl_comment = False normal = True for i in range(former_index, index + 1): result_str[i] = "" elif char == '[' and normal: array_stack += 1 elif char == ']' and normal: array_stack -= 1 _remove_last_comma(result_str, index) elif char == '{' and normal: object_stack += 1 elif char == '}' and normal: _remove_last_comma(result_str, index) object_stack -= 1 # Show respect to original input if we are in python2 return ("" if isinstance(json_str, str) else u"").join(result_str) # There may be performance suffer backtracking the last comma def _remove_last_comma(str_list, before_index): i = before_index - 1 while str_list[i].isspace() or not str_list[i]: i -= 1 # This is the first none space char before before_index if str_list[i] == ',': str_list[i] = '' # Below are just some wrapper function around the standard json module. def loads(text, **kwargs): return json.loads(dispose(text), **kwargs) def load(fp, **kwargs): return loads(fp.read(), **kwargs) def dumps(obj, **kwargs): return json.dumps(obj, **kwargs) def dump(obj, fp, **kwargs): json.dump(obj, fp, **kwargs) jstyleson-0.0.2/PKG-INFO0000644000076600000240000000077012732250570014553 0ustar ljkstaff00000000000000Metadata-Version: 1.1 Name: jstyleson Version: 0.0.2 Summary: Library to parse JSON with js-style comments. Home-page: https://github.com/linjackson78/jstyleson Author: linjackson Author-email: linjackson78@gmail.com License: MIT Description: UNKNOWN Keywords: json,comment,javascript,parse Platform: UNKNOWN Classifier: Development Status :: 3 - Alpha Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.5 jstyleson-0.0.2/setup.cfg0000644000076600000240000000014412732250570015272 0ustar ljkstaff00000000000000[metadata] description-file = README.md [egg_info] tag_svn_revision = 0 tag_date = 0 tag_build = jstyleson-0.0.2/setup.py0000644000076600000240000000112012732250516015156 0ustar ljkstaff00000000000000from setuptools import setup setup( name='jstyleson', py_modules=['jstyleson'], version='0.0.2', description='Library to parse JSON with js-style comments.', author='linjackson', author_email='linjackson78@gmail.com', url='https://github.com/linjackson78/jstyleson', keywords=['json', 'comment', 'javascript', 'parse'], classifiers=['Development Status :: 3 - Alpha', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.5'], license='MIT', test_suite="tests", )