Formatron v0.4.2
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.formats.json Namespace Reference

The module defines the JsonExtractor class, which is used to extract data from a string in JSON format. More...

Classes

class  JsonExtractor
 An extractor that loads json data to an object from a string. More...
 

Functions

None register_generate_nonterminal_def (typing.Callable[[typing.Type, str], typing.Optional[typing.Tuple[str, typing.List[typing.Tuple[typing.Type, str]]]]] generate_nonterminal_def)
 Register a callable to generate nonterminal definition from a type.
 
 _register_all_predefined_types ()
 
str _generate_kbnf_grammar (schemas.schema.Schema schema, str start_nonterminal)
 Generate a KBNF grammar string from a schema for JSON format.
 

Variables

str SPACE_NONTERMINAL = "[ \t\n\r]*"
 
str GRAMMAR_HEADER
 
list _type_to_nonterminals
 

Detailed Description

The module defines the JsonExtractor class, which is used to extract data from a string in JSON format.

Function Documentation

◆ _generate_kbnf_grammar()

str formatron.formats.json._generate_kbnf_grammar ( schemas.schema.Schema schema,
str start_nonterminal )
protected

Generate a KBNF grammar string from a schema for JSON format.

Parameters
schemaThe schema to generate a grammar for.
start_nonterminalThe start nonterminal of the grammar. Default is "start".
Returns
The generated KBNF grammar string.

Definition at line 215 of file json.py.

◆ _register_all_predefined_types()

formatron.formats.json._register_all_predefined_types ( )
protected

Definition at line 57 of file json.py.

◆ register_generate_nonterminal_def()

None formatron.formats.json.register_generate_nonterminal_def ( typing.Callable[ [typing.Type, str], typing.Optional[typing.Tuple[str, typing.List[typing.Tuple[typing.Type, str]]]]] generate_nonterminal_def)

Register a callable to generate nonterminal definition from a type.

The callable returns (nonterminal_definition, [(sub_type, sub_nonterminal), ...]) if the type is supported by this callable, otherwise None. [(sub_type, sub_nonterminal), ...] are the types and nonterminals used in nonterminal_definition that may need to be generated in the grammar too.

Parameters
generate_nonterminal_defA callable to generate nonterminal definition from a type.

Definition at line 49 of file json.py.

Variable Documentation

◆ _type_to_nonterminals

list formatron.formats.json._type_to_nonterminals
protected

Definition at line 36 of file json.py.

◆ GRAMMAR_HEADER

str formatron.formats.json.GRAMMAR_HEADER
Initial value:
1= rf"""integer ::= #"-?(0|[1-9]\\d*)";
2number ::= #"-?(0|[1-9]\\d*)(\\.\\d+)?([eE][+-]?\\d+)?";
3string ::= #'"([^\\\\"\u0000-\u001f]|\\\\["\\\\bfnrt/]|\\\\u[0-9A-Fa-f]{{4}})*"';
4boolean ::= "true"|"false";
5null ::= "null";
6array ::= array_begin (json_value (comma json_value)*)? array_end;
7object ::= object_begin (string colon json_value (comma string colon json_value)*)? object_end;
8json_value ::= number|string|boolean|null|array|object;
9comma ::= #"{SPACE_NONTERMINAL},{SPACE_NONTERMINAL}";
10colon ::= #"{SPACE_NONTERMINAL}:{SPACE_NONTERMINAL}";
11object_begin ::= #"\\{{{SPACE_NONTERMINAL}";
12object_end ::= #"{SPACE_NONTERMINAL}\\}}";
13array_begin ::= #"\\[{SPACE_NONTERMINAL}";
14array_end ::= #"{SPACE_NONTERMINAL}\\]";
15"""

Definition at line 18 of file json.py.

◆ SPACE_NONTERMINAL

str formatron.formats.json.SPACE_NONTERMINAL = "[ \t\n\r]*"

Definition at line 16 of file json.py.