Java Code Examples for com.fasterxml.jackson.core.JsonFactory#enable()

The following examples show how to use com.fasterxml.jackson.core.JsonFactory#enable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatalogManagerImpl.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
private Map<String, Map<String, CatalogInfo>> initializeData(CatalogReader catalogReader) throws Exception {
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper mapper = new ObjectMapper(factory);

    String catalog = catalogReader.readCatalog();
    catalogData = mapper.readValue(catalog, CatalogData.class);

    if (catalogData.getAttributes() != null) {
        for (Attribute attr : catalogData.getAttributes()) {
            if (attr.isRequestContext()) {
                requestContextAttributes.put(attr.getName(), attr);
            }
            Map<String, Attribute> attrMap = attributeMap.computeIfAbsent(attr.getCatalogId(), k -> new HashMap<>());
            attrMap.put(attr.getName(), attr);
        }
    }

    Map<String, Map<String, CatalogInfo>> map = new HashMap<>();
    map.put(DEFAULT_CATALOG, new HashMap<>());
    for (Event event : catalogData.getEvents()) {
        addEntry(map, event);
    }
    return map;
}
 
Example 2
Source File: StringCatalogReader.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
public StringCatalogReader() throws Exception {
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper mapper = new ObjectMapper(factory);
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider.addFilter("catalogEvent", new CatalogEventFilter());
    mapper.setFilterProvider(filterProvider);
    catalogData = createCatalogData();
    json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(catalogData);
    assertNotNull("No json catalog created", json);
    File file = new File("target/testCatalog.json");
    PrintStream ps = new PrintStream(new FileOutputStream(file));
    ps.print(json);
    ps.close();
    lastUpdated = LocalDateTime.now();
}
 
Example 3
Source File: ClassPathCatalogReader.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
public ClassPathCatalogReader(Map<String, String> attributes) throws IOException {
    String catalogFile = attributes != null ?
        attributes.getOrDefault(CATALOG_ATTRIBUTE_NAME, DEFAULT_CATALOG_FILE) : DEFAULT_CATALOG_FILE;
    Collection<URL> catalogs = LoaderUtil.findResources(catalogFile);
    if (catalogs.isEmpty()) {
        LOGGER.error("No catalog named {} could be found on the class path", catalogFile);
        throw new FileNotFoundException("No catalog named " + catalogFile + " could be found");
    }

    URL catalogURL = catalogs.iterator().next();
    if (catalogs.size() > 1) {
        LOGGER.warn("Multiple catalogs named {} were found. Using {}", catalogFile, catalogURL.toString());
    }

    catalog = readCatalog(catalogURL);
    LocalDateTime localDateTime = null;
    try {
        URLConnection connection = catalogURL.openConnection();
        localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(connection.getLastModified()),
                ZoneId.systemDefault());
    } catch (IOException ioe) {
        LOGGER.warn("Unable to open connection to {}", catalogURL.toString());
    }
    lastUpdated = localDateTime;
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper objectMapper = new ObjectMapper(factory);
    catalogData = objectMapper.readValue(catalog, CatalogData.class);
}
 
Example 4
Source File: FileCatalogReader.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
public FileCatalogReader(Map<String, String> attributes) throws IOException {
    StringBuilder catalogPath = new StringBuilder();
    String basePath = attributes.get(BASEDIR);
    String catalogFile = attributes.getOrDefault(CATALOG_ATTRIBUTE_NAME, DEFAULT_CATALOG_FILE);
    if (basePath != null) {
        catalogPath.append(attributes.get(BASEDIR));
        if (basePath.endsWith("/")) {
            if (catalogFile.startsWith("/")) {
                catalogPath.append(catalogFile.substring(1));
            } else {
                catalogPath.append(catalogFile);
            }
        } else {
            if (catalogFile.startsWith("/")) {
                catalogPath.append(catalogFile);
            } else {
                catalogPath.append("/").append(catalogFile);
            }
        }
    } else if (catalogFile != null){
        catalogPath.append(catalogFile);
    } else {
        LOGGER.warn("No catalogFile attribute was provided. Using {}", DEFAULT_CATALOG_FILE);
        catalogPath.append(DEFAULT_CATALOG_FILE);
    }
    Path path = Paths.get(catalogPath.toString());
    lastUpdated = LocalDateTime.ofInstant(Instant.ofEpochMilli(path.toFile().lastModified()),
            ZoneId.systemDefault());
    byte[] encoded = Files.readAllBytes(path);
    catalog = new String(encoded, StandardCharsets.UTF_8);
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper objectMapper = new ObjectMapper(factory);
    catalogData = objectMapper.readValue(catalog, CatalogData.class);
}
 
Example 5
Source File: FileCatalogReader.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
public FileCatalogReader() throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(DEFAULT_CATALOG_FILE));
    catalog = new String(encoded, StandardCharsets.UTF_8);
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper objectMapper = new ObjectMapper(factory);
    catalogData = objectMapper.readValue(catalog, CatalogData.class);
}
 
Example 6
Source File: JsonCatalogReader.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
@Override
public String readCatalog() {
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    ObjectMapper mapper = new ObjectMapper(factory);
    try {
        return mapper.writeValueAsString(catalogData);
    } catch (JsonProcessingException ex) {
        LOGGER.error("Unable to serialze Catalog", ex);
        return null;
    }
}
 
Example 7
Source File: GitCatalogDao.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
public GitCatalogDao() {
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    mapper = new ObjectMapper(factory).enable(SerializationFeature.INDENT_OUTPUT);
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider.addFilter("catalogEvent", new CatalogEventFilter());
    mapper.setFilterProvider(filterProvider);
}
 
Example 8
Source File: JacksonExceptionsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException {
    final String json = "{'id':1,'name':'John'}";

    final JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    final ObjectMapper mapper = new ObjectMapper(factory);

    final User user = mapper.reader()
        .forType(User.class)
        .readValue(json);
    assertEquals("John", user.name);
}
 
Example 9
Source File: JsonBrowser.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
private static ObjectMapper setupMapper() {
  JsonFactory jsonFactory = new JsonFactory();
  jsonFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
  jsonFactory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
  return new ObjectMapper(jsonFactory);
}
 
Example 10
Source File: Config.java    From StreamingRec with Apache License 2.0 3 votes vote down vote up
/**
 * Automatically create a list of {@link Algorithm} objects based on a JSON configuration file
 * @param filename the name of the JSON config file
 * @return a list of Algorithm objects
 * @throws JsonParseException -
 * @throws JsonMappingException -
 * @throws IOException -
 */
public static List<Algorithm> loadAlgorithms(String filename) throws JsonParseException, JsonMappingException, IOException{
	//let jackson create and configure the Algorithm objects
	JsonFactory factory = new JsonFactory();
	factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
	return new ObjectMapper(factory).readValue(new File(filename), new TypeReference<List<Algorithm>>(){});
}
 
Example 11
Source File: Config.java    From StreamingRec with Apache License 2.0 3 votes vote down vote up
/**
 * Automatically create a list of {@link Metric} objects based on a JSON configuration file
 * @param filename the name of the JSON config file
 * @return a list of Metric objects
 * @throws JsonParseException -
 * @throws JsonMappingException -
 * @throws IOException -
 */
public static List<Metric> loadMetrics(String filename) throws JsonParseException, JsonMappingException, IOException{
	//let jackson create and configure the Metric objects
	JsonFactory factory = new JsonFactory();
	factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
	return new ObjectMapper(factory).readValue(new File(filename), new TypeReference<List<Metric>>(){});
}