JSONMapParser extends BasicParser { @Override @SuppressWarnings("unchecked") public List<JSONObject> parse(byte[] rawMessage) { try { String rawString = new String(rawMessage, getReadCharset()); List<Map<String, Object>> messages = new ArrayList<>(); if (wrapJson) { rawString = wrapMessageJson(rawString); } if (!StringUtils.isEmpty(jsonpQuery)) { Object parsedObject = JsonPath.parse(rawString).read(jsonpQuery, typeRef); if (parsedObject != null) { messages.addAll((List<Map<String,Object>>)parsedObject); } } else { messages.add(JSONUtils.INSTANCE.load(rawString, JSONUtils.MAP_SUPPLIER)); } ArrayList<JSONObject> parsedMessages = new ArrayList<>(); for (Map<String, Object> rawMessageMap : messages) { JSONObject ret = normalizeJson(rawMessageMap); if (overrideOriginalString) { JSONObject originalJsonObject = new JSONObject(rawMessageMap); ret.put("original_string", originalJsonObject.toJSONString()); } if (!ret.containsKey("timestamp")) { ret.put("timestamp", System.currentTimeMillis()); } parsedMessages.add(ret); } return Collections.unmodifiableList(parsedMessages); } catch (Throwable e) { String message = "Unable to parse " + new String(rawMessage, getReadCharset()) + ": " + e.getMessage(); LOG.error(message, e); throw new IllegalStateException(message, e); } }  @Override void configure(Map<String, Object> config); @Override void init(); @Override @SuppressWarnings("unchecked") List<JSONObject> parse(byte[] rawMessage); static final String MAP_STRATEGY_CONFIG; static final String JSONP_QUERY; static final String WRAP_JSON; static final String WRAP_ENTITY_NAME; static final String DEFAULT_WRAP_ENTITY_NAME; static final String OVERRIDE_ORIGINAL_STRING; }
@Test public void testHappyPath() { List<JSONObject> output = parser.parse(happyPathJSON.getBytes(StandardCharsets.UTF_8)); assertEquals(output.size(), 1); assertEquals(output.get(0).size(), 4); JSONObject message = output.get(0); assertEquals("bar", message.get("foo")); assertEquals("blah", message.get("blah")); assertNotNull(message.get("timestamp")); assertTrue(message.get("timestamp") instanceof Number); assertNotNull(message.get("number")); assertTrue(message.get("number") instanceof Number); }
@Test public void testCollectionHandlingDrop() { List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes(StandardCharsets.UTF_8)); assertEquals(output.size(), 1); assertEquals(output.get(0).size(), 1); JSONObject message = output.get(0); assertNotNull(message.get("timestamp")); assertTrue(message.get("timestamp") instanceof Number); }