@Test public void parseDocumentTypeWithValidSeparator() { EventParser eventParser = new EventParser(); DBObject dbObject = buildExpectedObject(); DocumentFieldDefinition fieldDefinition = (DocumentFieldDefinition) definition(MongoDataType.DOCUMENT); fieldDefinition.setDelimiter("#"); Map<String, FieldDefinition> documentMapping = new LinkedHashMap<String, FieldDefinition>(); documentMapping.put("field1", new SimpleFieldDefinition(MongoDataType.STRING)); documentMapping.put("field2", new SimpleFieldDefinition(MongoDataType.ARRAY)); DocumentFieldDefinition fieldDefinition2 = (DocumentFieldDefinition) definition(MongoDataType.DOCUMENT); fieldDefinition2.setDelimiter("#"); documentMapping.put("field3", fieldDefinition2); documentMapping.put("field6", new SimpleFieldDefinition(MongoDataType.STRING)); Map<String, FieldDefinition> documentMapping2 = new LinkedHashMap<String, FieldDefinition>(); documentMapping2.put("field4", new SimpleFieldDefinition(MongoDataType.STRING)); documentMapping2.put("field5", new SimpleFieldDefinition(MongoDataType.ARRAY)); fieldDefinition.setDocumentMapping(documentMapping); fieldDefinition2.setDocumentMapping(documentMapping2); assertThat(eventParser.parseValue(fieldDefinition, "Point1#[111.11,222.22]#Point2#[111.11,222.22]#Point3")) .isEqualTo( dbObject); }
public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); Object parseValue(final FieldDefinition fd, final String stringValue); DBObject parse(Event event); List<DBObject> parse(List<Event> events); }
EventParser { public Object parseValue(final FieldDefinition fd, final String stringValue) { if (fd == null || fd.getType() == null) { try { return JSON.parse(stringValue); } catch (JSONParseException ex) { log.trace("Could not parse as JSON, defaulting to String: {}", stringValue); return stringValue; } } switch (fd.getType()) { case DOUBLE: return Double.parseDouble(stringValue); case STRING: return stringValue; case OBJECT: case ARRAY: return JSON.parse(stringValue); case BINARY: SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd; final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING : sfd.getEncoding() .toLowerCase(Locale.ENGLISH); if ("base64".equals(encoding)) { return BaseEncoding.base64().decode(stringValue); } else { throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding); } case OBJECTID: return new ObjectId(stringValue); case BOOLEAN: return Boolean.parseBoolean(stringValue); case DATE: DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat(); if (dateFormat == null) { if (NumberUtils.isNumber(stringValue)) { return new Date(Long.parseLong(stringValue)); } else { return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate(); } } else { try { return dateFormat.parse(stringValue); } catch (ParseException ex) { log.warn("Could not parse date, defaulting to String: {}", stringValue); return stringValue; } } case NULL: return null; case INT32: return Integer.parseInt(stringValue); case INT64: return Long.parseLong(stringValue); case DOCUMENT: return populateDocument((DocumentFieldDefinition) fd, stringValue); default: throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name()); } } EventParser();  EventParser(final MappingDefinition mappingDefinition); Object parseValue(final FieldDefinition fd, final String stringValue); DBObject parse(Event event); List<DBObject> parse(List<Event> events);  }