Formatron v0.5.0
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

str from_str_to_kbnf_str (str s)
 Convert a string to a kbnf string.
 
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|collections.abc.Sequence schema, str start_nonterminal)
 Generate a KBNF grammar string from a schema for JSON format.
 

Variables

bool strict_schema = True
 
str SPACE_NONTERMINAL = "[ \t\n]*"
 
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|collections.abc.Sequence 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 445 of file json.py.

◆ _register_all_predefined_types()

formatron.formats.json._register_all_predefined_types ( )
protected

Definition at line 83 of file json.py.

◆ from_str_to_kbnf_str()

str formatron.formats.json.from_str_to_kbnf_str ( str s)

Convert a string to a kbnf string.

Parameters
sThe string to convert.
Returns
The kbnf string.

Definition at line 54 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 75 of file json.py.

Variable Documentation

◆ _type_to_nonterminals

list formatron.formats.json._type_to_nonterminals
protected

Definition at line 61 of file json.py.

◆ GRAMMAR_HEADER

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

Definition at line 29 of file json.py.

◆ SPACE_NONTERMINAL

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

Definition at line 27 of file json.py.

◆ strict_schema

bool formatron.formats.json.strict_schema = True

Definition at line 25 of file json.py.