Example #1
0
std::string CborValue::inspect() const
{
    if( isNull() )
    {
        static std::string null = "(null)";
        return null;
    }
    else if( isUndefined() )
    {
        static std::string undefined = "(undefined)";
        return undefined;
    }
    else if( isBool() )
    {
        return boost::lexical_cast<std::string>(toBool());
    }
    else if( isPositiveInteger() )
    {
        return boost::lexical_cast<std::string>(toPositiveInteger());
    }
    else if( isNegativeInteger() )
    {
        std::string result = "-";
        const char specialValue[] = "18446744073709551616"; // 0x10000000000000000
        uint64_t value = toNegativeInteger();

        if( value == 0 )
            result += specialValue;
        else
            result += boost::lexical_cast<std::string>(value);

        return result;
    }
    else if( isDouble() )
    {
        return boost::lexical_cast<std::string>(toDouble());
    }
    else if( isString() )
    {
        return toString();
    }
    else if( isByteString() )
    {
        std::vector<char> byteString = toByteString();
        std::string result = "(0x";

        static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                     'A', 'B', 'C', 'D', 'E', 'F'};

        for(size_t i = 0; i < byteString.size(); ++i)
        {
            unsigned char c = static_cast<unsigned char>(byteString[i]);

            result += hex[c / sizeof(hex)];
            result += hex[c % sizeof(hex)];
        }

        result += ')';
        return result;
    }
    else if( isArray() )
    {
        std::vector<CborValue> values = toArray();
        std::string result = "[";

        if( values.empty() == false )
        {
            for(size_t i = 0; i < values.size(); ++i)
            {
                result += values[i].inspect();
                result += ", ";
            }

            result.resize(result.size() - 1);
            result[result.size() - 1] = ']';
        }
        else
        {
            result += ']';
        }

        return result;
    }
    else if( isMap() )
    {
        std::map<CborValue, CborValue> values = toMap();
        std::string result = "{";

        if( values.empty() == false )
        {
            std::map<CborValue, CborValue>::iterator it = values.begin();
            std::map<CborValue, CborValue>::iterator end = values.end();

            for(; it != end; ++it)
            {
                result += it->first.inspect();
                result += ": ";
                result += it->second.inspect();
                result += ", ";
            }

            result.resize(result.size() - 1);
            result[result.size() - 1] = '}';
        }
        else
        {
            result += '}';
        }

        return result;
    }
    else if( isBigInteger() )
    {
        BigInteger bigInteger = toBigInteger();
        std::string result;

        if( bigInteger.positive )
            result = "(big integer: 0x";
        else
            result = "(negative big integer: 0x";

        static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                     'A', 'B', 'C', 'D', 'E', 'F'};

        if( bigInteger.bigint.empty() == false )
        {
            for(size_t i = 0; i < bigInteger.bigint.size(); ++i)
            {
                unsigned char c = static_cast<unsigned char >(bigInteger.bigint[i]);

                result += hex[c / sizeof(hex)];
                result += hex[c % sizeof(hex)];
            }

            result.resize(result.size() - 1);
            result[result.size() - 1] = ')';
        }
        else
        {
            result += ')';
        }

        return result;
    }

    assert(false);
    std::string invalidType = "(invalid type)";
    return invalidType;
}
Example #2
0
const CBORValue::BinaryValue& CBORValue::getByteString() const
{
    ASSERT(isByteString());
    return m_byteStringValue;
}