The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SPVM::JSON - JSON

Description

The JSON class of SPVM has methods to manipulate JSON.

Usage

  use JSON;
  
  # new
  my $json = JSON->new;
  
  # decode
  my $spvm_data = $json->decode($json_data);
  
  # encode
  my $json_data = $json->encode($spvm_data);

Class Methods

new

  static method new : JSON ();

Creates a new JSON object.

Instance Methods

encode

  method encode : string ($spvm_data : object);

Converts the SPVM data $spvm_data to a JSON data.

A SPVM undef is converted to a JSON null.

A Bool object with the value field of 1 is converted to JSON true.

A Bool object with the value field of 0 is converted to a JSON false.

A SPVM string is converted to a JSON string. / in a SPVM string is escaped to \/ in a JSON string.

A Byte object is converted to a JSON number.

A Short object is converted to a JSON number.

A Int object is converted to a JSON number.

A Long object is converted to a JSON number.

A Float object is converted to a JSON number.

A Double object is converted to a JSON number.

A List object is converted to a JSON array.

A Hash object is converted to a JSON object. The keys are sorted by dictionaly order asc.

Exceptions:

The $spvm_data cannot contain a NaN float value. If so, an exception is thrown.

The $spvm_data cannot contain an inifinity float value. If so, an exception is thrown.

The $spvm_data cannot contain a NaN double value. If so, an exception is thrown.

The $spvm_data cannot contain an inifinity double value. If so, an exception is thrown.

If the $spvm_data contains a value of an invalid type, an exception is thrown.

decode

  method decode : object ($json_data : string);

Converts the JSON data $json_data to a SPVM data.

A JSON null is converted to a SPVM undef.

A JSON true is converted to a Bool object with the value field of 1.

A JSON false is converted to a Bool object with the value field of 0.

A JSON string is converted to a SPVM string.

A JSON number is converted to a Double object. Accuracy may be reduced.

A JSON array is converted to a List object.

A JSON object is converted to a Hash object.

Exceptions:

If the decoding of the $json_data failed, an exception is thrwon with one of the following messages adding the line number and the column number.

Expected character: "%s". (%s is a string)

Expected token: "%s". (%s is a string)

Invalid string.

Invalid number.

Repository

SPVM::JSON - Github

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License