JSON Pretty Print – revisited

Last week I was surfing the internet (does anyone use this expression yet?) and I stumbled upon a project for minifying JSON in multiple languages, and LabVIEW wasn’t there yet.

The Problem

As for the most acquainted with JSON, you probably already know that comments are not cited in the RFC or ECMA Standards, and therefore you must consider that they are not allowed structures.

The reality is that they are used anyways, although your application may not support them, it is interesting that your parser may be able to ignore or remove them. That’s where this minifying function comes handy.

For those who are already asking: does the native LV Unflatten from JSON support comments?

The answer is no. It throws an error saying the JSON is invalid.

Open source contribution and challenge

So I took the challenge. The ideia was to build a function to minify a JSON removing all white spaces and comments and the algorithm must be very close to the original JS algorithm, and must pass the tests already predefined.

So, challenge accepted.

I opened my github account, forked the original project and created a branch for the LabVIEW language (following the Port instructions). Of course, I opened an issue before.

So far so good. After that, I started coding and I had the first problem, I wasn’t familiar with JS, so I started learning a bit about the functions and syntax, but still it was not a language that I developed something. So I took a look at the python and perl implementations which gave a little bit of more insight of what I should’ve done.

In the end, I could port all the code to LabVIEW, and passed all the required tests (Yey!!). I did a pull request and I am waiting to be accepted.

As result I learned a little bit more of JS and other languages used in this project.

The minify algorithm in LabVIEW is not optimized or simplified, so if you want to refactor and update it, I will be glad.

Pretty Print – Revisited

So, I remembered one of the first posts of the blog that I did a pretty printer for LabVIEW, that library also had a function for minifying, but it was simple as removing the spaces and tabs, without considering in strings and comments.

The idea so, was to replace that function with this new one, the old one I deprecated.

I took the opportunity to tweak a little bit the Pretty Print function, which it was not really complete, and included also two options as:

  • Option to change the identation
  • Option to switch on/off cutting trailing zeros from floating numbers.

After that I released a new version of the library.

Code Available

As always I made the code available.

There are some places where you can find it:

Last Words

So, if you want a very lightweight function to minify your JSON (great for streaming) or pretty print, this library is the way to go.

If you need more advanced handling of JSON functions, maybe you should consider other libraries such as JKI JSON or JSONtext.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s