1
1
/*
2
- * Copyright 2017 hbz
2
+ * Copyright 2017, 2021 hbz
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 the "License";
5
5
* you may not use this file except in compliance with the License.
17
17
18
18
import com .fasterxml .jackson .core .JsonFactory ;
19
19
import com .fasterxml .jackson .core .JsonParser ;
20
+ import com .fasterxml .jackson .core .JsonProcessingException ;
20
21
import com .fasterxml .jackson .core .JsonToken ;
22
+ import com .fasterxml .jackson .databind .ObjectMapper ;
23
+ import com .jayway .jsonpath .JsonPath ;
24
+
25
+ import org .metafacture .framework .FluxCommand ;
21
26
import org .metafacture .framework .MetafactureException ;
22
27
import org .metafacture .framework .StreamReceiver ;
28
+ import org .metafacture .framework .annotations .Description ;
29
+ import org .metafacture .framework .annotations .In ;
30
+ import org .metafacture .framework .annotations .Out ;
23
31
import org .metafacture .framework .helpers .DefaultObjectPipe ;
24
32
25
33
import java .io .IOException ;
34
+ import java .util .Arrays ;
35
+ import java .util .List ;
36
+ import java .util .stream .Collectors ;
26
37
27
38
/**
28
39
* Decodes a record in JSON format.
29
40
*
30
41
* @author Jens Wille
31
42
*
32
43
*/
44
+ @ Description ("Decodes JSON to metadata events. The \' recordPath\' option can be used to set a JsonPath "
45
+ + "to extract a path as JSON - or to split the data into multiple JSON documents." )
46
+ @ In (String .class )
47
+ @ Out (StreamReceiver .class )
48
+ @ FluxCommand ("decode-json" )
33
49
public final class JsonDecoder extends DefaultObjectPipe <String , StreamReceiver > {
34
50
35
51
public static final String DEFAULT_ARRAY_MARKER = JsonEncoder .ARRAY_MARKER ;
@@ -38,6 +54,8 @@ public final class JsonDecoder extends DefaultObjectPipe<String, StreamReceiver>
38
54
39
55
public static final String DEFAULT_RECORD_ID = "%d" ;
40
56
57
+ public static final String DEFAULT_ROOT_PATH = "" ;
58
+
41
59
private final JsonFactory jsonFactory = new JsonFactory ();
42
60
43
61
private JsonParser jsonParser ;
@@ -46,12 +64,15 @@ public final class JsonDecoder extends DefaultObjectPipe<String, StreamReceiver>
46
64
private String recordId ;
47
65
private int recordCount ;
48
66
67
+ private String recordPath ;
68
+
49
69
public JsonDecoder () {
50
70
super ();
51
71
52
72
setArrayMarker (DEFAULT_ARRAY_MARKER );
53
73
setArrayName (DEFAULT_ARRAY_NAME );
54
74
setRecordId (DEFAULT_RECORD_ID );
75
+ setRecordPath (DEFAULT_ROOT_PATH );
55
76
56
77
resetRecordCount ();
57
78
}
@@ -96,25 +117,45 @@ public int getRecordCount() {
96
117
return recordCount ;
97
118
}
98
119
120
+ public void setRecordPath (final String recordPath ) {
121
+ this .recordPath = recordPath ;
122
+ }
123
+
124
+ public String getRecordPath () {
125
+ return recordPath ;
126
+ }
127
+
99
128
public void resetRecordCount () {
100
129
setRecordCount (0 );
101
130
}
102
131
103
132
@ Override
104
- public void process (final String string ) {
133
+ public void process (final String json ) {
105
134
assert !isClosed ();
106
-
107
- createParser (string );
108
-
109
- try {
110
- decode ();
111
- }
112
- catch (final IOException e ) {
113
- throw new MetafactureException (e );
114
- }
115
- finally {
116
- closeParser ();
117
- }
135
+ final List <String > records = recordPath .isEmpty () ? Arrays .asList (json )
136
+ : matches (JsonPath .read (json , recordPath ));
137
+ records .forEach (record -> {
138
+ createParser (record );
139
+ try {
140
+ decode ();
141
+ } catch (final IOException e ) {
142
+ throw new MetafactureException (e );
143
+ } finally {
144
+ closeParser ();
145
+ }
146
+ });
147
+ }
148
+
149
+ private List <String > matches (Object obj ) {
150
+ final List <?> records = (obj instanceof List <?>) ? ((List <?>) obj ) : Arrays .asList (obj );
151
+ return records .stream ().map (doc -> {
152
+ try {
153
+ return new ObjectMapper ().writeValueAsString (doc );
154
+ } catch (JsonProcessingException e ) {
155
+ e .printStackTrace ();
156
+ return doc .toString ();
157
+ }
158
+ }).collect (Collectors .toList ());
118
159
}
119
160
120
161
@ Override
0 commit comments