src/lq-collections.c
/**
 *  \brief Scans a JSON formatted C-String (char array) for a property, once found a descriptive struct is populated with info to allow for property value consumption.
 * 
 *  \param [in] jsonSrc - Char array containing the JSON document.
 *  \param [in] propName - The name of the property you are searching for.
 * 
 *  \return Struct with a pointer to property value, a property type (enum) and the len of property value.
*/
\brief Scans a JSON formatted C-String (char array) for a property, once found a descriptive struct is populated with info to allow for property value consumption.

\param [in] jsonSrc - Char array containing the JSON document.
\param [in] propName - The name of the property you are searching for.

\return Struct with a pointer to property value, a property type (enum) and the len of property value.
\ brief Scans a JSON formatted C - String ( char array ) for a property once found a descriptive struct is populated with info to allow for property value consumption . \ param [ in ] jsonSrc - Char array containing the JSON document . \ param [ in ] propName - The name of the property you are searching for . \ return Struct with a pointer to property value a property type ( enum ) and the len of property value .
lqJsonPropValue_t lq_getJsonPropValue(const char *jsonSrc, const char *propName)
{
    lqJsonPropValue_t results = {0, 0, lqcJsonPropType_notFound};
    char *jsonEnd = (char*)jsonSrc + strlen(jsonSrc);
    char *next;
    char propSearch[40] = {0};
    uint8_t nameSz = strlen(propName);
    memcpy(propSearch + 1, propName, nameSz);
    propSearch[0] = '"';
    propSearch[nameSz + 1] = '"';
    char *nameAt = strstr(jsonSrc, propSearch);
    if (nameAt)
    {
        next = nameAt + nameSz;
        next = (char*)memchr(next, ':', jsonEnd - next);
        if (next)
        {
            next++;
            while (*next == '\040' || *next == '\011' )   
            {
                next++;
                if (next >= jsonEnd)
                    return results;
            }
            switch (*next)
            {
            case '{':
                results.type = lqcJsonPropType_object;
                results.value = next;
                results.len = findJsonBlockLength(next, jsonEnd, '{', '}');
                return results;
            case '[':
                results.type = lqcJsonPropType_array;
                results.value = next;
                results.len = findJsonBlockLength(next, jsonEnd, '[', ']');
                return results;
            case '"':
                results.type = lqcJsonPropType_text;
                results.value = (char*)++next;
                results.len = (char*)memchr(results.value, '\042', jsonEnd - next) - results.value;    
                return results;
            case 't':
                results.type = lqcJsonPropType_bool;
                results.value = (char*)next;
                results.len = 4;
                return results;
            case 'f':
                results.type = lqcJsonPropType_bool;
                results.value = (char*)next;
                results.len = 5;
                return results;
            case 'n':
                results.type = lqcJsonPropType_null;
                results.value = (char*)next;
                results.len = 4;
                return results;
            default:
                results.type = lqcJsonPropType_int;
                results.value = (char*)next;
                while (*next != ',' && *next != '}' && next < jsonEnd)   
                {
                    next++;
                    if (*next == '.') { results.type = lqcJsonPropType_float; }
                }
                results.len = next - results.value;
                return results;
            }
        }
    }
    return results;
}
lqJsonPropValue_t lq_getJsonPropValue ( const char * jsonSrc , const char * propName ) { lqJsonPropValue_t results = { 0 , 0 , lqcJsonPropType_notFound } ; char * jsonEnd = ( char * ) jsonSrc + strlen ( jsonSrc ) ; char * next ; char propSearch [ 40 ] = { 0 } ; uint8_t nameSz = strlen ( propName ) ; memcpy ( propSearch + 1 , propName , nameSz ) ; propSearch [ 0 ] = ' ' ; propSearch [ nameSz + 1 ] = ' ' ; char * nameAt = strstr ( jsonSrc , propSearch ) ; if ( nameAt ) { next = nameAt + nameSz ; next = ( char * ) memchr ( next , ' ' , jsonEnd - next ) ; if ( next ) { next ++ ; while ( * next == ' \040 ' || * next == ' \011 ' ) { next ++ ; if ( next >= jsonEnd ) return results ; } switch ( * next ) { case ' ' : results . type = lqcJsonPropType_object ; results . value = next ; results . len = findJsonBlockLength ( next , jsonEnd , ' ' , ' ' ) ; return results ; case ' ' : results . type = lqcJsonPropType_array ; results . value = next ; results . len = findJsonBlockLength ( next , jsonEnd , ' ' , ' ' ) ; return results ; case ' ' : results . type = lqcJsonPropType_text ; results . value = ( char * ) ++ next ; results . len = ( char * ) memchr ( results . value , ' \042 ' , jsonEnd - next ) - results . value ; return results ; case ' ' : results . type = lqcJsonPropType_bool ; results . value = ( char * ) next ; results . len = 4 ; return results ; case ' ' : results . type = lqcJsonPropType_bool ; results . value = ( char * ) next ; results . len = 5 ; return results ; case ' ' : results . type = lqcJsonPropType_null ; results . value = ( char * ) next ; results . len = 4 ; return results ; default : results . type = lqcJsonPropType_int ; results . value = ( char * ) next ; while ( * next != ' ' && * next != ' ' && next < jsonEnd ) { next ++ ; if ( * next == ' ' ) { results . type = lqcJsonPropType_float ; } } results . len = next - results . value ; return results ; } } } return results ; }
\brief Scans a JSON formatted C-String (char array) for a property, once found a descriptive struct is populated with info to allow for property value consumption.
\ brief Scans a JSON formatted C - String ( char array ) for a property once found a descriptive struct is populated with info to allow for property value consumption .
// skip space or tab // dblQuote = \042 // scan forward until beyond current property