forked from Fraunhofer-AISEC/ids-clearing-house-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypeManagerUtil.java
47 lines (40 loc) · 1.3 KB
/
TypeManagerUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright (c) 2023 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - Initial implementation
* truzzt GmbH - EDC extension implementation
*
*/
package de.truzzt.clearinghouse.edc.types;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.edc.spi.EdcException;
import java.io.IOException;
import java.io.InputStream;
public class TypeManagerUtil {
private final ObjectMapper mapper;
public TypeManagerUtil(ObjectMapper mapper) {
this.mapper = mapper;
}
public <T> T parse(InputStream inputStream, Class<T> type) {
try {
return mapper.readValue(inputStream, type);
} catch (IOException e) {
throw new EdcException("Error parsing to type " + type, e);
}
}
public String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new EdcException("Error converting to JSON", e);
}
}
}