diff options
author | Gerwin Gsenger <g.gsenger@datentechnik-innovation.at> | 2015-01-13 14:43:57 +0100 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:07:44 +0200 |
commit | 3ee292090346f27e7e23d4943b74ada281f20552 (patch) | |
tree | 4cb00377a31757a39689bec25ee0dc20aca978ee /id/server/idserverlib/src/main/java/at/gv | |
parent | ccb85bd3fcd268b4e3cece2d57c1a92374cbc59d (diff) | |
download | moa-id-spss-3ee292090346f27e7e23d4943b74ada281f20552.tar.gz moa-id-spss-3ee292090346f27e7e23d4943b74ada281f20552.tar.bz2 moa-id-spss-3ee292090346f27e7e23d4943b74ada281f20552.zip |
add initial deserialization, small code restructuring
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv')
-rw-r--r-- | id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java | 118 |
1 files changed, 90 insertions, 28 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java index 2efb1e251..00a685b5e 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java @@ -8,16 +8,19 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import at.gv.egovernment.moa.id.config.ConfigurationException; import at.gv.egovernment.moa.id.config.ConfigurationProvider; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -30,8 +33,18 @@ public class ConfigurationToJSONConverter { try { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - List<String> jsonResults = converter.convertConfigToJSONStrings(true); - System.out.println(jsonResults); + Map<String, String> jsonResults = converter.convertConfigToJSONStrings(true); + for (String name : jsonResults.keySet()) { + System.out.println("Method: " + name + " = "); + System.out.println(jsonResults.get(name)); + converter.convertJsonStringToObject(jsonResults.get(name)); + } + + System.out.println("Try conversion from Json to object"); + for (String name : jsonResults.keySet()) { + Object obj = converter.convertJsonStringToObject(jsonResults.get(name)); + System.out.println(obj); + } } catch (ConfigurationException e) { e.printStackTrace(); @@ -51,9 +64,10 @@ public class ConfigurationToJSONConverter { try (PrintWriter outStream = new PrintWriter(out)) { // get pretty printed JSON ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - List<String> jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); - for (String json : jsonResults) { - outStream.println(json); + Map<String, String> jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); + for (String name : jsonResults.keySet()) { + outStream.println(name); + outStream.println(jsonResults.get(name)); } } @@ -61,24 +75,17 @@ public class ConfigurationToJSONConverter { public void writeConfigToJSONDB() throws ConfigurationException { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - List<String> jsonResults = converter.convertConfigToJSONStrings(false); - if(jsonResults.isEmpty()){ + Map<String, String> jsonResults = converter.convertConfigToJSONStrings(false); + if (jsonResults.isEmpty()) { System.out.println("WARN: writing EMPTY configuration to database"); } // TODO: write to db } - public List<String> convertConfigToJSONStrings(boolean prettyPrint) { - - List<String> result = new ArrayList<String>(); - ObjectMapper mapper = new ObjectMapper(); - mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); - mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); - mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + public Map<String, String> convertConfigToJSONStrings(boolean prettyPrint) { - if (prettyPrint) { - mapper.enable(SerializationFeature.INDENT_OUTPUT); - } + Map<String, String> result = new HashMap<String, String>(); + JsonMapper mapper = new JsonMapper(prettyPrint); try { // find all getter methods @@ -89,30 +96,85 @@ public class ConfigurationToJSONConverter { && (!"class".equals(pd.getName())) && (pd.getReadMethod().getAnnotation(JsonIgnore.class) == null)) { try { + JsonProperty name = pd.getReadMethod().getAnnotation(JsonProperty.class); // get result of get method Object tmp = pd.getReadMethod().invoke(config); // convert result to JSON - String show = mapper.writeValueAsString(tmp); - System.out.println("Method: " + pd.getDisplayName()); - System.out.println(show); - result.add(show); + String json = mapper.serialize(tmp); + if (name != null) { + result.put(name.value(), json); + } else { + System.out.println("CHECK if '" + pd.getDisplayName() + "' is NOT ANNOTATED"); + } + } catch (JsonGenerationException | JsonMappingException e) { - System.out.println("Problems while writing JSON as String"); - return new ArrayList<String>(); + System.out.println("Problems while writing JSON as String"); + return new HashMap<String, String>(); } } } - // TODO: handle static methods + // no static method handling needed } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { System.out.println("Problems while using reflection to get all getter methods."); } catch (IOException e) { - System.out.println("Problems while writing JSON as String"); - return new ArrayList<String>(); + System.out.println("Problems while writing JSON as string"); + return new HashMap<String, String>(); } return result; } + public Object convertJsonStringToObject(String jsonString){ + JsonMapper mapper = new JsonMapper(); + Object result = null; + try { + result = mapper.deserialize(jsonString, null); + } catch (JsonParseException | JsonMappingException e) { + System.out.println("Problems parsing the JSON string"); + return null; + } catch (IOException e) { + System.out.println("Problems while reading JSON string"); + return null; + } + + return result; + } + + private class JsonMapper { + + private ObjectMapper mapper = new ObjectMapper(); + + public JsonMapper(){ + this(false); + } + + public JsonMapper(boolean prettyPrint) { + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + if (prettyPrint) { + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + } + + public String serialize(Object value) throws JsonProcessingException { + return mapper.writeValueAsString(value); + } + + public Object deserialize(String value, Class type) throws JsonParseException, JsonMappingException, + IOException { + + ObjectMapper mapper = new ObjectMapper(); + if (type != null) { + return mapper.readValue(value, type); + } else { + return mapper.readValue(value, Object.class); + } + + } + + } + } |