<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:59:40 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CXX-587] to_json() throws exception on empty array</title>
                <link>https://jira.mongodb.org/browse/CXX-587</link>
                <project id="11980" key="CXX">C++ Driver</project>
                    <description>&lt;p&gt;we have a document that includes a collection with an array that is sometimes empty.  If we access the database and iterate through the documents, the ones with empty arrays throw an exception when we use to_json().&lt;/p&gt;

&lt;p&gt;example document: &lt;br/&gt;
{&lt;br/&gt;
    &quot;_id&quot;: &lt;/p&gt;
{
        &quot;$oid&quot;: &quot;553ab2f6d7e9e61537efb919&quot;
    }
&lt;p&gt;,&lt;br/&gt;
    &quot;uid&quot;: &quot;CA1236300671005&quot;,&lt;br/&gt;
    &quot;deviceType&quot;: &quot;GX440&quot;,&lt;br/&gt;
    &quot;location&quot;: &quot;&quot;,&lt;br/&gt;
    &quot;deviceData&quot;: &lt;/p&gt;
{
        &quot;ssid&quot;: &quot;&quot;,
        &quot;mdn&quot;: &quot;&quot;,
        &quot;imea&quot;: &quot;990000561959919&quot;,
        &quot;sim&quot;: &quot;89148000000382974468&quot;,
        &quot;firmwareVersion&quot;: &quot;4.3.5.010B&quot;,
        &quot;imsi&quot;: &quot;311480038944334&quot;,
        &quot;carrier&quot;: &quot;VERIZON&quot;,
        &quot;checkinInterval&quot;: 15
    }
&lt;p&gt;,&lt;br/&gt;
    &quot;checkinData&quot;: []&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;example code:&lt;br/&gt;
    mongocxx::instance instance{};&lt;br/&gt;
    std::string uri_string = &quot;mongodb://-------.feeneywireless.com:27017&quot;;&lt;br/&gt;
    mongocxx::client conn&lt;/p&gt;
{mongocxx::uri(uri_string)}
&lt;p&gt;;&lt;/p&gt;

&lt;p&gt;    std::string dbname = &quot;devices_db&quot;;&lt;br/&gt;
    auto db = conn&lt;span class=&quot;error&quot;&gt;&amp;#91;dbname&amp;#93;&lt;/span&gt;;&lt;/p&gt;

&lt;p&gt;    auto cursor = db&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;quot;device&amp;quot;&amp;#93;&lt;/span&gt;.find({});&lt;br/&gt;
    for(auto&amp;amp;&amp;amp; doc : cursor) {&lt;br/&gt;
        std::string teststring;&lt;br/&gt;
        try &lt;/p&gt;
{
            teststring = bsoncxx::to_json(doc);
        }
&lt;p&gt;        catch (exception e)&lt;/p&gt;
        {
            printf(&quot;error&quot;);
        }
&lt;p&gt;        std:cout &amp;lt;&amp;lt; teststring &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
    }&lt;/p&gt;

&lt;p&gt;Looking at bsoncxx-&amp;gt;json.cpp there is no check for an empty array.&lt;br/&gt;
    void visit_value(const types::b_array&amp;amp; value) &lt;/p&gt;
{
        out &amp;lt;&amp;lt; &quot;[&quot; &amp;lt;&amp;lt; std::endl;
        stack.push_back(true);
        visit_children(value.value);
        pad();
        out &amp;lt;&amp;lt; &quot;]&quot;;
    }

&lt;p&gt;I tried to add something along the lines of&lt;br/&gt;
    void visit_value(const types::b_array&amp;amp; value) {&lt;br/&gt;
        out &amp;lt;&amp;lt; &quot;[&quot; &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
        if(value.value.length != 0) &lt;/p&gt;
{
            stack.push_back(true);
            visit_children(value.value);
            pad();
        }
&lt;p&gt;        out &amp;lt;&amp;lt; &quot;]&quot;;&lt;br/&gt;
    }&lt;br/&gt;
but couldn&apos;t figure out how to check for the empty array.&lt;/p&gt;

&lt;p&gt;Thanks for your help.&lt;/p&gt;</description>
                <environment></environment>
        <key id="200873">CXX-587</key>
            <summary>to_json() throws exception on empty array</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="mira.carey@mongodb.com">Mira Carey</assignee>
                                    <reporter username="robert.hougen">Robert Hougen</reporter>
                        <labels>
                    </labels>
                <created>Tue, 28 Apr 2015 21:50:52 +0000</created>
                <updated>Fri, 15 May 2015 22:18:35 +0000</updated>
                            <resolved>Wed, 13 May 2015 20:35:28 +0000</resolved>
                                    <version>0.1.0</version>
                                    <fixVersion>0.2.0</fixVersion>
                                    <component>BSON</component>
                                        <votes>0</votes>
                                    <watches>4</watches>
                                                                                                                <comments>
                            <comment id="913308" author="xgen-internal-githook" created="Wed, 13 May 2015 20:35:26 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;username&apos;: u&apos;yhager&apos;, u&apos;name&apos;: u&apos;Yuval Hager&apos;, u&apos;email&apos;: u&apos;yhager@yhager.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CXX-587&quot; title=&quot;to_json() throws exception on empty array&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CXX-587&quot;&gt;&lt;del&gt;CXX-587&lt;/del&gt;&lt;/a&gt;: Added tests for empty documents and empty array conversion to string&lt;/p&gt;

&lt;p&gt;Closes #286&lt;/p&gt;

&lt;p&gt;Signed-off-by: Adam Midvidy &amp;lt;amidvidy@gmail.com&amp;gt;&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-cxx-driver/commit/b50b4cc63733fd467163ed83a3eb540592af8e86&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-cxx-driver/commit/b50b4cc63733fd467163ed83a3eb540592af8e86&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="913307" author="xgen-internal-githook" created="Wed, 13 May 2015 20:35:26 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;username&apos;: u&apos;yhager&apos;, u&apos;name&apos;: u&apos;Yuval Hager&apos;, u&apos;email&apos;: u&apos;yhager@yhager.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CXX-587&quot; title=&quot;to_json() throws exception on empty array&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CXX-587&quot;&gt;&lt;del&gt;CXX-587&lt;/del&gt;&lt;/a&gt;: validate an element before printing it as json string&lt;/p&gt;

&lt;p&gt;Signed-off-by: Adam Midvidy &amp;lt;amidvidy@gmail.com&amp;gt;&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-cxx-driver/commit/5fc10a9fa511afced424317ca4e10e77401a125d&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-cxx-driver/commit/5fc10a9fa511afced424317ca4e10e77401a125d&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="910767" author="acm" created="Tue, 12 May 2015 00:48:03 +0000"  >&lt;p&gt;&lt;a href=&quot;https://github.com/mongodb/mongo-cxx-driver/pull/286&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-cxx-driver/pull/286&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="910622" author="yhager" created="Mon, 11 May 2015 22:08:33 +0000"  >&lt;p&gt;Not the OP, but here&apos;s a similar PR to fix this: &lt;a href=&quot;https://github.com/mongodb/mongo-cxx-driver/pull/286&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-cxx-driver/pull/286&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="903071" author="acm" created="Fri, 1 May 2015 12:53:53 +0000"  >&lt;p&gt;Thanks for sending that. If you send that as a GitHub pull request we can merge it for you, or just let us know if you don&apos;t want to do that and we can fix on our side.&lt;/p&gt;</comment>
                            <comment id="902703" author="robert.hougen" created="Thu, 30 Apr 2015 22:21:33 +0000"  >&lt;p&gt;I think I found a fix which works for both empty arrays and empty documents. &lt;br/&gt;
 In bsoncxx::json.cpp the visit_children needs to check for the document it is passed to see if its empty.&lt;/p&gt;

&lt;p&gt;void visit_children(const bsoncxx::document::view&amp;amp; view) {&lt;br/&gt;
    bool first = true;&lt;br/&gt;
    for (auto&amp;amp;&amp;amp; x : view) {&lt;br/&gt;
//added code&lt;br/&gt;
    if(x.raw == 0 || x.length == 0)  &lt;/p&gt;
{
        break;
    }
&lt;p&gt;//end of added code&lt;br/&gt;
   if (!first) &lt;/p&gt;
{
        out &amp;lt;&amp;lt; &quot;, &quot; &amp;lt;&amp;lt; std::endl;
    }
&lt;p&gt;    first = false;&lt;br/&gt;
    visit_key(x.key());&lt;br/&gt;
    switch (static_cast&amp;lt;int&amp;gt;(x.type())) &lt;/p&gt;
{
    #define BSONCXX_ENUM(name, val)      \
        case val:                        \
            visit_value(x.get_##name()); \
            break;
      #include &amp;lt;bsoncxx/enums/type.hpp&amp;gt;
     #undef BSONCXX_ENUM
         }
&lt;p&gt;    }&lt;br/&gt;
    out &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
    stack.pop_back();&lt;br/&gt;
}&lt;/p&gt;
</comment>
                            <comment id="900311" author="acm" created="Tue, 28 Apr 2015 21:59:48 +0000"  >&lt;p&gt;Thanks for the bug report, we will take a look. We appreciate your testing out the alpha of the new driver!&lt;/p&gt;

</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_10857" key="com.pyxis.greenhopper.jira:gh-epic-link">
                        <customfieldname>Epic Link</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>PM-123</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hs8unb:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                    <customfield id="customfield_10557" key="com.pyxis.greenhopper.jira:gh-sprint">
                        <customfieldname>Sprint</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue id="501">Platform 3 05/15/15</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>