-
Notifications
You must be signed in to change notification settings - Fork 361
/
JsonTypeDescriptor.java
180 lines (156 loc) · 6.37 KB
/
JsonTypeDescriptor.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.vladmihalcea.hibernate.type.json.internal;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
import com.vladmihalcea.hibernate.type.util.Objects;
import com.vladmihalcea.hibernate.type.util.ReflectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.java.JavaXMember;
import org.hibernate.engine.jdbc.BinaryStream;
import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
import org.hibernate.type.descriptor.java.BlobTypeDescriptor;
import org.hibernate.type.descriptor.java.DataHelper;
import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
import org.hibernate.usertype.DynamicParameterizedType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Properties;
/**
* @author Vlad Mihalcea
*/
public class JsonTypeDescriptor
extends AbstractTypeDescriptor<Object> implements DynamicParameterizedType {
private Type type;
private ObjectMapperWrapper objectMapperWrapper;
public JsonTypeDescriptor() {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
protected Object deepCopyNotNull(Object value) {
return ObjectMapperWrapper.INSTANCE.clone(value);
}
});
}
public JsonTypeDescriptor(Type type) {
this();
this.type = type;
}
public JsonTypeDescriptor(final ObjectMapperWrapper objectMapperWrapper) {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
protected Object deepCopyNotNull(Object value) {
return objectMapperWrapper.clone(value);
}
});
this.objectMapperWrapper = objectMapperWrapper;
}
public JsonTypeDescriptor(final ObjectMapperWrapper objectMapperWrapper, Type type) {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
protected Object deepCopyNotNull(Object value) {
return objectMapperWrapper.clone(value);
}
});
this.objectMapperWrapper = objectMapperWrapper;
this.type = type;
}
@Override
public void setParameterValues(Properties parameters) {
final XProperty xProperty = (XProperty) parameters.get(DynamicParameterizedType.XPROPERTY);
if (xProperty instanceof JavaXMember) {
type = ReflectionUtils.invokeGetter(xProperty, "javaType");
} else {
type = ((ParameterType) parameters.get(PARAMETER_TYPE)).getReturnedClass();
}
}
@Override
public boolean areEqual(Object one, Object another) {
if (one == another) {
return true;
}
if (one == null || another == null) {
return false;
}
if (one instanceof String && another instanceof String) {
return one.equals(another);
}
if (one instanceof Collection && another instanceof Collection) {
return Objects.equals(one, another);
}
return objectMapperWrapper.toJsonNode(objectMapperWrapper.toString(one)).equals(
objectMapperWrapper.toJsonNode(objectMapperWrapper.toString(another))
);
}
@Override
public String toString(Object value) {
return objectMapperWrapper.toString(value);
}
@Override
public Object fromString(String string) {
if (String.class.isAssignableFrom(typeToClass())) {
return string;
}
return objectMapperWrapper.fromString(string, type);
}
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(Object value, Class<X> type, WrapperOptions options) {
if (value == null) {
return null;
}
if (String.class.isAssignableFrom(type)) {
return value instanceof String ? (X) value : (X) toString(value);
} else if (BinaryStream.class.isAssignableFrom(type) ||
byte[].class.isAssignableFrom(type)) {
String stringValue = (value instanceof String) ? (String) value : toString(value);
return (X) new BinaryStreamImpl(DataHelper.extractBytes(new ByteArrayInputStream(stringValue.getBytes())));
} else if (Blob.class.isAssignableFrom(type)) {
String stringValue = (value instanceof String) ? (String) value : toString(value);
final Blob blob = BlobTypeDescriptor.INSTANCE.fromString(stringValue);
return (X) blob;
} else if (Object.class.isAssignableFrom(type)) {
String stringValue = (value instanceof String) ? (String) value : toString(value);
return (X) objectMapperWrapper.toJsonNode(stringValue);
}
throw unknownUnwrap(type);
}
@Override
public <X> Object wrap(X value, WrapperOptions options) {
if (value == null) {
return null;
}
Blob blob = null;
if (Blob.class.isAssignableFrom(value.getClass())) {
blob = options.getLobCreator().wrap((Blob) value);
} else if (byte[].class.isAssignableFrom(value.getClass())) {
blob = options.getLobCreator().createBlob((byte[]) value);
} else if (InputStream.class.isAssignableFrom(value.getClass())) {
InputStream inputStream = (InputStream) value;
try {
blob = options.getLobCreator().createBlob(inputStream, inputStream.available());
} catch (IOException e) {
throw unknownWrap(value.getClass());
}
}
String stringValue;
try {
stringValue = (blob != null) ? new String(DataHelper.extractBytes(blob.getBinaryStream())) : value.toString();
} catch (SQLException e) {
throw new HibernateException("Unable to extract binary stream from Blob", e);
}
return fromString(stringValue);
}
private Class typeToClass() {
Type classType = type;
if(type instanceof ParameterizedType) {
classType = ((ParameterizedType) type).getRawType();
}
return (Class) classType;
}
}