profile
viewpoint

Ask questionsAdd set(distinct elements) in rapidjson

I want to create following Json object

    {"name":"abc",
     "property1"=[2010, 2013, 2015],
     "property2"=["str1, "str2", str3"],
      "property3"=[true, false]
    }

property1, property2, property3 is basically an array but with distinct elements. Currently I am creating through:

document.AddMember("property2", Value(kArrayType), allocator);
document.AddMember("property3", Value(kArrayType), allocator);```
Then I iterate over the list and push the value in the object through:

```for (auto& iter: object_info_list) {
    document["property1"].PushBack(iter->property1, allocator); //int value
    document["property2"].PushBack(iter->property2, allocator); // string value
    document["property3"].PushBack(iter->property3, allocator); // bool value
}```
But the above statement pushes all the values (duplicates) as well. Is there a way to add distinct values only (like set in c++)

Thank you in advance
Tencent/rapidjson

Answer questions miloyip

I think you may use std::unique() on your data structures, with std::sort() it is O(n log n). RapidJSON actually just use a vector-like data strcture internally, which cannot handle this purpose efficient (O(n^2)).

useful!

Related questions

是否支持流式解析以及多个json分离解析 hot 1
是否支持流式解析以及多个json分离解析 hot 1
RapidJSON causes cc1plus: internal compiler error: Segmentation fault - rapidjson hot 1
GenericMemberIterator::Iterator implicitly declared private when RAPIDJSON_NOMEMBERITERATORCLASS is defined hot 1
Always gets a core dump while creating json string (rapidjson) hot 1
Bazel Support hot 1
Syntax typo in tutorial hot 1
是否支持流式解析以及多个json分离解析 hot 1
作者你好,我 想请教一下,这个框架是否能够将变量中文stirng 转成wchar_t? 在文档只看到定量 hot 1
travis on windows hot 1
Added headers to my project and got thousands of errors hot 1
Added headers to my project and got thousands of errors hot 1
想要的反斜杠 \. 无法表示,否则JSON解析出错 hot 1
Assertion `stack_.GetSize() == sizeof(ValueType)' failed hot 1
source:https://uonfu.com/
Github User Rank List