Link Search Menu Expand Document

json

Provides APIs for encoding and decoding JSON data.

JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of structured data.

This implementation complies with RFC 8259.

JSON to Blade value mapping

JSON Blade
Null Nil
String String
Number Number
Boolean Boolean
Array List
Object Dict

Blade to JSON object mapping

Blade JSON
nil Null
Integer Number
Number Number
Char String
String String
List Array
Dict Object
Instance of class implementing to_json() decorator Any

Example,

%> import json
%> json.encode([1, 2, 3])
'[1,2,3]'
%> 
%> json.encode({name: 'Blade', version: '0.0.7'})
'{"name":"Blade","version":"0.0.7"}'
%> 
%> json.encode({name: 'Blade', version: '0.0.7'}, false)
'{
  "name": "Blade", 
  "version": "0.0.7"
}'

Functions


json.encode(value: any [, compact: boolean = true [, max_depth: number = 1024]])
JSON encodes the given value with a recursive depth up to max_depth.

If compact is false, the resulting json string will be tightly packed. i.e. spaces will be trimmed from objects and arrays. Otherwise, the JSON output will be pretty formatted.

param max_depth is the maximum recursive depth for encoding, default = 1024.
  • pretty formatting use 2 spaces instead of tabs.
return string
json.decode(value: string [, allow_comments: boolean = true])
decodes the input JSON string into Blade objects
param value is the string to decode
param allow_comments can be set to enable/disable C-style comments in json [default = true]
return object
json.parse(path: string)
parses a file containing json data.
return object

Classes


class Encoder


Blade to JSON encoding class

class Encoder methods


Encoder([compact: boolean = false, [max_depth: number = 1024]])
constructor
  • that depth starts from zero
  • set max_depth to 0 to disable max depth
encode(value: any)
main encode method

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.