Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
config:json [2012/09/28 10:24] – add a keyword bidouconfig:json [2015/01/31 17:07] – Add instructions to pretty print JSON enrico
Line 1: Line 1:
 ====== JSON Filetype ====== ====== JSON Filetype ======
 +JSON files are intended for serializing data objects, but are sometimes used as configuration files.  JSON syntax is mostly a subset of JavaScript syntax and therefore the JavaScript lexer can be used for it.
  
-Although JSON files aren't terribly common, sometimes you'll have to hand-edit some JSON data, for example when it's used as a configuration file format. +Add this to the ''filetype_extensions.conf'' file (''...'' means existing data):
- +
-Add this to the ''filetypes_extensions.conf'' file (''...'' means existing data):+
 <code ini> <code ini>
 [Extensions] [Extensions]
Line 17: Line 16:
  
 And create a new filedef named ''filetypes.JSON.conf'' with the following contents: And create a new filedef named ''filetypes.JSON.conf'' with the following contents:
-<code ini>+<file properties filetypes.JSON.conf>
 [styling=C] [styling=C]
  
Line 24: Line 23:
  
 [settings] [settings]
-extension=json 
 lexer_filetype=Javascript lexer_filetype=Javascript
-comment_single=# +extension=json 
-comment_use_indent=true+mime_type=application/json
  
 [indentation] [indentation]
-width=2+#width=4
 # 0 is spaces, 1 is tabs, 2 is tab & spaces # 0 is spaces, 1 is tabs, 2 is tab & spaces
-type=1 +#type=1 
-</code>+</file> 
 + 
 +=== Build Commands === 
 + 
 +For people using Unix, the following will be helpful for checking your JSON files for correctness. 
 + 
 +<file bash /usr/local/bin/check_json> 
 +#!/bin/sh 
 + 
 +if which python >/dev/null ; then 
 +  python -m json.tool <"$1" 2>&1 >/dev/null \ 
 +  | sed 's/^\([^:]*\): line \(.*\)/'"$1"':\2: \1/' 
 +elif which perl >/dev/null ; then 
 +  perl -MJSON -e 'local $/;decode_json(<STDIN>);' <"$1" 2>&1 >/dev/null \ 
 +  |sed 's/^\(.*\) at .e line 1, .STDIN. [^ ]* \([0-9]*\)/'"$1"':\2: \1/;' 
 +else 
 +  echo "No known JSON parsers found" >&
 +  exit 1 
 +fi 
 +</file> 
 + 
 +In Geany, go to the Build menu and 'Set Build Commands...' and use ''/usr/local/bin/check_json %f'' as the compile command, and ''([^:]+):([0-9]+)'' as the Error Regular Expression. 
 + 
 +=== Pretty Printing === 
 + 
 +Assuming you have Python installed, pretty printing (aka formatting) of JSON is very easy with Geany: 
 + 
 +  * Python is required (works with Python 2 and 3) 
 +  * Configure Geany: Edit->Format->Send Selection to->Set Custom Commands 
 +  * In the dialog add a new command and simply use: //python -mjson.tool// 
 + 
 +To use this, select some string in Geany and use Edit->Format->Send Selection to-><your configured command> and Geany will replace the string by the converted version. 
 +Alternatively, you can use the shortcut Ctrl-<number> in case your command is one of the first three configured commands. 
 +For details, read the [[http://www.geany.org/manual/#sending-text-through-custom-commands|documentation about Send Selection to commands]]. 
  
 {{tag>configure json filetype}} {{tag>configure json filetype}}
 +~~NOTOC~~
 +
Print/export