@Test public void testNestedObject() throws Exception { final String JSON = "{\"array\":[1,2],\"obj\":{\"first\":true}}"; ConfigObject value = MAPPER.readValue(JSON, ConfigObject.class); assertEquals(ConfigValueType.OBJECT, value.valueType()); assertEquals(2, value.size()); ConfigList array = (ConfigList) value.get("array"); assertEquals(ConfigValueType.LIST, array.valueType()); assertEquals(2, (array.unwrapped().size())); ConfigValue objValue = value.get("obj"); assertEquals(ConfigValueType.OBJECT, objValue.valueType()); ConfigObject obj = (ConfigObject) objValue; assertEquals(1, (obj.size())); }
private void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
void parseJson(String jsonString, LogRecord logRecord) { String cleanedJsonString = IngestUtilities.removeUnprintableCharacters(jsonString); Config cfg = ConfigFactory.parseString(cleanedJsonString); for (Map.Entry<String, ConfigValue> entry : cfg.entrySet()) { String key = entry.getKey(); ConfigValue value = entry.getValue(); switch (value.valueType()) { case BOOLEAN: case NUMBER: case OBJECT: case STRING: logRecord.setValue(key, ObjectUtils.toString(value.unwrapped())); break; case LIST: ConfigList list = (ConfigList) value; Gson gson = new Gson(); String json = gson.toJson(list.unwrapped()); logRecord.setValue(key, json); break; case NULL: default: // skip } } }
private static ConfigList expandSugarArray(ConfigValue fieldValue, Class<?> elementType, PluginRegistry pluginRegistry, boolean nested) { ConfigList fieldList = (ConfigList) fieldValue; List<Object> newList = new ArrayList<>(fieldList.size()); for (ConfigValue listEntry : fieldList) { ConfigValue listObject; if (nested) { listObject = expandSugarArray(listEntry, elementType, pluginRegistry, false); } else { listObject = expandSugar(elementType, listEntry, pluginRegistry); } newList.add(listObject.unwrapped()); } return ConfigValueFactory.fromIterable(newList, fieldList.origin().description()); }
private static String render(ConfigValue value) { String result; if (value.valueType().equals(ConfigValueType.LIST)) { result = "[" + ((ConfigList) value).stream() .map(KeyValue::render) .collect(Collectors.joining(", ")) + "]"; } else if (value.valueType().equals(ConfigValueType.STRING)) { result = '"' + value.unwrapped().toString() + '"'; } else { result = value.unwrapped().toString(); } return result; }
public HoconModule() { super("Hocon"); this.addDeserializer(ConfigValue.class, HoconDeserializer.CONFIG_VALUE); this.addDeserializer(ConfigObject.class, HoconDeserializer.CONFIG_OBJECT); this.addDeserializer(ConfigList.class, HoconDeserializer.CONFIG_LIST); this.addDeserializer(Config.class, HoconDeserializer.CONFIG); }
@Test public void testSimpleArray() throws Exception { final String JSON = "[1,true,\"foo\"]"; ConfigList value = MAPPER.readValue(JSON, ConfigList.class); assertEquals(ConfigValueType.LIST, value.valueType()); assertEquals(3, value.size()); assertEquals(ConfigValueType.NUMBER, value.get(0).valueType()); assertEquals(true, value.get(1).unwrapped()); assertEquals(ConfigValueType.STRING, value.get(2).valueType()); }
@Test public void testNestedArray() throws Exception { final String JSON = "[1,[false,45],{\"foo\":13}]"; ConfigList value = MAPPER.readValue(JSON, ConfigList.class); assertEquals(ConfigValueType.LIST, value.valueType()); assertEquals(3, value.size()); assertEquals(ConfigValueType.NUMBER, value.get(0).valueType()); assertEquals(ConfigValueType.LIST, value.get(1).valueType()); assertEquals(ConfigValueType.OBJECT, value.get(2).valueType()); }
private static ConfigValueType getValueTypeOrNull(Class<?> parameterClass) { if (parameterClass == Boolean.class || parameterClass == boolean.class) { return ConfigValueType.BOOLEAN; } else if (parameterClass == Integer.class || parameterClass == int.class) { return ConfigValueType.NUMBER; } else if (parameterClass == Double.class || parameterClass == double.class) { return ConfigValueType.NUMBER; } else if (parameterClass == Long.class || parameterClass == long.class) { return ConfigValueType.NUMBER; } else if (parameterClass == String.class) { return ConfigValueType.STRING; } else if (parameterClass == Duration.class) { return null; } else if (parameterClass == ConfigMemorySize.class) { return null; } else if (parameterClass == List.class) { return ConfigValueType.LIST; } else if (parameterClass == Map.class) { return ConfigValueType.OBJECT; } else if (parameterClass == Config.class) { return ConfigValueType.OBJECT; } else if (parameterClass == ConfigObject.class) { return ConfigValueType.OBJECT; } else if (parameterClass == ConfigList.class) { return ConfigValueType.LIST; } else { return null; } }
/** * Validates that the passed in config object contains a valid supported regions section by * checking that the param is: * - not absent, null, or wrong type * - not an empty list * * @param providerSection the provider section of the Azure Plugin config * @throws IllegalArgumentException if the regions list is empty * @throws ConfigException if the regions config section is missing or the wrong type */ static void validateSupportedRegions(Config providerSection) throws IllegalArgumentException, ConfigException { ConfigList supportedRegions = providerSection.getList(Configurations.AZURE_CONFIG_PROVIDER_REGIONS); if (supportedRegions.size() == 0) { throw new IllegalArgumentException(String.format("The list of supported regions \"%s\" in " + "the the Azure director plugin configuration is empty.", Configurations.AZURE_CONFIG_PROVIDER_REGIONS)); } }
private static void initializeUDFs(Config config) { if (!config.hasPath("udfs")) return; if (!config.getValue("udfs").valueType().equals(ConfigValueType.LIST)) { throw new RuntimeException("UDFs must be provided as a list"); } ConfigList udfList = config.getList("udfs"); for (ConfigValue udfValue : udfList) { ConfigValueType udfValueType = udfValue.valueType(); if (!udfValueType.equals(ConfigValueType.OBJECT)) { throw new RuntimeException("UDF list must contain UDF objects"); } Config udfConfig = ((ConfigObject)udfValue).toConfig(); for (String path : Lists.newArrayList("name", "class")) { if (!udfConfig.hasPath(path)) { throw new RuntimeException("UDF entries must provide '" + path + "'"); } } String name = udfConfig.getString("name"); String className = udfConfig.getString("class"); // null third argument means that registerJava will infer the return type Contexts.getSparkSession().udf().registerJava(name, className, null); LOG.info("Registered Spark SQL UDF: " + name); } }
private void parseProtectedResources(HttpSecurity http, ConfigObject protectedResources) throws Exception { for (ConfigValue cv : protectedResources.values()) { LinkedList<String> patterns = new LinkedList<>(); LinkedList<String> roles = new LinkedList<>(); HashSet<HttpMethod> methods = new HashSet<>(); for (ConfigValue configValue : (ConfigList) cv) { try { if (configValue instanceof ConfigList) { for (ConfigValue role : (ConfigList) configValue) { String r = ((String) role.unwrapped()).toUpperCase().trim(); // check if any HTTP methods appear here HttpMethod m = HttpMethod.resolve(r); if (m != null) { methods.add(m); } else { roles.add(r); } } } else { patterns.add((String) configValue.unwrapped()); } } catch (Exception e) { logger.error("Invalid config syntax for protected resource: {}.", configValue.render(), e); } } String[] rolz = (roles.isEmpty()) ? DEFAULT_ROLES : roles.toArray(new String[0]); String[] patternz = patterns.toArray(new String[0]); if (methods.isEmpty()) { http.authorizeRequests().antMatchers(patternz).hasAnyRole(rolz); } else { for (HttpMethod method : methods) { http.authorizeRequests().antMatchers(method, patternz).hasAnyRole(rolz); } } } }
private IgnoredRequestMatcher() { ConfigList c = Config.getConfig().getList("security.ignored"); List<RequestMatcher> list = new ArrayList<>(c.size()); for (ConfigValue configValue : c) { list.add(new AntPathRequestMatcher((String) configValue.unwrapped())); } orMatcher = new OrRequestMatcher(list); }
/** * Method called to create a new context for iterating all * contents of the current structured value (JSON array or object) */ public final ConfigNodeCursor iterateChildren() { ConfigValue n = currentNode(); if (n == null) throw new IllegalStateException("No current node"); if (n.valueType() == LIST) { // false since we have already returned START_ARRAY return new Array((ConfigList) n, this); } if (n.valueType() == OBJECT) { return new Object((ConfigObject) n, this); } throw new IllegalStateException("Current node of type " + n.getClass().getName()); }
@Override public boolean currentHasChildren() { // note: ONLY to be called for container nodes ConfigValue currentValue = currentNode(); if (currentValue.valueType() == ConfigValueType.LIST) { return !((ConfigList) currentValue).isEmpty(); } else { return !((ConfigObject) currentValue).isEmpty(); } }
public ConfigTraversingParser(ConfigValue n, ObjectCodec codec) { super(0); _objectCodec = codec; currentConfig = n; if (n.valueType() == LIST) { _nextToken = JsonToken.START_ARRAY; _nodeCursor = new ConfigNodeCursor.Array((ConfigList) n, null); } else if (n.valueType() == OBJECT) { _nextToken = JsonToken.START_OBJECT; _nodeCursor = new ConfigNodeCursor.Object((ConfigObject) n, null); } else { // value node _nodeCursor = new ConfigNodeCursor.RootValue(n, null); } }
@Override public List<? extends ConfigValue> getValues(String path) { ConfigList cl = config.getList(path); if (cl != null) { List<ConfigValue> l = new LinkedList<>(); for (com.typesafe.config.ConfigValue cv : cl) { l.add(new TypesafeValue(cv)); } return l; } else { return null; } }
@Override public boolean currentHasChildren() { if(currentNode() instanceof ConfigList) { return !((ConfigList)currentNode()).isEmpty(); } else if (currentNode() instanceof ConfigObject) { return !((ConfigObject)currentNode()).isEmpty(); } else { return false; } }
@Override public ConfigList getList(String path) { return c.getList(path); }
@Override public ConfigList getList(String path) { return config.getList(path); }
public TimeseriesClassifierNetworkBuilder setConfig(Config tsconfig, String networkConfigPrefix) { this.configuration = tsconfig; NeuralNetConfiguration.Builder networkConfigurationBuilder = new NeuralNetConfiguration.Builder() .seed(configuration.getInt(networkConfigPrefix+".seed")) .optimizationAlgo(OptimizationAlgorithm.valueOf(configuration.getString(networkConfigPrefix+".optimizationAlgo.type"))) .iterations(configuration.getInt(networkConfigPrefix+".optimizationAlgo.iterations")) .weightInit(WeightInit.valueOf( configuration.getString(networkConfigPrefix+".weightInit"))) .updater(Updater.valueOf(configuration.getString(networkConfigPrefix+".updater.type"))) .momentum(configuration.getDouble(networkConfigPrefix+".updater.momentum")) .learningRate(configuration.getDouble(networkConfigPrefix+".learningRate")) .gradientNormalization(GradientNormalization.valueOf( configuration.getString(networkConfigPrefix+".gradientNormalization.type"))) .gradientNormalizationThreshold( configuration.getDouble(networkConfigPrefix+".gradientNormalization.threshold")); ConfigList layers = configuration.getList(networkConfigPrefix+".layers"); if(layers != null && layers.size()>0){ NeuralNetConfiguration.ListBuilder networkConfigurationListBuilder = networkConfigurationBuilder.list(); for (int i = 0; i < layers.size(); i++) { ConfigValue value = layers.get(i); switch (value.valueType()) { case OBJECT: Config configObject = ((ConfigObject) value).toConfig(); networkConfigurationListBuilder .layer(configObject.getInt("number"), NetworkTypeFactory.makeLayer(configObject)); break; case NULL: throw new RuntimeException("LAYER LIST ITEM CANT BE NULL."); default: throw new RuntimeException("LAYER TYPE MUST BE A CONFIG OBJECT"); } } this.multiLayerConfiguration = networkConfigurationListBuilder .pretrain(false).backprop(true).build(); } else { throw new RuntimeException("NO NETWORK LIST FOUND."); } // // ----- Configure the network ----- // MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() // .seed(123) //Random number generator seed for improved repeatability. Optional. // .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1) // .weightInit(WeightInit.XAVIER) // .updater(Updater.NESTEROVS).momentum(0.85) // .learningRate(0.02) // .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) //Not always required, but helps with this data set // .gradientNormalizationThreshold(0.45) // .list() // .layer(0, new GravesLSTM.Builder().activation("tanh").nIn(1).nOut(10).build()) // .layer(1, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT) // .activation("softmax").nIn(10).nOut(numLabelClasses).build()) // .pretrain(false).backprop(true).build(); return this; }
private static Object getValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPropName) { if (parameterClass == Boolean.class || parameterClass == boolean.class) { return config.getBoolean(configPropName); } else if (parameterClass == Integer.class || parameterClass == int.class) { return config.getInt(configPropName); } else if (parameterClass == Double.class || parameterClass == double.class) { return config.getDouble(configPropName); } else if (parameterClass == Long.class || parameterClass == long.class) { return config.getLong(configPropName); } else if (parameterClass == String.class) { return config.getString(configPropName); } else if (parameterClass == Duration.class) { return config.getDuration(configPropName); } else if (parameterClass == ConfigMemorySize.class) { return config.getMemorySize(configPropName); } else if (parameterClass == Object.class) { return config.getAnyRef(configPropName); } else if (parameterClass == List.class) { return getListValue(beanClass, parameterType, parameterClass, config, configPropName); } else if (parameterClass == Map.class) { // we could do better here, but right now we don't. Type[] typeArgs = ((ParameterizedType) parameterType).getActualTypeArguments(); if (typeArgs[0] != String.class || typeArgs[1] != Object.class) { throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported Map<" + typeArgs[0] + "," + typeArgs[1] + ">, only Map<String,Object> is supported right now"); } return config.getObject(configPropName).unwrapped(); } else if (parameterClass == Config.class) { return config.getConfig(configPropName); } else if (parameterClass == ConfigObject.class) { return config.getObject(configPropName); } else if (parameterClass == ConfigValue.class) { return config.getValue(configPropName); } else if (parameterClass == ConfigList.class) { return config.getList(configPropName); } else if (hasAtLeastOneBeanProperty(parameterClass)) { return createInternal(config.getConfig(configPropName), parameterClass); } else { throw new ConfigException.BadBean("Bean property " + configPropName + " of class " + beanClass.getName() + " has unsupported type " + parameterType); } }
public ConfigList getList(String path) { return config.getList(path); }
public Array(ConfigList n, ConfigNodeCursor p) { super(JsonStreamContext.TYPE_ARRAY, p); _contents = n.iterator(); }
public Array(ConfigValue n, HoconNodeCursor p) { super(JsonStreamContext.TYPE_ARRAY, p); _contents = ((ConfigList)n).iterator(); }