This is an engineering-notes blog. After publishing the first entry, I was alerted that these are sent to subscribers around the world. Apologies if this isn't your cup of tea, but I'm hoping to share another side of our project with the world! And I heard blog readers love pictures.
Fixing JSON element names
A few months ago, I decided to migrate to a JSON format for data returned from my devices from the old XML standard. I was restructuring the web framework and realized that JSON data is much easier to parse and use in the browser than XML. It's also just easier to read.
What I didn't realize was valid JSON names shouldn't include dashes/hyphens, so I happily named a bunch of multi-word variables variable-one, variable-two, etc. Don't worry, I use more descriptive names than those! Everything was fine until I tried to run some fancy javascript on one of these variables, and the dash caused it to barf. I knew the time would come; I'd have to change all of those variable names, replacing dashes with underscores. Not only did this mean I had to change the names that were written out in command responses, but also in the HTML forms that displayed the different variables because I had javascript that automatically populated the proper fields with the right data when the response is received in the browser.
Today that time came. Manually editing every variable isn't an option for me. There's a possibility it could be faster, but I'd end up insane. Plus, I'm a programmer, there's a smarter way to do this!
Introducing sed
Sed is a little command line tool for editing a stream of text. One of the popular uses is to replace text in a file. I had to hammer a little rust off my regex skills, but I finally came up with a command to replace "-" with "_".
for file in `find . -name "*.c"`; do sed -e "s/([a-z0-9])-([a-z0-9])/1_2/g" -e "s/([a-z0-9])%d/1_%d/g" -e "s/([a-z])([0-9])/1_2/g" $file > temp; mv temp $file; done
There are actually three replace commands embedded in there, but it did the trick. And then some. There were a handful of things I didn't prepare for, and had to go back and revert some of the edits. Sed was also useful for those fixes, as well as svn diff and grep. Lots of command line fun today!
Testing the changes
Overall, the replacement went pretty well. It took about twice as long to build each project, install the new versions, and test each one. There were a couple hiccups, but more use of svn diff and svn revert proved useful again.
Lesson learned: Define string representation of variable names in one place.