1
2
3
4
5
6 package gov.nist.secauto.metaschema.databind.io.json;
7
8 import com.fasterxml.jackson.core.JsonFactory;
9 import com.fasterxml.jackson.core.JsonParser;
10
11 import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
12 import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
13 import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
14 import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory;
15 import gov.nist.secauto.metaschema.core.model.IBoundObject;
16 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
17 import gov.nist.secauto.metaschema.databind.io.AbstractDeserializer;
18 import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
19 import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
20
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.net.URI;
24
25 import edu.umd.cs.findbugs.annotations.NonNull;
26 import nl.talsmasoftware.lazy4j.Lazy;
27
28
29
30
31
32
33
34
35 public class DefaultJsonDeserializer<CLASS extends IBoundObject>
36 extends AbstractDeserializer<CLASS> {
37 private Lazy<JsonFactory> factory;
38
39
40
41
42
43
44
45
46
47 public DefaultJsonDeserializer(@NonNull IBoundDefinitionModelAssembly definition) {
48 super(definition);
49 resetFactory();
50 }
51
52
53
54
55
56
57 protected final void resetFactory() {
58 this.factory = Lazy.lazy(this::newFactoryInstance);
59 }
60
61 @Override
62 protected void configurationChanged(IMutableConfiguration<DeserializationFeature<?>> config) {
63 super.configurationChanged(config);
64 resetFactory();
65 }
66
67
68
69
70
71
72
73
74
75 @NonNull
76 protected JsonFactory newFactoryInstance() {
77 return JsonFactoryFactory.instance();
78 }
79
80
81
82
83
84
85 @NonNull
86 protected JsonFactory getJsonFactory() {
87 return ObjectUtils.notNull(factory.get());
88 }
89
90
91
92
93
94
95
96
97
98
99
100 @SuppressWarnings("resource")
101 @NonNull
102 protected final JsonParser newJsonParser(@NonNull Reader reader) throws IOException {
103 return ObjectUtils.notNull(getJsonFactory().createParser(reader));
104 }
105
106 @Override
107 protected INodeItem deserializeToNodeItemInternal(@NonNull Reader reader, @NonNull URI documentUri)
108 throws IOException {
109 INodeItem retval;
110 try (JsonParser jsonParser = newJsonParser(reader)) {
111 MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser, documentUri);
112 IBoundDefinitionModelAssembly definition = getDefinition();
113 IConfiguration<DeserializationFeature<?>> configuration = getConfiguration();
114
115 if (definition.isRoot()
116 && configuration.isFeatureEnabled(DeserializationFeature.DESERIALIZE_JSON_ROOT_PROPERTY)) {
117
118 CLASS value = ObjectUtils.requireNonNull(parser.readObjectRoot(
119 definition,
120 ObjectUtils.notNull(definition.getRootJsonName())));
121
122 retval = INodeItemFactory.instance().newDocumentNodeItem(definition, documentUri, value);
123 } else {
124
125 CLASS value = ObjectUtils.asType(parser.readObject(definition));
126
127 retval = INodeItemFactory.instance().newAssemblyNodeItem(definition, documentUri, value);
128 }
129 return retval;
130 }
131 }
132
133 @Override
134 public CLASS deserializeToValueInternal(@NonNull Reader reader, @NonNull URI documentUri) throws IOException {
135 try (JsonParser jsonParser = newJsonParser(reader)) {
136 MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser, documentUri);
137 IBoundDefinitionModelAssembly definition = getDefinition();
138 IConfiguration<DeserializationFeature<?>> configuration = getConfiguration();
139
140 CLASS retval;
141 if (definition.isRoot()
142 && configuration.isFeatureEnabled(DeserializationFeature.DESERIALIZE_JSON_ROOT_PROPERTY)) {
143
144
145 retval = ObjectUtils.requireNonNull(parser.readObjectRoot(
146 definition,
147 ObjectUtils.notNull(definition.getRootJsonName())));
148 } else {
149
150 retval = ObjectUtils.asType(ObjectUtils.requireNonNull(
151 parser.readObject(definition)));
152 }
153 return retval;
154 }
155 }
156 }