ex00 commented on a change in pull request #8632: [FLINK-12744][ml] add shared
params in ml package
URL: https://github.com/apache/flink/pull/8632#discussion_r293398823
##########
File path:
flink-ml-parent/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java
##########
@@ -114,38 +171,104 @@ public Params clone() {
* @return a json containing all parameters in this Params
*/
public String toJson() {
- ObjectMapper mapper = new ObjectMapper();
- Map<String, String> stringMap = new HashMap<>();
try {
- for (Map.Entry<String, Object> e : paramMap.entrySet())
{
- stringMap.put(e.getKey(),
mapper.writeValueAsString(e.getValue()));
- }
- return mapper.writeValueAsString(stringMap);
+ return mapper.writeValueAsString(params);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to serialize params
to json", e);
}
}
/**
* Restores the parameters from the given json. The parameters should
be exactly the same with
- * the one who was serialized to the input json after the restoration.
The class mapping of the
- * parameters in the json is required because it is hard to directly
restore a param of a user
- * defined type. Params will be treated as String if it doesn't exist
in the {@code classMap}.
+ * the one who was serialized to the input json after the restoration.
*
- * @param json the json String to restore from
- * @param classMap the classes of the parameters contained in the json
+ * @param json the json String to restore from
*/
@SuppressWarnings("unchecked")
- public void loadJson(String json, Map<String, Class<?>> classMap) {
+ public void loadJson(String json) {
ObjectMapper mapper = new ObjectMapper();
+ Map<String, String> params;
try {
- Map<String, String> m = mapper.readValue(json,
Map.class);
- for (Map.Entry<String, String> e : m.entrySet()) {
- Class<?> valueClass =
classMap.getOrDefault(e.getKey(), String.class);
- paramMap.put(e.getKey(),
mapper.readValue(e.getValue(), valueClass));
+ params = mapper.readValue(json, Map.class);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to deserialize
json:" + json, e);
+ }
+ this.params.clear();
Review comment:
what do you think about using the merge approach here?
I thought that the goal of this method is restoring params values from json
without dropping all exist params.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services