import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Map<String, Object> variables = new HashMap<>();
return variables;
}
-
-
- static Object convert(JsonParser parser) throws IOException
- {
- JsonToken token = parser.getCurrentToken();
- if (token == null)
- fail(parser, "unexpected EOF");
-
- switch (token)
- {
- case VALUE_STRING: return parser.getText();
- case VALUE_NUMBER_INT: return parser.getIntValue();
- case VALUE_NUMBER_FLOAT: return parser.getDoubleValue();
- case START_OBJECT: return convertObject(parser);
- case START_ARRAY: return convertArray(parser);
- case VALUE_TRUE: return Boolean.TRUE;
- case VALUE_FALSE: return Boolean.FALSE;
- case VALUE_NULL: return null;
- }
-
- fail(parser, "unexpected token " + token.toString());
- return null; // << Will never be reached, because fail always throws an exception
- }
-
- static Map<String, Object> convertObject(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
-
- Map<String, Object> map = new LinkedHashMap<>();
-
- while (!JsonToken.END_OBJECT.equals(token))
- {
- if (!JsonToken.FIELD_NAME.equals(token))
- fail(parser, "expected a field-name");
-
- String name = parser.getText();
- parser.nextToken();
- Object value = convert(parser);
- map.put(name, value);
-
- token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
- }
-
- return map;
- }
-
- static List<Object> convertArray(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
-
- List<Object> list = new LinkedList<>();
-
- while (!JsonToken.END_ARRAY.equals(token))
- {
- list.add(convert(parser));
-
- token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
- }
-
- return list;
- }
-
- static void fail(JsonParser parser, String message)
- {
- JsonLocation location = parser.getCurrentLocation();
- LOG.error(
- "{} at char-offset {} (line {}, column {})",
- message,
- location.getCharOffset(),
- location.getLineNr(),
- location.getColumnNr()
- );
- throw new RuntimeException("Cannot parse JSON: " + message);
- }
}