-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deserialization in DynawoModelsSupplier and DynawoEventModelsSupp…
…lier Signed-off-by: lisrte <[email protected]>
- Loading branch information
Showing
5 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/SupplierJsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.powsybl.dynawaltz.suppliers; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.powsybl.commons.PowsyblException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public class SupplierJsonDeserializer<T> { | ||
|
||
private final StdDeserializer<List<T>> deserializer; | ||
|
||
public SupplierJsonDeserializer(StdDeserializer<List<T>> deserializer) { | ||
this.deserializer = deserializer; | ||
} | ||
|
||
public List<T> deserialize(Path path) { | ||
try { | ||
Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8); | ||
return setupObjectMapper().readValue(reader, new TypeReference<>() { | ||
}); | ||
} catch (IOException e) { | ||
throw new PowsyblException("JSON input cannot be read", e); | ||
} | ||
} | ||
|
||
public List<T> deserialize(InputStream is) { | ||
try { | ||
return setupObjectMapper().readValue(is, new TypeReference<>() { | ||
}); | ||
} catch (IOException e) { | ||
throw new PowsyblException("JSON input cannot be read", e); | ||
} | ||
} | ||
|
||
private ObjectMapper setupObjectMapper() { | ||
return new ObjectMapper().registerModule(new SimpleModule().addDeserializer(List.class, deserializer)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters