Advertisement
spacechase0

remove comments from json

Aug 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. json parseJsonFileWithComments( const std::string& path )
  2. {
  3.     std::string str = util::getFileContents( path, true );
  4.    
  5.     std::string noComments = "";
  6.     bool quote = false;
  7.     bool escape = false;
  8.     for ( int i = 0; i < str.length(); ++i )
  9.     {
  10.         char c = str[ i ];
  11.         char n = i < str.length() - 1 ? str[ i + 1 ] : '\0';
  12.  
  13.         if ( escape )
  14.         {
  15.             noComments += c;
  16.             escape = false;
  17.             continue;
  18.         }
  19.  
  20.         if ( quote )
  21.         {
  22.             if ( c == '\\' )
  23.             {
  24.                 escape = true;
  25.                 continue;
  26.             }
  27.         }
  28.  
  29.         if ( c == '"' )
  30.             quote = !quote;
  31.         else if ( c == '/' && n == '/' )
  32.         {
  33.             i = str.find( '\n', i + 2 );
  34.             continue;
  35.         }
  36.         else if ( c == '/' && n == '*' )
  37.         {
  38.             i = str.find( "*/", i + 2 ) + 2;
  39.             continue;
  40.         }
  41.  
  42.         noComments += c;
  43.     }
  44.  
  45.     return json::parse( noComments );
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement