Quantcast
Channel: WE MOVED to github.com/microsoft/cpprestsdk. This site is not monitored!
Viewing all articles
Browse latest Browse all 4845

Commented Unassigned: json empty array serialized as "array":] instead "array":[] [73]

$
0
0
Let's consider following test code:
```
auto json_original = std::wstring(LR"({"thumbsDown":[],"thumbsUp":[]})");
auto json_value = json::value::parse(json_original);
auto json_copy = json_value.to_string();
auto json_same = json_copy == json_original;
```

As in original result should be ```{"thumbsDown":[],"thumbsUp":[]}``` but what I got is ```{"thumbsDown":],"thumbsUp":]}```.

Let's look at code. Arrays are serialized by this function:
```
template<typename CharType>
void format_impl(std::basic_string<CharType>& str) const
{
str.push_back('[');
for (auto iter = m_elements.begin(); iter != m_elements.end(); ++iter)
{
iter->second.format(str);
str.push_back(',');
}
str[str.size() - 1] = ']';
}
```
In case where m_elements container is empty openning bracket will be replaced by closing one. A way to fix it is adding a case for empty container case:
```
template<typename CharType>
void format_impl(std::basic_string<CharType>& str) const
{
if (!m_elements.empty())
{
str.push_back('[');
for (auto iter = m_elements.begin(); iter != m_elements.end(); ++iter)
{
iter->second.format(str);
str.push_back(',');
}
str[str.size() - 1] = ']';
}
else
{
str.push_back('[');
str.push_back(']');
}
}
```

Comments: Hi thedmd, Yes you are absolutely correct here, we are not handling the empty array case correctly. Thanks for taking the time to report this issue. I'll make sure it is fix in our next release, 1.5. Thanks, Steve

Viewing all articles
Browse latest Browse all 4845

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>