下面列出了怎么用org.apache.commons.lang3.SerializationException的API类实例代码及写法,或者点击链接到github查看源代码。
public SerializeUIView() {
Div label = new Div();
label.setId("message");
NativeButton button = createButton("Serialize", "serialize", event -> {
UI ui = UI.getCurrent();
try {
byte[] serialize = SerializationUtils.serialize(ui);
String result = serialize.length > 0 ?
"Successfully serialized ui" :
"Serialization failed";
label.setText(result);
}catch(SerializationException se) {
label.setText(se.getMessage());
}
});
add(label, button);
}
public Object deserialize(byte[] bytes) {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
/**
* Deserialize a {@link LoadProfileEstimator} according to {@link #serialize(LoadProfileEstimator, JSONArray)}.
*
* @param jsonObject that should be deserialized
* @return the {@link LoadProfileEstimator}
*/
private LoadProfileEstimator deserializeEstimator(JSONObject jsonObject) {
if (jsonObject.has("key")) {
final String key = jsonObject.getString("key");
final LoadProfileEstimator estimator = LoadProfileEstimators.createFromSpecification(key, this.configuration);
if (estimator == null) {
throw new SerializationException("Could not create estimator for key " + key);
}
return estimator;
} else if (jsonObject.has("load")) {
final LoadProfile load = JsonSerializables.deserialize(jsonObject.getJSONObject("load"), LoadProfile.class);
return new ConstantLoadProfileEstimator(load);
}
throw new SerializationException(String.format("Cannot deserialize load estimator from %s.", jsonObject));
}
/**
* Deserializes an object.
*
* @param json that should be serialized
* @return the deserialized object
*/
@SuppressWarnings("unchecked")
default T deserialize(JSONObject json) {
if (JsonSerializables.isJsonNull(json)) return null;
try {
final Class<?> classTag = JsonSerializables.getClassTag(json);
if (classTag == null) {
throw new IllegalArgumentException(String.format("Cannot determine class from %s.", json));
}
return this.deserialize(json, (Class<? extends T>) classTag);
} catch (ClassNotFoundException e) {
throw new SerializationException("Could not load class.", e);
}
}
/**
* Performs a deep copy of the given object. This method is preferable to
* the more general version because that relies on the object having a
* default (zero-argument) constructor; however, this method only works for
* serializable objects.
*
* @param <T> the type of the object to copy and return
* @param original the object to copy
* @return a deep copy of the given object
*/
public static <T extends Serializable> T deepCopy(T original) {
if (original == null) {
return null;
}
try {
return SerializationUtils.clone(original);
} catch (SerializationException notSerializable) {
return (T) deepCopy((Object) original);
}
}
public static Revision fromString(String scopedName) {
String[] tokens = scopedName.split(":");
if (tokens.length == 3) {
return new RevisionImpl(Segment.fromScopedName(tokens[0]), Long.parseLong(tokens[1]), Integer.parseInt(tokens[2]));
} else {
throw new SerializationException("Not a valid segment name: " + scopedName);
}
}
public static <T extends Serializable> T clone(T obj){
try {
return SerializationUtils.clone(obj);
}catch (SerializationException e){
logger.error("[clone]", e);
throw e;
}
}
@Test
/**
* -Dlog4j.configurationFile=log4j2cat.xml
*/
public void testException() throws IOException {
logger.error("[testException]", new SerializationException("exception"));
waitForAnyKeyToExit();
}
@Override
public byte[] serialize(Spec spec) throws SpecSerDeException {
try {
return SerializationUtils.serialize(spec);
} catch (SerializationException e) {
throw new SpecSerDeException(spec, e);
}
}
@Override
public Spec deserialize(byte[] spec) throws SpecSerDeException {
try {
return SerializationUtils.deserialize(spec);
} catch (SerializationException e) {
throw new SpecSerDeException(e);
}
}
@Override
public void serialize(final String value, final OutputStream output) throws SerializationException, IOException {
output.write(value.getBytes(StandardCharsets.UTF_8));
}
@Test
void deserializeShouldThrowWhenNotBase64StringProvided() {
assertThatExceptionOfType(SerializationException.class)
.isThrownBy(() -> deserialize("abc"));
}
@Test
void deserializeShouldThrowWhenNotSerializedBytesAreEncodedInBase64() {
assertThatExceptionOfType(SerializationException.class)
.isThrownBy(() -> deserialize(Base64.getEncoder().encodeToString("abc".getBytes(StandardCharsets.UTF_8))));
}
@Override
public void serialize(final String value, final OutputStream output) throws SerializationException, IOException {
output.write(value.getBytes(StandardCharsets.UTF_8));
}
/**
* Deserialize a given JSON datatype. The following cases are supported:
* <ul>
* <li>{@code json} is a (JSON) {@code null} value;</li>
* <li>{@code json} is a basic (JSON) datatype;</li>
* <li>{@code json} is a {@link Class}-tagged {@link JSONObject} that corresponds to a {@link JsonSerializable};</li>
* <li>{@code json} is a {@link JSONArray} with {@link Class}-tagged {@link JSONObject}s that correspond to a
* {@link JsonSerializable}s - in this case, the result type is a {@link List}.</li>
* </ul>
*
* @param json the JSON data
* @return the deserialization result
*/
public static Object deserialize(Object json) {
if (isJsonNull(json)) return null;
else if (isUnconvertedInstance(json)) return json;
else if (json instanceof JSONObject) return deserialize((JSONObject) json);
else if (json instanceof JSONArray) return deserializeAllAsList((JSONArray) json);
throw new SerializationException(String.format("Don't know how to deserialize %s.", json));
}