001/* 002 * Copyright (c) 2004-2020, Oracle and/or its affiliates. 003 * 004 * Licensed under the 2-clause BSD license. 005 * 006 * Redistribution and use in source and binary forms, with or without 007 * modification, are permitted provided that the following conditions are met: 008 * 009 * 1. Redistributions of source code must retain the above copyright notice, 010 * this list of conditions and the following disclaimer. 011 * 012 * 2. Redistributions in binary form must reproduce the above copyright notice, 013 * this list of conditions and the following disclaimer in the documentation 014 * and/or other materials provided with the distribution. 015 * 016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 017 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 019 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 020 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 021 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 022 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 023 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 024 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 025 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 026 * POSSIBILITY OF SUCH DAMAGE. 027 */ 028 029package com.oracle.labs.mlrg.olcut.config.json; 030 031import com.fasterxml.jackson.core.JsonFactory; 032import com.fasterxml.jackson.core.JsonGenerator; 033import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; 034import com.oracle.labs.mlrg.olcut.config.io.ConfigLoader; 035import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException; 036import com.oracle.labs.mlrg.olcut.config.io.ConfigWriter; 037import com.oracle.labs.mlrg.olcut.config.io.ConfigWriterException; 038import com.oracle.labs.mlrg.olcut.config.ConfigurationData; 039import com.oracle.labs.mlrg.olcut.config.io.FileFormatFactory; 040import com.oracle.labs.mlrg.olcut.config.property.GlobalProperties; 041import com.oracle.labs.mlrg.olcut.config.SerializedObject; 042import com.oracle.labs.mlrg.olcut.config.io.URLLoader; 043 044import java.io.IOException; 045import java.io.OutputStream; 046import java.util.Map; 047 048/** 049 * 050 */ 051public class JsonConfigFactory implements FileFormatFactory { 052 053 private final JsonFactory factory = new JsonFactory(); 054 055 @Override 056 public String getExtension() { 057 return "json"; 058 } 059 060 @Override 061 public ConfigLoader getLoader(URLLoader parent, Map<String, ConfigurationData> rpdMap, Map<String, ConfigurationData> existingRPD, Map<String, SerializedObject> serializedObjects, GlobalProperties globalProperties) throws ConfigLoaderException { 062 return new JsonLoader(factory,parent,rpdMap,existingRPD,serializedObjects,globalProperties); 063 } 064 065 @Override 066 public ConfigWriter getWriter(OutputStream writer) throws ConfigWriterException { 067 try { 068 JsonGenerator jsonWriter = factory.createGenerator(writer); 069 jsonWriter.setPrettyPrinter(new DefaultPrettyPrinter()); 070 return new JsonConfigWriter(jsonWriter); 071 } catch (IOException e) { 072 throw new ConfigWriterException(e); 073 } 074 } 075}