-
Notifications
You must be signed in to change notification settings - Fork 3
/
Example.java
143 lines (129 loc) · 5.96 KB
/
Example.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* JB4JSON-LD Jackson
* Copyright (C) 2023 Czech Technical University in Prague
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package cz.cvut.kbss.jsonld.jackson.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import cz.cvut.kbss.jsonld.jackson.JsonLdModule;
import cz.cvut.kbss.jsonld.jackson.example.model.Organization;
import cz.cvut.kbss.jsonld.jackson.example.model.User;
import cz.cvut.kbss.jsonld.jackson.serialization.SerializationConstants;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
public class Example {
private final ObjectMapper objectMapper;
public static void main(String[] args) throws Exception {
final Example ex = new Example();
if (args.length < 1) {
System.err.println("Provide at least 1 argument to determine what the demo should do.\n\n");
printHelp();
return;
}
final String type = args[0].toLowerCase();
switch (type) {
case "su":
ex.runSerializeSingleUser();
break;
case "slu":
ex.runSerializeListOfUsers();
break;
case "so":
ex.runSerializeSingleOrganization();
break;
case "du":
ex.runDeserializeSingleUser(loadDeserializationInput(args, "singleUser.json"));
break;
case "do":
ex.runDeserializeSingleOrganization(loadDeserializationInput(args, "singleOrganization.json"));
break;
default:
System.err.println("Unsupported option '" + type + "'.\n\n");
printHelp();
break;
}
}
private Example() {
this.objectMapper = initObjectMapper();
}
private ObjectMapper initObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
// Include only non-null values in serialization
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Pretty print serialization output
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// Ignore unknown properties in deserialization input
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Here we create the JSON-LD serialization/deserialization module
final JsonLdModule module = new JsonLdModule();
// Select the serialization output form
module.configure(SerializationConstants.FORM, SerializationConstants.FORM_COMPACT_WITH_CONTEXT);
// Register the module
objectMapper.registerModule(module);
return objectMapper;
}
private static void printHelp() {
System.out.println("Use one of the following values as program argument:");
System.out.println("1. 'slu' - serialize a list of User objects.");
System.out.println("2. 'su' - serialize a single User instance.");
System.out.println("3. 'so' - serialize a single Organization referencing a list of member Users.");
System.out.println("4. 'du' - deserialize a single User, optionally provide path to an input file as the second argument.");
System.out.println("5. 'do' - deserialize a single Organization, optionally provide path to an input file as the second argument.");
}
private static String loadDeserializationInput(String[] args, String fallback) throws IOException {
if (args.length > 1) {
final String path = args[1];
final File inputFile = new File(path);
return String.join("\n", Files.readAllLines(inputFile.toPath(), StandardCharsets.UTF_8));
} else {
final InputStream is = Example.class.getClassLoader().getResourceAsStream(fallback);
assert is != null;
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
final StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}
private void runSerializeListOfUsers() throws IOException {
final List<User> users =
Arrays.asList(Generator.generateUser(), Generator.generateUser(), Generator.generateUser());
System.out.println(objectMapper.writeValueAsString(users));
}
private void runSerializeSingleUser() throws IOException {
final User user = Generator.generateUser();
System.out.println(objectMapper.writeValueAsString(user));
}
private void runSerializeSingleOrganization() throws IOException {
final Organization organization = Generator.generateOrganization();
System.out.println(objectMapper.writeValueAsString(organization));
}
private void runDeserializeSingleUser(String input) throws IOException {
final User result = objectMapper.readValue(input, User.class);
System.out.println(result);
}
private void runDeserializeSingleOrganization(String input) throws IOException {
final Organization result = objectMapper.readValue(input, Organization.class);
System.out.println(result);
}
}