Skip to content

Squirrelistic/sjpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Squirrelistic JSON Preprocessor

JSON files can be long and boring, overgrowth of lines, slowly hypnotising, with its repetitive cadence and sea of malevolent characters.
That's where DJ Squirrel shows up - to alleviate the JSON pain with it's flashy pre-processing formula:

Squirrelistic JSON Template (SJT) + Input Parameters (JSON) = Long JSON

This equation probably doesn't make sense, so let's delve into an obligatory Hello World example.

Template:

{
    "hello": "{{ .helloText }}"
}

Plus parameters:

{
    "helloText": "world"
}

Equals:

{
    "hello": "world"
}

Examples

Check out test-files directory
  in this Github repository
Can you see the examples factory?
  is it satisfactory?

Command line syntax

sjcp -t=template.sjt -p=params.json -o=result.json

Squirrelistic JSON Template features

  • Conditional logic (if, else if, else)
  • Includes, including includes including includes (iiii)
  • Loops
  • Comments
  • And much more...

Usage scenarios

  • Reusable JSON snippets
  • Splitting large JSON files into small logical units
  • More readable Azure ARM Templates
  • Interspecies communication with Alien Squirrels

Engine

Template engine piggybacks on Golang Text Template package so all its syntax is valid as long as it produces valid JSON file.

The following functions were added on top:

  • {{ include filename }} - includes another SJT template (SJT buddy), which in turn can include more buddies
  • {{ json .paramName }} - JSON representation of parameter (see the explanation bellow)
  • {{ jsonEscape .paramName }} - escapes JSON offending characters (double quote, newline etc.) lest it ends up in JSON prison.

The {{ .param }} vs {{ json .param }} vs {{ jsonEscape .param }} conundrum

Let's have this nifty parameters file where:

  • Param1 is string which contains single double quote, which is encoded as " otherwise JSON gods would be angry.
  • Param2 is an array. A nice one.
{
    "param1": "\"",
    "param2": [ "Squirrels", "are", "nice" ]
}

Here is the sexy table which will shed a light on the JSON encoding multiverse.

Template snippet Output Explanation
{{ .param1 }} " Not escaped, not enclosed in ""
{{ json .param1 }} "\"" Escaped, enclosed in ""
{{ jsonEscape .param1 }} \" Escaped, not enclosed in ""
{{ .param2 }} [Squirrels are nice] String representation of an array, not escaped, not enclosed in ""
{{ json .param2 }} ["Squirrels","are","nice"] JSON representation of an array, escaped and everything
{{ jsonEscape .param2 }} Kernel Panic! Invalid, as jsonEscape only accepts strings, not array or other objects

Contributions

All contributions, suggestions, bug reports and peanuts are welcome.
Bear in mind that I wrote this project in 2 days, after learning Golang from tutorial, so The Perfection is not the name of this game.

Links