Reads an array or object, writing JSON to the output.
| 129 | |
| 130 | // Reads an array or object, writing JSON to the output. |
| 131 | void parseSequence(bool isObject) { |
| 132 | _out << get(); // open bracket/brace |
| 133 | const char closeBracket = (isObject ? '}' : ']'); |
| 134 | bool first = true; |
| 135 | char c; |
| 136 | while (closeBracket != (c = peekToken())) { |
| 137 | if (first) |
| 138 | first = false; |
| 139 | else |
| 140 | _out << ","; |
| 141 | |
| 142 | if (isObject) { |
| 143 | // Key: |
| 144 | if (c == '"' || c == '\'') { |
| 145 | parseString(); |
| 146 | } else if (isalpha(c) || c == '_' || c == '$') { |
| 147 | _out << '"' << get(); |
| 148 | while (true) { |
| 149 | c = peek(); |
| 150 | if (isalnum(c) || c == '_') |
| 151 | _out << get(); |
| 152 | else |
| 153 | break; |
| 154 | } |
| 155 | _out << '"'; |
| 156 | } else { |
| 157 | fail("Invalid key"); |
| 158 | } |
| 159 | if (peekToken() != ':') |
| 160 | fail("Expected ':' after key"); |
| 161 | _out << get(); |
| 162 | } |
| 163 | |
| 164 | // Value, or array item: |
| 165 | parseValue(); |
| 166 | |
| 167 | if (peekToken() == ',') |
| 168 | get(); |
| 169 | else if (peekToken() != closeBracket) |
| 170 | fail("unexpected token after array/object item"); |
| 171 | } |
| 172 | _out << get(); // copy close bracket/brace |
| 173 | } |
| 174 | |
| 175 | // Returns the next non-whitespace, non-comment character from the input. |
| 176 | // Consumes whitespace and comments, but not the character it returns. |