src/libtsduck/base/xml/tsxmlJSONConverter.cpp
//----------------------------------------------------------------------------
// Convert an XML tree of elements.
//----------------------------------------------------------------------------
Convert an XML tree of elements.
Convert an XML tree of elements .
ts::json::ValuePtr ts::xml::JSONConverter::convertElement(const Element* model, const Element* source) const
{
    json::ValuePtr jobj(new json::Object());
    CheckNonNull(jobj.pointer());
    jobj->add(u"#name", source->name());
    std::map<UString,UString> attributes;
    source->getAttributes(attributes);
    for (auto it = attributes.begin(); it != attributes.end(); ++it) {
        json::ValuePtr jvalue;
        int64_t intValue = 0;
        bool boolValue = false;
        UString description;
        bool intModel = false;
        bool boolModel = false;
        if (model != nullptr) {
            model->getAttribute(description, it->first, false);
            description.trim(true, false, false);
            intModel = description.startWith(u"uint", CASE_INSENSITIVE) || description.startWith(u"int", CASE_INSENSITIVE);
            boolModel = description.startWith(u"bool", CASE_INSENSITIVE);
        }
        if (intModel) {
            if (it->second.toInteger(intValue, UString::DEFAULT_THOUSANDS_SEPARATOR)) {
                jvalue = new json::Number(intValue);
            }
            else {
                source->report().warning(u"attribute '%s' in <%s> line %d is '%s' but should be an integer", {it->first, source->name(), source->lineNumber(), it->second});
            }
        }
        else if (boolModel) {
            if (it->second.toBool(boolValue)) {
                jvalue = json::Bool(boolValue);
            }
            else {
                source->report().warning(u"attribute '%s' in <%s> line %d is '%s' but should be a boolean", {it->first, source->name(), source->lineNumber(), it->second});
            }
        }
        if (jvalue.isNull() && _args.enforce_integer && !intModel && it->second.toInteger(intValue, UString::DEFAULT_THOUSANDS_SEPARATOR)) {
            jvalue = new json::Number(intValue);
        }
        if (jvalue.isNull() && _args.enforce_boolean && !boolModel && it->second.toBool(boolValue)) {
            jvalue = json::Bool(boolValue);
        }
        if (jvalue.isNull()) {
            jvalue = new json::String(it->second);
        }
        jobj->add(it->first, jvalue);
    }
    if (source->hasChildren()) {
        jobj->add(u"#nodes", convertChildren(model, source));
    }
    return jobj;
}
ts :: json :: ValuePtr ts :: xml :: JSONConverter :: convertElement ( const Element * model , const Element * source ) const { json :: ValuePtr jobj ( new json :: Object ( ) ) ; CheckNonNull ( jobj . pointer ( ) ) ; jobj -> add ( u" " , source -> name ( ) ) ; std :: map < UString , UString > attributes ; source -> getAttributes ( attributes ) ; for ( auto it = attributes . begin ( ) ; it != attributes . end ( ) ; ++ it ) { json :: ValuePtr jvalue ; int64_t intValue = 0 ; bool boolValue = false ; UString description ; bool intModel = false ; bool boolModel = false ; if ( model != nullptr ) { model -> getAttribute ( description , it -> first , false ) ; description . trim ( true , false , false ) ; intModel = description . startWith ( u" " , CASE_INSENSITIVE ) || description . startWith ( u" " , CASE_INSENSITIVE ) ; boolModel = description . startWith ( u" " , CASE_INSENSITIVE ) ; } if ( intModel ) { if ( it -> second . toInteger ( intValue , UString :: DEFAULT_THOUSANDS_SEPARATOR ) ) { jvalue = new json :: Number ( intValue ) ; } else { source -> report ( ) . warning ( u" " , { it -> first , source -> name ( ) , source -> lineNumber ( ) , it -> second } ) ; } } else if ( boolModel ) { if ( it -> second . toBool ( boolValue ) ) { jvalue = json :: Bool ( boolValue ) ; } else { source -> report ( ) . warning ( u" " , { it -> first , source -> name ( ) , source -> lineNumber ( ) , it -> second } ) ; } } if ( jvalue . isNull ( ) && _args . enforce_integer && ! intModel && it -> second . toInteger ( intValue , UString :: DEFAULT_THOUSANDS_SEPARATOR ) ) { jvalue = new json :: Number ( intValue ) ; } if ( jvalue . isNull ( ) && _args . enforce_boolean && ! boolModel && it -> second . toBool ( boolValue ) ) { jvalue = json :: Bool ( boolValue ) ; } if ( jvalue . isNull ( ) ) { jvalue = new json :: String ( it -> second ) ; } jobj -> add ( it -> first , jvalue ) ; } if ( source -> hasChildren ( ) ) { jobj -> add ( u" " , convertChildren ( model , source ) ) ; } return jobj ; }
Convert an XML tree of elements.
Convert an XML tree of elements .
// Build the JSON object for the node. // Get all attributes of the XML element. // Add attributes in the JSON object. // JSON value of the attribute. // Get description of this attribute in the model. // Get description, empty string without error if not found. // Try to convert as an integer or boolean if defined as such by the model. // Should be an integer according to the model. // Should be a boolean according to the model. // Try to enforce integer of boolean value if specified on command line. // Use a string value by default. // Add the attribute in the JSON object. // Process the list of children, if any.