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

Edited 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(']');
}
}
```


Viewing all articles
Browse latest Browse all 4845


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