@@ -13,11 +13,12 @@ JsonParser::JsonParser(std::string const& _input, CJOptions const& _opt)
1313 if (_input.size () < 2 || _input.find (" {" ) == string::npos || _input.rfind (" }" ) == string::npos)
1414 throw DataObjectException () << " ConvertJsoncppStringToData can't read json structure in file: `" + _input.substr (0 , 50 );
1515
16+ m_applyDepth.clear ();
1617 m_root.getContent ().setAutosort (_opt.autosort );
1718 m_actualRoot = &m_root.getContent ();
1819}
1920
20- string JsonParser::printDebug (size_t const & _i)
21+ string JsonParser::printDebug (size_t const & _i) const
2122{
2223 static const short c_debugSize = 120 ;
2324 string debug;
@@ -55,13 +56,24 @@ void JsonParser::parse()
5556 }
5657}
5758
59+ void JsonParser::checkJsonCommaEnding (size_t & _i) const
60+ {
61+ if (m_input.at (_i) == ' }' || m_input.at (_i) == ' ]' )
62+ _i--; // because cycle iteration we need to process ending clouse
63+ else if (m_input.at (_i) != ' ,' )
64+ throw DataObjectException () << errorPrefix
65+ + " Dataobject array/object expected ',' when listing elements, but got `" + m_input.at (_i) + " `"
66+ + " around: " + printDebug (_i);
67+ }
68+
5869JsonParser::RET JsonParser::tryParseKeyValue (size_t & _i)
5970{
6071 const bool escapeChar = (_i > 0 && m_input.at (_i - 1 ) == ' \\ ' );
6172 if (m_input.at (_i) == ' "' && !escapeChar)
6273 {
6374 spDataObject obj;
6475 string key = parseKeyValue (_i);
76+
6577 _i = skipSpaces (_i);
6678 if (m_input.at (_i) == ' :' )
6779 {
@@ -104,14 +116,12 @@ JsonParser::RET JsonParser::tryParseKeyValue(size_t& _i)
104116 if (m_actualRoot->type () == DataType::Array)
105117 {
106118 m_actualRoot->addArrayObject (sDataObject (std::move (key)));
107- if (m_input.at (_i) != ' ,' )
108- _i--; // because cycle iteration we need to process ending clouse
119+ checkJsonCommaEnding (_i);
109120 return RET::CONTINUE;
110121 }
111122 else
112123 m_actualRoot->setString (std::move (key));
113- if (m_input.at (_i) != ' ,' )
114- _i--; // because cycle iteration we need to process ending clouse
124+ checkJsonCommaEnding (_i);
115125 m_actualRoot = m_applyDepth.at (m_applyDepth.size () - 1 );
116126 m_applyDepth.pop_back ();
117127 return RET::CONTINUE;
@@ -263,25 +273,25 @@ size_t JsonParser::skipSpaces(size_t const& _i) const
263273string JsonParser::parseKeyValue (size_t & _i) const
264274{
265275 if (_i + 1 > m_input.size ())
266- throw DataObjectException () << errorPrefix + " reached EOF before reading char: `\" `" ;
276+ throw DataObjectException () << errorPrefix + " reached EOF before reading char: `\" ` around: " + printDebug (_i) ;
267277
268278 bool escapeChar = true ;
269279 size_t endPos = m_input.find (' "' , _i + 1 );
270- while (escapeChar)
271- {
272- escapeChar = (m_input[endPos - 1 ] == ' \\ ' );
273- if (escapeChar)
274- endPos = m_input.find (' "' , endPos + 1 );
275- }
276-
277280 if (endPos != string::npos)
278281 {
282+ while (escapeChar)
283+ {
284+ escapeChar = (m_input[endPos - 1 ] == ' \\ ' );
285+ if (escapeChar)
286+ endPos = m_input.find (' "' , endPos + 1 );
287+ }
288+
279289 const string key = m_input.substr (_i + 1 , endPos - _i - 1 );
280290 _i = endPos + 1 ;
281291 return key;
282292 }
283293 else
284- throw DataObjectException () << errorPrefix + " not found key ending char: `\" `" ;
294+ throw DataObjectException () << errorPrefix + " not found key ending char: `\" ` around: " + printDebug (_i) ;
285295 return string ();
286296}
287297
0 commit comments