jonquil_ser Module

JSON serializer implementation.

This module provides procedures and types for serializing TOML Fortran data structures to JSON format.

Main Interfaces

Configuration

The json_ser_config type allows customizing the serialization output:

  • indent - String used for indentation (e.g., " " for 2 spaces)
  • literal_nan - Write NaN as literal instead of string
  • literal_inf - Write Inf as literal instead of string
  • literal_datetime - Write datetime as literal instead of string

Example

use jonquil, only : json_object, json_dumps, json_ser_config, &
   & new_object, set_value
type(json_object) :: obj
type(json_ser_config) :: config
character(:), allocatable :: str

call new_object(obj)
call set_value(obj, "key", "value")

config%indent = "  "  ! Pretty print with 2-space indent
call json_dumps(obj, str, config=config)
print '(a)', str


Interfaces

public interface json_dump

  • private subroutine json_dump_to_file(val, filename, error, config)

    Write string representation of JSON value to a file

    Arguments

    Type IntentOptional Attributes Name
    class(toml_value), intent(inout) :: val

    TOML value to visit

    character(len=*), intent(in) :: filename

    File name to write to

    type(toml_error), intent(out), allocatable :: error

    Error handling

    type(json_ser_config), intent(in), optional :: config

    Configuration for serializer

  • private subroutine json_dump_to_unit(val, io, error, config)

    Write string representation of JSON value to a connected formatted unit

    Arguments

    Type IntentOptional Attributes Name
    class(toml_value), intent(inout) :: val

    TOML value to visit

    integer, intent(in) :: io

    Formatted unit to write to

    type(toml_error), intent(out), allocatable :: error

    Error handling

    type(json_ser_config), intent(in), optional :: config

    Configuration for serializer

public interface json_dumps

  • private subroutine json_dump_to_string(val, string, error, config)

    Create a string representing the JSON value

    Arguments

    Type IntentOptional Attributes Name
    class(toml_value), intent(inout) :: val

    TOML value to visit

    character(len=:), intent(out), allocatable :: string

    Formatted unit to write to

    type(toml_error), intent(out), allocatable :: error

    Error handling

    type(json_ser_config), intent(in), optional :: config

    Configuration for serializer


Derived Types

type, public ::  json_ser_config

Configuration for JSON serializer

Components

Type Visibility Attributes Name Initial
character(len=:), public, allocatable :: indent

Indentation

logical, public :: literal_datetime = .false.

Write literal datetime

logical, public :: literal_inf = .false.

Write literal Inf

logical, public :: literal_nan = .false.

Write literal NaN

type, public, extends(toml_visitor) ::  json_serializer

Serializer to produduce a JSON document from a TOML datastructure

Components

Type Visibility Attributes Name Initial
type(json_ser_config), public :: config = json_ser_config()

Configuration for serializer

integer, public :: depth = 0

Current depth in the tree

character(len=:), public, allocatable :: output

Output string

Type-Bound Procedures

procedure, public :: visit

Visit a TOML value


Functions

public function json_serialize(val, config) result(string)

Serialize a JSON value to a string and return it.

Read more…

Arguments

Type IntentOptional Attributes Name
class(toml_value), intent(inout) :: val

TOML value to visit

type(json_ser_config), intent(in), optional :: config

Configuration for serializer

Return Value character(len=:), allocatable

Serialized JSON value