1

the python program http://sourceforge.net/projects/cppheaderparser/ can parse a c++ header file and store the info (about classes etc) in a python dictionary.

Using the included example program readSampleClass.py and

data_string = ( repr(cppHeader) )
with open('data.txt', 'w') as outfile:
    json.dumps(data_string,outfile)

it saved the output but it is not valid json as it uses single, not double quotes and key part is not quoted.

sample of output: (reduced)

{'enums': [], 'variables': [], 'classes': 
    {'SampleClass': 
        {'inherits': [], 'line_number': 8, 'declaration_method': 'class', 'typedefs': 
            {'public': [], 'private': [], 'protected': []
            }, 'abstract': False, 'parent': None,'parent': None, 'reference': 0, 'constant': 0, 'aliases': [], 'raw_type': 'void', 'typedef': None, 'mutable': False
        }], 'virtual': False, 'rtnType': 'int', 'returns_class': False, 'name': 'anotherFreeFunction', 'constructor': False, 'inline': False, 'returns_pointer': 0, 'defined': False
     }]
}

so the question is:

How can I make it use double quotes and not single and how can I also make it quote the value part. Like False in sample.

I assume is possible as the creator of cppheaderparser wrote about json.dumps(repr(cppHeader)) https://twitter.com/senexcanis/status/559444754166198272

Why use the json lib if its not valid jason?
That said I have never used python before and it might just not work as i think.

-update-

After some json doc reading, i gave up on json.dump as it seems to do nothing to the output in this case. I ended up doing

data_string = ( repr(cppHeader) )
data_string = string.replace(data_string,'\'', '\"')
data_string = string.replace(data_string,'False', '\"False\"')
data_string = string.replace(data_string,'True', '\"True\"')
data_string = string.replace(data_string,'None', '\"None\"')
data_string = string.replace(data_string,'...', '')
with open('data.txt', 'w') as outfile:
    outfile.write (data_string)

which give valid json - at least for my test c++ headers.

-update 2- The creator of cppheaderparse just released a new 2.6 version where its possible to write CppHeaderParser.CppHeader("yourHeader.h").toJSON() to save as json.

MrJ
  • 46
  • 1
  • 5
  • Huh? json does use single quotes, at least for whatever tests I ran – hd1 Jun 06 '15 at 23:38
  • Reading the specs at http://www.json.org/ and http://stackoverflow.com/questions/14355655/jquery-parsejson-single-quote-vs-double-quote suggest it does NOT. – MrJ Jun 08 '15 at 06:54
  • The [JSON](https://pypi.python.org/pypi/simplejson/) encoder does the right thing, is what I meant, for whatever tests and validators I put the output through. – hd1 Jun 08 '15 at 12:49
  • Oh, using the output of cppheaderparser ? – MrJ Jun 08 '15 at 20:29
  • Just use update #2.. .toJSON is bound to do the right thing. – hd1 Jun 08 '15 at 21:09

0 Answers0