@Test public void testFull() { String pExp = "{\"name\":\"p1\",\"description\":null,\"type\":\"org.ff4j.property.PropertyString\",\"value\":\"v1\",\"fixedValues\":null}"; Property<?> p = PropertyJsonParser.parseProperty(pExp); Assert.assertNotNull(p); Assert.assertNotNull(p.getType()); Assert.assertEquals("v1", p.getValue()); }
@SuppressWarnings("unchecked") public static Property<?> parseProperty(String json) { if (null == json || "".equals(json)) { return null; } Map<String, Object> propertyJson; try { propertyJson = objectMapper.readValue(json, HashMap.class); } catch (Exception re) { throw new IllegalArgumentException("Cannot parse JSON Property", re); } String propertyName = String.valueOf(propertyJson.get("name")); String propertyVal = String.valueOf(propertyJson.get("value")); String propertyType = String.valueOf(propertyJson.get("type")); Property < ?> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal); List <Object> listOfFixedValue = (List<Object>) propertyJson.get(FIXED_VALUES); if (listOfFixedValue != null) { for (Object v : listOfFixedValue) { ap.add2FixedValueFromString(String.valueOf(v)); } if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) { throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues()); } } return ap; }
PropertyJsonParser { @SuppressWarnings("unchecked") public static Property<?> parseProperty(String json) { if (null == json || "".equals(json)) { return null; } Map<String, Object> propertyJson; try { propertyJson = objectMapper.readValue(json, HashMap.class); } catch (Exception re) { throw new IllegalArgumentException("Cannot parse JSON Property", re); } String propertyName = String.valueOf(propertyJson.get("name")); String propertyVal = String.valueOf(propertyJson.get("value")); String propertyType = String.valueOf(propertyJson.get("type")); Property < ?> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal); List <Object> listOfFixedValue = (List<Object>) propertyJson.get(FIXED_VALUES); if (listOfFixedValue != null) { for (Object v : listOfFixedValue) { ap.add2FixedValueFromString(String.valueOf(v)); } if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) { throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues()); } } return ap; } }
PropertyJsonParser { @SuppressWarnings("unchecked") public static Property<?> parseProperty(String json) { if (null == json || "".equals(json)) { return null; } Map<String, Object> propertyJson; try { propertyJson = objectMapper.readValue(json, HashMap.class); } catch (Exception re) { throw new IllegalArgumentException("Cannot parse JSON Property", re); } String propertyName = String.valueOf(propertyJson.get("name")); String propertyVal = String.valueOf(propertyJson.get("value")); String propertyType = String.valueOf(propertyJson.get("type")); Property < ?> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal); List <Object> listOfFixedValue = (List<Object>) propertyJson.get(FIXED_VALUES); if (listOfFixedValue != null) { for (Object v : listOfFixedValue) { ap.add2FixedValueFromString(String.valueOf(v)); } if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) { throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues()); } } return ap; } private  PropertyJsonParser(); }
PropertyJsonParser { @SuppressWarnings("unchecked") public static Property<?> parseProperty(String json) { if (null == json || "".equals(json)) { return null; } Map<String, Object> propertyJson; try { propertyJson = objectMapper.readValue(json, HashMap.class); } catch (Exception re) { throw new IllegalArgumentException("Cannot parse JSON Property", re); } String propertyName = String.valueOf(propertyJson.get("name")); String propertyVal = String.valueOf(propertyJson.get("value")); String propertyType = String.valueOf(propertyJson.get("type")); Property < ?> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal); List <Object> listOfFixedValue = (List<Object>) propertyJson.get(FIXED_VALUES); if (listOfFixedValue != null) { for (Object v : listOfFixedValue) { ap.add2FixedValueFromString(String.valueOf(v)); } if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) { throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues()); } } return ap; } private  PropertyJsonParser(); @SuppressWarnings("unchecked") static Property<?> parseProperty(String json); @SuppressWarnings("unchecked") static Property<?>[] parsePropertyArray(String json); @SuppressWarnings("unchecked") static Property<?> parsePropertyMap(Map<String, Object> fMap); }
PropertyJsonParser { @SuppressWarnings("unchecked") public static Property<?> parseProperty(String json) { if (null == json || "".equals(json)) { return null; } Map<String, Object> propertyJson; try { propertyJson = objectMapper.readValue(json, HashMap.class); } catch (Exception re) { throw new IllegalArgumentException("Cannot parse JSON Property", re); } String propertyName = String.valueOf(propertyJson.get("name")); String propertyVal = String.valueOf(propertyJson.get("value")); String propertyType = String.valueOf(propertyJson.get("type")); Property < ?> ap = PropertyFactory.createProperty(propertyName, propertyType, propertyVal); List <Object> listOfFixedValue = (List<Object>) propertyJson.get(FIXED_VALUES); if (listOfFixedValue != null) { for (Object v : listOfFixedValue) { ap.add2FixedValueFromString(String.valueOf(v)); } if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) { throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues()); } } return ap; } private  PropertyJsonParser(); @SuppressWarnings("unchecked") static Property<?> parseProperty(String json); @SuppressWarnings("unchecked") static Property<?>[] parsePropertyArray(String json); @SuppressWarnings("unchecked") static Property<?> parsePropertyMap(Map<String, Object> fMap); static final String FIXED_VALUES; }