17
17
import cz .cvut .kbss .jsonld .deserialization .InstanceBuilder ;
18
18
import cz .cvut .kbss .jsonld .deserialization .util .DataTypeTransformer ;
19
19
import cz .cvut .kbss .jsonld .deserialization .util .LangString ;
20
+ import cz .cvut .kbss .jsonld .deserialization .util .ValueUtils ;
20
21
import cz .cvut .kbss .jsonld .deserialization .util .XSDTypeCoercer ;
21
22
import cz .cvut .kbss .jsonld .exception .MissingIdentifierException ;
23
+ import jakarta .json .JsonArray ;
24
+ import jakarta .json .JsonObject ;
25
+ import jakarta .json .JsonValue ;
22
26
23
27
import java .net .URI ;
24
- import java .util .List ;
25
- import java .util .Map ;
26
28
27
- class CollectionDeserializer extends Deserializer <List <?> > {
29
+ class CollectionDeserializer extends Deserializer <JsonArray > {
28
30
29
31
private final String property ;
30
32
@@ -34,58 +36,58 @@ class CollectionDeserializer extends Deserializer<List<?>> {
34
36
}
35
37
36
38
@ Override
37
- void processValue (List <?> value ) {
38
- if (value .size () == 1 && value .get (0 ) instanceof Map ) {
39
- final Map <?, ?> map = ( Map <?, ?>) value .get (0 );
39
+ void processValue (JsonArray value ) {
40
+ if (value .size () == 1 && value .get (0 ). getValueType () == JsonValue . ValueType . OBJECT ) {
41
+ final JsonObject obj = value .getJsonObject (0 );
40
42
if (!instanceBuilder .isPlural (property )) {
41
- resolvePropertyValue (map );
43
+ resolvePropertyValue (obj );
42
44
return ;
43
45
}
44
- if (map .size () == 1 && map .containsKey (JsonLd .LIST )) {
45
- assert map .get (JsonLd .LIST ) instanceof List ;
46
- processValue (( List <?>) map . get (JsonLd .LIST ));
46
+ if (obj .size () == 1 && obj .containsKey (JsonLd .LIST )) {
47
+ assert obj .get (JsonLd .LIST ). getValueType () == JsonValue . ValueType . ARRAY ;
48
+ processValue (obj . getJsonArray (JsonLd .LIST ));
47
49
return ;
48
50
}
49
51
}
50
52
instanceBuilder .openCollection (property );
51
- for (Object item : value ) {
52
- if (item instanceof Map ) {
53
- resolveValue (( Map <?, ?>) item );
53
+ for (JsonValue item : value ) {
54
+ if (item . getValueType () == JsonValue . ValueType . OBJECT ) {
55
+ resolveValue (item . asJsonObject () );
54
56
} else {
55
- instanceBuilder .addValue (item );
57
+ instanceBuilder .addValue (ValueUtils . literalValue ( item ) );
56
58
}
57
59
}
58
60
instanceBuilder .closeCollection ();
59
61
}
60
62
61
- private void resolveValue (Map <?, ?> value ) {
63
+ private void resolveValue (JsonObject value ) {
62
64
final Class <?> targetType = instanceBuilder .getCurrentCollectionElementType ();
63
65
if (config .getDeserializers ().hasCustomDeserializer (targetType )) {
64
66
instanceBuilder .addValue (deserializeUsingCustomDeserializer (targetType , value ));
65
67
} else if (value .size () == 1 && value .containsKey (JsonLd .VALUE )) {
66
- instanceBuilder .addValue (value . get ( JsonLd . VALUE ));
68
+ instanceBuilder .addValue (ValueUtils . literalValue ( ValueUtils . getValue ( value ) ));
67
69
} else if (value .size () == 1 && value .containsKey (JsonLd .ID )) {
68
70
handleReferenceNodeInCollection (value , targetType );
69
71
} else if (value .containsKey (JsonLd .LANGUAGE )) {
70
72
assert value .containsKey (JsonLd .VALUE );
71
73
instanceBuilder .addValue (
72
- new LangString (value . get ( JsonLd . VALUE ). toString (), value .get (JsonLd .LANGUAGE ). toString ( )));
74
+ new LangString (ValueUtils . stringValue ( ValueUtils . getValue ( value )), ValueUtils . stringValue ( value .get (JsonLd .LANGUAGE ))));
73
75
} else if (instanceBuilder .isCurrentCollectionProperties ()) {
74
76
// If we are deserializing an object into @Properties, just extract the identifier and put it into the map
75
77
if (!value .containsKey (JsonLd .ID )) {
76
78
throw new MissingIdentifierException (
77
79
"Cannot put an object without an identifier into @Properties. Object: " + value );
78
80
}
79
- instanceBuilder .addValue (URI .create (value .get (JsonLd .ID ). toString ( )));
81
+ instanceBuilder .addValue (URI .create (ValueUtils . stringValue ( value .get (JsonLd .ID ))));
80
82
} else {
81
83
final Class <?> elementType = instanceBuilder .getCurrentCollectionElementType ();
82
84
new ObjectDeserializer (instanceBuilder , config , elementType ).processValue (value );
83
85
}
84
86
}
85
87
86
- private void handleReferenceNodeInCollection (Map <?, ?> value , Class <?> targetType ) {
88
+ private void handleReferenceNodeInCollection (JsonObject value , Class <?> targetType ) {
87
89
assert value .size () == 1 && value .containsKey (JsonLd .ID );
88
- final String identifier = value .get (JsonLd .ID ). toString ( );
90
+ final String identifier = ValueUtils . stringValue ( value .get (JsonLd .ID ));
89
91
if (targetType .isEnum ()) {
90
92
instanceBuilder .addValue (DataTypeTransformer .transformIndividualToEnumConstant (identifier ,
91
93
(Class <? extends Enum >) targetType ));
@@ -94,7 +96,7 @@ private void handleReferenceNodeInCollection(Map<?, ?> value, Class<?> targetTyp
94
96
}
95
97
}
96
98
97
- private void resolvePropertyValue (Map <?, ?> value ) {
99
+ private void resolvePropertyValue (JsonObject value ) {
98
100
final Class <?> targetType = instanceBuilder .getTargetType (property );
99
101
if (config .getDeserializers ().hasCustomDeserializer (targetType )) {
100
102
instanceBuilder .addValue (property , deserializeUsingCustomDeserializer (targetType , value ));
@@ -109,27 +111,27 @@ private void resolvePropertyValue(Map<?, ?> value) {
109
111
}
110
112
}
111
113
112
- private <T > T deserializeUsingCustomDeserializer (Class <T > targetType , Map <?, ?> value ) {
114
+ private <T > T deserializeUsingCustomDeserializer (Class <T > targetType , JsonObject value ) {
113
115
final DeserializationContext <T > ctx = new DeserializationContext <>(targetType , config .getTargetResolver ());
114
116
assert config .getDeserializers ().getDeserializer (ctx ).isPresent ();
115
117
return config .getDeserializers ().getDeserializer (ctx ).get ().deserialize (value , ctx );
116
118
}
117
119
118
- private void extractLiteralValue (Map <?, ?> value ) {
119
- final Object val = value .get (JsonLd .VALUE );
120
+ private void extractLiteralValue (JsonObject value ) {
121
+ final JsonValue val = value .get (JsonLd .VALUE );
120
122
if (value .containsKey (JsonLd .TYPE )) {
121
123
instanceBuilder
122
- .addValue (property , XSDTypeCoercer .coerceType (val . toString ( ), value .get (JsonLd .TYPE ). toString ( )));
124
+ .addValue (property , XSDTypeCoercer .coerceType (ValueUtils . stringValue ( val ), ValueUtils . stringValue ( value .get (JsonLd .TYPE ))));
123
125
} else if (value .containsKey (JsonLd .LANGUAGE )) {
124
- instanceBuilder .addValue (property , new LangString (val . toString ( ), value .get (JsonLd .LANGUAGE ). toString ( )));
126
+ instanceBuilder .addValue (property , new LangString (ValueUtils . stringValue ( val ), ValueUtils . stringValue ( value .get (JsonLd .LANGUAGE ))));
125
127
} else {
126
- instanceBuilder .addValue (property , val );
128
+ instanceBuilder .addValue (property , ValueUtils . literalValue ( val ) );
127
129
}
128
130
}
129
131
130
- private void handleSingularReferenceNode (Map <?, ?> value , Class <?> targetType ) {
132
+ private void handleSingularReferenceNode (JsonObject value , Class <?> targetType ) {
131
133
assert value .size () == 1 && value .containsKey (JsonLd .ID );
132
- final String identifier = value .get (JsonLd .ID ). toString ( );
134
+ final String identifier = ValueUtils . stringValue ( value .get (JsonLd .ID ));
133
135
if (targetType .isEnum ()) {
134
136
instanceBuilder .addValue (property , DataTypeTransformer .transformIndividualToEnumConstant (identifier ,
135
137
(Class <? extends Enum >) targetType ));
0 commit comments