如何获得提振JSON使用正确的数据类型 [英] how to get boost json to use the correct data types
问题描述
当我使用一个int put_value
,它被作为字符串写入。有谁知道如何得到它打印为int?
的#include<&iostream的GT;
#包括LT&;升压/ property_tree / ptree.hpp>
#包括LT&;升压/ property_tree / json_parser.hpp>使用boost :: property_tree :: ptree中;
使用命名空间std;INT主(INT ARGC,CHAR *的argv []){ ptree中节点;
node.put(字符串,这里的文字);
node.put(INT,1); //输出作为1和应为1
write_json(COUT,节点,FALSE); // {字符串:这里的文字,INT:1} 返回0;
}
图书馆前pressly不支持它。
提升物业库还没有被命名为提升JSON库,因为它不是一个JSON库。相反,它是一个属性树库(碰巧的使用的其目的JSON子集)。
从<一个href=\"http://www.boost.org/doc/libs/1_57_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.json_parser\"相对=nofollow>文档:
属性树数据集没有输入,不支持数组本身。因此,下面的JSON /属性树映射用于:
- JSON对象映射到节点。每个属性是一个子节点。
- JSON数组被映射到节点。每个元素都是具有空名称的子节点。如果一个节点都命名和无名子节点,它不能被映射到一个JSON重新presentation。
- JSON值被映射到包含该值的节点。 的但是,所有类型的信息丢失;号,以及文字空,真和假被简单地映射到它们的字符串的形式。的强>
同时包含子节点和数据- 属性树节点不能映射。
块引用>和
JSON往返,除了类型信息丢失。
块引用>When I
put_value
using an int, it gets written as a string. Does anyone know how to get it to print as an int?#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using boost::property_tree::ptree; using namespace std; int main(int argc, char* argv[]) { ptree node; node.put("string", "text here"); node.put("int", 1);//outputs as "1" and should be 1 write_json(cout, node, false);//{"string":"text here","int":"1"} return 0; }
解决方案The library expressly doesn't support it.
Boost Property Library has not been named "Boost Json Library" because it's not a JSON library. Instead, it's a Property Tree library (that happens to use JSON subsets for its purposes).
From the documentation:
The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used:
- JSON objects are mapped to nodes. Each property is a child node.
- JSON arrays are mapped to nodes. Each element is a child node with an empty name. If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation.
- JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
- Property tree nodes containing both child nodes and data cannot be mapped.
And
JSON round-trips, except for the type information loss.
这篇关于如何获得提振JSON使用正确的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!