1
1
package com .redhat .labs .omp .config ;
2
2
3
+ import java .io .IOException ;
4
+ import java .lang .reflect .Type ;
5
+ import java .nio .file .Files ;
6
+ import java .nio .file .Path ;
7
+ import java .nio .file .Paths ;
8
+ import java .util .List ;
9
+
3
10
import javax .enterprise .context .ApplicationScoped ;
4
11
import javax .enterprise .event .Observes ;
5
12
import javax .json .bind .Jsonb ;
6
13
import javax .json .bind .JsonbBuilder ;
7
14
import javax .json .bind .JsonbConfig ;
8
15
import javax .json .bind .config .PropertyNamingStrategy ;
9
16
17
+ import org .slf4j .Logger ;
18
+ import org .slf4j .LoggerFactory ;
19
+
20
+ import com .fasterxml .jackson .databind .JavaType ;
21
+ import com .fasterxml .jackson .databind .ObjectMapper ;
22
+ import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
23
+
10
24
import io .quarkus .runtime .StartupEvent ;
11
25
12
26
/**
16
30
*/
17
31
@ ApplicationScoped
18
32
public class JsonMarshaller {
33
+ public static Logger LOGGER = LoggerFactory .getLogger (JsonMarshaller .class );
19
34
20
35
private Jsonb jsonb ;
21
36
37
+ private ObjectMapper om = new ObjectMapper (new YAMLFactory ());
38
+
22
39
void onStart (@ Observes StartupEvent event ) {
23
40
JsonbConfig config = new JsonbConfig ()
24
41
.withFormatting (true )
25
42
.withPropertyNamingStrategy (PropertyNamingStrategy .LOWER_CASE_WITH_UNDERSCORES );
26
43
jsonb = JsonbBuilder .create (config );
44
+
45
+ }
46
+
47
+ /**
48
+ * Reads a file from the system and returns the contents transformed into a new object. Must be a list. If this fails
49
+ * for any IOException or the file is not readable it will return null
50
+ * @param <T>
51
+ * @param yamlFile The path to the file on disk
52
+ * @param clazz The class that will be return as a list
53
+ * @return A list!
54
+ */
55
+ public <T > List <T > fromYamlFile (String yamlFile , Class <T > clazz ) {
56
+
57
+ Path path = Paths .get (yamlFile );
58
+
59
+ if (Files .isReadable (path )) {
60
+ LOGGER .debug ("Loading config file {}" , yamlFile );
61
+ String yamlFileContent ;
62
+ try {
63
+ yamlFileContent = new String (Files .readAllBytes (path ));
64
+ return fromYaml (yamlFileContent , clazz );
65
+ } catch (IOException e ) {
66
+ LOGGER .error (String .format ("Found but unable to read file %s" , yamlFile ), e );
67
+ }
68
+ }
69
+
70
+ return null ;
27
71
}
28
72
73
+ public <T > List <T > fromYaml (String yamlContent , Class <T > clazz ) {
74
+ JavaType type = om .getTypeFactory ().constructCollectionType (List .class , clazz );
75
+
76
+ try {
77
+ return om .readValue (yamlContent , type );
78
+ } catch (IOException e ) {
79
+ LOGGER .error (String .format ("Found but unable to map file %s" , yamlContent ), e );
80
+ }
81
+
82
+ return null ;
83
+ }
84
+
85
+
29
86
public <T > String toJson (T object ) {
30
87
return jsonb .toJson (object );
31
88
}
@@ -35,5 +92,8 @@ public <T> T fromJson(String json, Class<T> type) {
35
92
return jsonb .fromJson (json , type );
36
93
}
37
94
38
-
95
+ public <T > T fromJson (String json , Type type ) {
96
+ return jsonb .fromJson (json , type );
97
+ }
98
+
39
99
}
0 commit comments