OSVR-Core
OSVR-Core copied to clipboard
Python version of osvr_json_to_c
I think we still want to use the C++ version by default, since it keeps dependencies low, but it makes cross-building easier (one step rather than two) to have an equivalent. It's not complex to write.
If it's blocking the Android merge, let's do it.
Hi ! I'm pretty competent with Python, looking to contribute, and that .cpp file doesn't look very complicated If this is still needed i'd be glad to write it. I would need an example of expected input and output, and that's about it.
Ello, If there's any menial Python tasks that need to be addressed I'm willing to help as well. I'm versed in C and C++ but I've used Python the most. So you need a python to c conversion or json to python?
Thanks for your interest! So this osvr_json_to_c
tool works a bit like xxd
if you've ever used that. Right now, using a locally-built tool makes cross-compiling (like for Android) harder, hence why a Python equivalent would be handy.
Here's what the tool does. You give it a JSON file, a variable name, and an output file. It reads in the JSON file (discarding comments - might need to use a more liberal json parser in Python since apparently the standard one is a bit pickier than jsoncpp), turns it back into a string in "compressed" format - that is, without unneeded whitespaces, etc. It then writes out a file that basically is a C/C++ source file defining a string array by converting every character to hex.
Sample input and output pair:
- in: https://github.com/OSVR/OSVR-Core/blob/master/src/osvr/Client/display-HDK.json
- out: https://gist.github.com/rpavlik/e392afc6770a9e046e4f
Still a welcome addition, but no longer blocking Android - the OSVR-Android-Build repo builds just osvr_json_to_c separately on the host.
Hi. This issue looks like it's a year old, but do still want help with this? If so, can you provide an updated link to an example input file? The link from the comment posted on April 1, 2015 (https://github.com/OSVR/OSVR-Core/blob/master/src/osvr/Client/display-HDK.json) seems to be no longer valid.
I'd like to take a shot a porting https://github.com/OSVR/OSVR-Core/blob/master/devtools/osvr_json_to_c.cpp to Python if you still need it done.
@malcolmathci Any JSON file would serve as input. You could use https://github.com/OSVR/OSVR-Core/blob/master/apps/displays/OSVR_HDK_2_0.json as an example.
It reads the JSON file and converts it to a const char[]
. For example, the following JSON file:
{
"hello": "world"
}
would get converted to something like this:
static const char my_json[] = {
0x7b,0x22,0x68,0x65,0x6c,0x6c,0x6f,0x22,0x3a,0x22,0x77,0x6f,0x72,0x6c,0x64,0x22,0x7d,0x0a};
It just converts each character of the JSON input into its ASCII value and dumps it into an array.
@godbyk Ah, okay. I got it. Thanks for clearing that up for me.
@godbyk @rpavlik Can I have some feedback on my solution? Is there anything that should be changed? The output of this script matches the "hello world" example, except the EndOfFile newline character is missing the leading 0. The script returns "0xa" instead of "0x0a", which may be the same for all single-digit hex values. I'd have to test some more to be sure. I'm not sure if that will be a problem on the C/C++ end.: https://github.com/malcolmathci/OSVR-Core/commit/912531da52ac52407d9a6dae1365cc84bbede93f
@godbyk @rpavlik I have a pull request at https://github.com/OSVR/OSVR-Core/pull/483, but I'd like to add a unit test for the following before you review it:
{ "hello": "world" }
@godbyk @rpavlik @VRguy I added the unit tests, so my solution is ready for review now. My branch fails the continuous integration check, and I'm looking into why. I welcome any feedback you have.