griffon.plugins.preferences.persistors.JsonPreferencesPersistor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of griffon-preferences-json Show documentation
Show all versions of griffon-preferences-json Show documentation
Griffon Preferences JSON Plugin
The newest version!
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package griffon.plugins.preferences.persistors;
import griffon.core.GriffonApplication;
import griffon.core.env.Metadata;
import griffon.plugins.preferences.Preferences;
import griffon.plugins.preferences.PreferencesManager;
import griffon.plugins.preferences.PreferencesNode;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* @author Andres Almiray
*/
public class JsonPreferencesPersistor extends AbstractMapBasedPreferencesPersistor {
@Inject
public JsonPreferencesPersistor(@Nonnull GriffonApplication application, @Nonnull Metadata metadata) {
super(application, metadata);
}
@Nonnull
@Override
protected String resolveExtension() {
return ".json";
}
@Nonnull
@SuppressWarnings("unchecked")
public Preferences read(@Nonnull PreferencesManager preferencesManager) throws IOException {
InputStream inputStream = inputStream();
JSONObject json = doRead(inputStream);
inputStream.close();
PreferencesNode node = preferencesManager.getPreferences().getRoot();
readInto(json, node);
return preferencesManager.getPreferences();
}
@SuppressWarnings({"unchecked", "ConstantConditions"})
protected void readInto(@Nonnull JSONObject json, @Nonnull PreferencesNode node) {
for (Object k : json.keySet()) {
String key = String.valueOf(k);
Object value = json.get(key);
if (value instanceof JSONObject) {
readInto((JSONObject) value, node.node(key));
} else if (value instanceof JSONArray) {
try {
node.putAt(key, expand((JSONArray) value));
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("Invalid value for '" + node.path() + "." + key + "' => " + value, iae);
}
} else if (value instanceof Number ||
value instanceof Boolean ||
value instanceof CharSequence) {
node.putAt(key, value);
} else {
throw new IllegalArgumentException("Invalid value for '" + node.path() + "." + key + "' => " + value);
}
}
}
@Nonnull
protected Collection expand(@Nonnull JSONArray array) {
List