org.ff4j.utils.json.FeatureJsonParser Maven / Gradle / Ivy
package org.ff4j.utils.json;
/*
* #%L
* ff4j-web
* %%
* Copyright (C) 2013 - 2014 Ff4J
* %%
* 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.
* #L%
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.ff4j.core.Feature;
import org.ff4j.core.FlippingStrategy;
import org.ff4j.property.Property;
import org.ff4j.property.util.PropertyFactory;
import org.ff4j.utils.MappingUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Unmarshalling data from JSON with Jackson.
*
* @author Cedrick LUNVEN
*/
public class FeatureJsonParser {
/** Jackson mapper. */
private static ObjectMapper objectMapper = new ObjectMapper();
/**
* Hide constructor.
*/
private FeatureJsonParser() {
}
/**
* Unmarshall {@link Feature} from json string.
*
* @param json
* json representation of feature.
* @return feature object
*/
@SuppressWarnings("unchecked")
public static Feature parseFeature(String json) {
try {
return parseFeatureMap(objectMapper.readValue(json, HashMap.class));
} catch (IOException e) {
throw new IllegalArgumentException("Cannot parse json as Feature " + json, e);
}
}
@SuppressWarnings("unchecked")
private static Feature parseFeatureMap(Map fMap) {
Feature f = new Feature((String) fMap.get("uid"));
f.setEnable((Boolean) fMap.get("enable"));
f.setDescription((String) fMap.get("description"));
f.setGroup((String) fMap.get("group"));
// permissions
List perm = (ArrayList) fMap.get("permissions");
f.setPermissions(new HashSet());
if (perm != null) {
f.getPermissions().addAll(perm);
}
// flipping strategy
f.setFlippingStrategy(parseFlipStrategy(f.getUid(), (LinkedHashMap) fMap.get("flippingStrategy")));
// custom properties
Map propertyMap = (Map < String, Object >) fMap.get("customProperties");
f.setCustomProperties(parseCustomProperties(propertyMap));
return f;
}
/**
* Parse the "customproperties" JSOn attribute.
*
* @param uid
* current feature identifier
* @param tag
* current TAG
* @return
* target map of properties
*/
@SuppressWarnings("unchecked")
private static Map < String, Property>> parseCustomProperties(Map customPTag) {
Map < String, Property>> myProperties = new LinkedHashMap>();
if (null != customPTag && !customPTag.isEmpty()) {
// Loop over properties
for (Object property : customPTag.values()) {
HashMap propertyJson = (HashMap) property;
String propertyName = (String) propertyJson.get("name");
String propertyVal = String.valueOf(propertyJson.get("value"));
String propertyType = (String) propertyJson.get("type");
Property> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal);
// FixedValued
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy