PFL and JSON

Friday, 30 October 2026 15:07 UTC

The base PFL object class - PFL_Obj, provides a virtual function to_str(). This is overridden in sub-classes to provide a string representation of the object instance. (i.e each class can have its own implementation).

A convenience shortcut function, pfl_to_str(obj) is provided, which will return the string representation of any object. pfl_print() makes direct use of this.

Supported Types

The classes PFL_Null, PFL_Bool, PFL_Numeric, PFL_Str, PFL_List and PFL_Map (and their respective subclasses) will all return valid JSON strings, assuming all the objects conform to these types.

  • NB. PFL_Numeric and PFL_List are abstract super-classes.

    • PFL_Numeric includes concrete implementations such as PFL_Int32, PFL_Float and PFL_Dec (decimal).

    • PFL_List includes concrete implementations such as PFL_Array and PFL_Link_List.

A JSON document can be produced simply by printing or calling pfl_to_str() on an object.

Parsing JSON

Parsing a JSON document works similarly in reverse. PFL_Str has a related function pfl_str_i_parse_json() which accepts a JSON formatted string and if valid, builds and returns the appropriate object structure.

Example


int 
main (int argc, char * argv[]) 
{
    pfl_env_init();


    // Create a JSON string from objects
    PFL_Str * json;
    
    PFL_Map * map = pfl_map_new(16);
    
    pfl_scope(map)
    {
        pfl_map_i_set(map, "item_1", "Foo");
        
        pfl_map_i_set(map, "item_2", 123.45);
        
        PFL_Array * arr = pfl_map_i_set(map, "item_3", pfl_array_new(3));
        
        pfl_array_i_append(arr, "one");
        
        pfl_array_i_append(arr, 42);
        
        json = pfl_to_str(map);
    }
    pfl_scope_end;


    // Parse the JSON string to create a new set of objects
    PFL_Obj * obj;
    
    pfl_scope(json)
    {
        // prints: {"item_1":"Foo", "item_2":123.450000, "item_3":["one", 42]}
        // ...the hash-map item order may vary.
        pfl_str_i_println(json);
        
        obj = pfl_str_i_parse_json(json);
    }
    pfl_scope_end;        
        
    
    // if obj is NULL, it was not valid JSON    
    assert(pfl_is_not_null(obj));
    
    pfl_scope(obj)
    {
        // the root object is a hash_map
        assert(pfl_obj_is_a(obj, PFL_MAP_TYPE));
    
        // item_1 is a PFL_Str containing "Foo"
        void * item_1 = pfl_map_i_get(obj, "item_1");
        
        assert(pfl_obj_i_is_a(item_1, PFL_STR_TYPE));
        
        assert(pfl_obj_i_equals(item_1, "Foo"));
        
        // prints: "Foo"
        pfl_println(item_1); 
        
        
        // item_2 is a PFL_Dec (decimal) containing 123.45
        // - all JSON numerics are converted to decimal
        void * item_2 = pfl_map_i_get(obj, "item_2");
        
        assert(pfl_obj_i_is_a(item_2, PFL_DEC_TYPE));
        
        assert(pfl_obj_i_equals(item_2, 123.45));      
        
        // prints: 123.45
        pfl_println(item_2); 
        
    
        // item_3 is a PFL_Array
        void * item_3 = pfl_map_i_get(obj, "item_3");
        
        assert(pfl_obj_i_is_a(item_3, PFL_ARRAY_TYPE));
        
        void * index_0 = pfl_array_i_get_item(item_3, 0);
        
        assert(pfl_obj_i_equals(index_0, "one"));
        
        void * index_1 = pfl_array_i_get_item(item_3, 1);
        
        assert(pfl_obj_i_equals(index_1, 42));
        
        // prints: ["one", 42]
        pfl_println(item_3);          
    }
    pfl_scope_end;


    pfl_end();    
}