Addons/convert/pjson

From J Wiki
Jump to navigation Jump to search

Overview

This is a json encoder/decoder and is read into the pjson locale. Load with:

   load 'convert/pjson'

The main functions are enc and dec:

   enc_pjson_ 1 2 3;'hello'
[[1,2,3],"hello"]

   dec_pjson_ enc_pjson_ 1 2 3;'hello'
┌─────┬─────┐
│1 2 3│hello│
└─────┴─────┘

Encode as object:

   enc_pjson_ ('name';'anne'),('city';'rome'),:'term';7
{"name":"anne",
"city":"rome",
"term":7
}

   dec_pjson_ enc_pjson_ ('name';'anne'),('city';'rome'),:'term';7
┌────┬────┐
│name│anne│
├────┼────┤
│city│rome│
├────┼────┤
│term│7   │
└────┴────┘

See the demo folder for examples.

Encoding

  • a character scalar or vector is encoded as a string
  • a character array of rank 2 or more is encoded as an array (of arrays) of strings
  • trailing spaces in strings can be automatically removed by fixchar_pjson_=: dtb f.
  • a numeric array of rank 1 or more is encoded as an array (of arrays) of numbers
  • a boxed scale or vector is encoded as an array
  • a 2-column boxed matrix of key, value pairs is encoded as an object
  • other boxed arrays of rank 2 or more are not supported
  • 0 1 are encoded to 0 1
  • _ __ and _. are encoded as null

Decoding

  • objects are decoded to 2-column matrices of key, value pairs
  • false true are decoded to 0 1
  • null is decoded to NULL_pjson_. This can be set by the user.