|
5 | 5 | package org.hibernate.type.descriptor.jdbc; |
6 | 6 |
|
7 | 7 |
|
8 | | -import java.io.IOException; |
9 | | -import java.lang.reflect.Array; |
10 | 8 | import java.sql.SQLException; |
11 | 9 | import java.util.AbstractCollection; |
12 | 10 | import java.util.ArrayList; |
|
24 | 22 | import org.hibernate.internal.util.collections.StandardStack; |
25 | 23 | import org.hibernate.metamodel.mapping.EmbeddableMappingType; |
26 | 24 | import org.hibernate.metamodel.mapping.JdbcMapping; |
27 | | -import org.hibernate.metamodel.mapping.MappingType; |
28 | 25 | import org.hibernate.metamodel.mapping.SelectableMapping; |
29 | | -import org.hibernate.metamodel.mapping.ValuedModelPart; |
30 | | -import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping; |
31 | 26 | import org.hibernate.type.BasicPluralType; |
32 | | -import org.hibernate.type.BasicType; |
33 | | -import org.hibernate.type.SqlTypes; |
34 | 27 | import org.hibernate.type.descriptor.WrapperOptions; |
35 | 28 | import org.hibernate.type.descriptor.java.BasicPluralJavaType; |
36 | 29 | import org.hibernate.type.descriptor.java.JavaType; |
37 | 30 | import org.hibernate.type.format.JsonDocumentItemType; |
38 | 31 | import org.hibernate.type.format.JsonDocumentReader; |
39 | | -import org.hibernate.type.format.JsonDocumentWriter; |
40 | | -import static org.hibernate.type.descriptor.jdbc.StructHelper.getEmbeddedPart; |
| 32 | + |
41 | 33 | import static org.hibernate.type.descriptor.jdbc.StructHelper.instantiate; |
42 | 34 | import org.hibernate.type.format.JsonValueJDBCTypeAdapter; |
43 | 35 | import org.hibernate.type.format.JsonValueJDBCTypeAdapterFactory; |
|
52 | 44 | @Internal |
53 | 45 | public class JsonHelper { |
54 | 46 |
|
55 | | - /** |
56 | | - * Serializes an array of values into JSON object/array |
57 | | - * @param elementMappingType the type definitions |
58 | | - * @param values the values to be serialized |
59 | | - * @param options wrapping options |
60 | | - * @param writer the document writer used for serialization |
61 | | - */ |
62 | | - public static void serializeArray(MappingType elementMappingType, Object[] values, WrapperOptions options, JsonDocumentWriter writer) { |
63 | | - writer.startArray(); |
64 | | - if ( values.length == 0 ) { |
65 | | - writer.endArray(); |
66 | | - return; |
67 | | - } |
68 | | - for ( Object value : values ) { |
69 | | - try { |
70 | | - serialize(elementMappingType, value, options, writer); |
71 | | - } |
72 | | - catch (IOException e) { |
73 | | - throw new IllegalArgumentException( "Could not serialize JSON array value" , e ); |
74 | | - } |
75 | | - } |
76 | | - writer.endArray(); |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Serializes an array of values into JSON object/array |
81 | | - * @param elementJavaType the array element type |
82 | | - * @param elementJdbcType the JDBC type |
83 | | - * @param values values to be serialized |
84 | | - * @param options wrapping options |
85 | | - * @param writer the document writer used for serialization |
86 | | - */ |
87 | | - public static void serializeArray(JavaType<?> elementJavaType, JdbcType elementJdbcType, Object[] values, WrapperOptions options, JsonDocumentWriter writer) { |
88 | | - writer.startArray(); |
89 | | - if ( values.length == 0 ) { |
90 | | - writer.endArray(); |
91 | | - return; |
92 | | - } |
93 | | - for ( Object value : values ) { |
94 | | - if (value == null) { |
95 | | - writer.nullValue(); |
96 | | - } |
97 | | - else { |
98 | | - writer.serializeJsonValue( value ,(JavaType<?>) elementJavaType,elementJdbcType,options); |
99 | | - } |
100 | | - } |
101 | | - writer.endArray(); |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * Checks that a <code>JDBCType</code> is assignable to an array |
106 | | - * @param type the jdbc type |
107 | | - * @return <code>true</code> if types is of array kind <code>false</code> otherwise. |
108 | | - */ |
109 | | - private static boolean isArrayType(JdbcType type) { |
110 | | - return (type.getDefaultSqlTypeCode() == SqlTypes.ARRAY || |
111 | | - type.getDefaultSqlTypeCode() == SqlTypes.JSON_ARRAY); |
112 | | - } |
113 | | - |
114 | | - /** |
115 | | - * Serialized an Object value to JSON object using a document writer. |
116 | | - * |
117 | | - * @param embeddableMappingType the embeddable mapping definition of the given value. |
118 | | - * @param domainValue the value to be serialized. |
119 | | - * @param options wrapping options |
120 | | - * @param writer the document writer |
121 | | - * @throws IOException if the underlying writer failed to serialize a mpped value or failed to perform need I/O. |
122 | | - */ |
123 | | - public static void serialize(EmbeddableMappingType embeddableMappingType, |
124 | | - Object domainValue, WrapperOptions options, JsonDocumentWriter writer) throws IOException { |
125 | | - writer.startObject(); |
126 | | - serializeMapping(embeddableMappingType, domainValue, options, writer); |
127 | | - writer.endObject(); |
128 | | - } |
129 | | - |
130 | | - private static void serialize(MappingType mappedType, Object value, WrapperOptions options, JsonDocumentWriter writer) |
131 | | - throws IOException { |
132 | | - if ( value == null ) { |
133 | | - writer.nullValue(); |
134 | | - } |
135 | | - else if ( mappedType instanceof EmbeddableMappingType ) { |
136 | | - serialize( (EmbeddableMappingType) mappedType, value, options, writer ); |
137 | | - } |
138 | | - else if ( mappedType instanceof BasicType<?> basicType) { |
139 | | - if ( isArrayType(basicType.getJdbcType())) { |
140 | | - final int length = Array.getLength( value ); |
141 | | - writer.startArray(); |
142 | | - if ( length != 0 ) { |
143 | | - final JavaType<Object> elementJavaType = ( (BasicPluralJavaType<Object>) basicType.getJdbcJavaType() ).getElementJavaType(); |
144 | | - final JdbcType elementJdbcType = ( (ArrayJdbcType) basicType.getJdbcType() ).getElementJdbcType(); |
145 | | - final Object domainArray = basicType.convertToRelationalValue( value ); |
146 | | - for ( int j = 0; j < length; j++ ) { |
147 | | - writer.serializeJsonValue(Array.get(domainArray,j), elementJavaType, elementJdbcType, options); |
148 | | - } |
149 | | - } |
150 | | - writer.endArray(); |
151 | | - } |
152 | | - else { |
153 | | - writer.serializeJsonValue(basicType.convertToRelationalValue( value), |
154 | | - (JavaType<Object>)basicType.getJdbcJavaType(),basicType.getJdbcType(), options); |
155 | | - } |
156 | | - } |
157 | | - else { |
158 | | - throw new UnsupportedOperationException( "Support for mapping type not yet implemented: " + mappedType.getClass().getName() ); |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - /** |
163 | | - * JSON object attirbute serialization |
164 | | - * @see #serialize(EmbeddableMappingType, Object, WrapperOptions, JsonDocumentWriter) |
165 | | - * @param embeddableMappingType the embeddable mapping definition of the given value. |
166 | | - * @param domainValue the value to be serialized. |
167 | | - * @param options wrapping options |
168 | | - * @param writer the document writer |
169 | | - * @throws IOException if an error occurred while writing to an underlying writer |
170 | | - */ |
171 | | - private static void serializeMapping(EmbeddableMappingType embeddableMappingType, |
172 | | - Object domainValue, WrapperOptions options, JsonDocumentWriter writer) throws IOException { |
173 | | - final Object[] values = embeddableMappingType.getValues( domainValue ); |
174 | | - for ( int i = 0; i < values.length; i++ ) { |
175 | | - final ValuedModelPart attributeMapping = getEmbeddedPart( embeddableMappingType, i ); |
176 | | - if ( attributeMapping instanceof SelectableMapping ) { |
177 | | - final String name = ( (SelectableMapping) attributeMapping ).getSelectableName(); |
178 | | - writer.objectKey( name ); |
179 | | - |
180 | | - if ( attributeMapping.getMappedType() instanceof EmbeddableMappingType ) { |
181 | | - writer.startObject(); |
182 | | - serializeMapping( (EmbeddableMappingType)attributeMapping.getMappedType(), values[i], options,writer); |
183 | | - writer.endObject(); |
184 | | - } |
185 | | - else { |
186 | | - serialize(attributeMapping.getMappedType(), values[i], options, writer); |
187 | | - } |
188 | | - |
189 | | - } |
190 | | - else if ( attributeMapping instanceof EmbeddedAttributeMapping ) { |
191 | | - if ( values[i] == null ) { |
192 | | - continue; |
193 | | - } |
194 | | - final EmbeddableMappingType mappingType = (EmbeddableMappingType) attributeMapping.getMappedType(); |
195 | | - final SelectableMapping aggregateMapping = mappingType.getAggregateMapping(); |
196 | | - if (aggregateMapping == null) { |
197 | | - serializeMapping( |
198 | | - mappingType, |
199 | | - values[i], |
200 | | - options, |
201 | | - writer ); |
202 | | - } |
203 | | - else { |
204 | | - final String name = aggregateMapping.getSelectableName(); |
205 | | - writer.objectKey( name ); |
206 | | - writer.startObject(); |
207 | | - serializeMapping( |
208 | | - mappingType, |
209 | | - values[i], |
210 | | - options, |
211 | | - writer); |
212 | | - writer.endObject(); |
213 | | - |
214 | | - } |
215 | | - } |
216 | | - else { |
217 | | - throw new UnsupportedOperationException( "Support for attribute mapping type not yet implemented: " + attributeMapping.getClass().getName() ); |
218 | | - } |
219 | | - |
220 | | - } |
221 | | - } |
222 | | - |
223 | 47 | /** |
224 | 48 | * Consumes Json document items from a document reader and return the serialized Objects |
225 | 49 | * @param reader the document reader |
226 | 50 | * @param embeddableMappingType the type definitions |
227 | 51 | * @param returnEmbeddable do we return an Embeddable object or array of Objects ? |
228 | 52 | * @param options wrapping options |
229 | 53 | * @return serialized values |
230 | | - * @param <X> |
| 54 | + * @param <X> the type of the returned value |
231 | 55 | * @throws SQLException if error occured during mapping of types |
232 | 56 | */ |
233 | 57 | private static <X> X consumeJsonDocumentItems(JsonDocumentReader reader, EmbeddableMappingType embeddableMappingType, boolean returnEmbeddable, WrapperOptions options) |
|
0 commit comments