mirror of https://github.com/apache/cloudstack.git
78 lines
3.0 KiB
Java
78 lines
3.0 KiB
Java
// Copyright 2012 Citrix Systems, Inc. Licensed under the
|
|
// Apache License, Version 2.0 (the "License"); you may not use this
|
|
// file except in compliance with the License. Citrix Systems, Inc.
|
|
// reserves all rights not expressly granted by the License.
|
|
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// Automatically generated by addcopyright.py at 04/03/2012
|
|
package com.cloud.api;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
public class ApiSerializerHelper {
|
|
public static final Logger s_logger = Logger.getLogger(ApiSerializerHelper.class.getName());
|
|
public static String token = "/";
|
|
|
|
public static String toSerializedStringOld(Object result) {
|
|
if (result != null) {
|
|
Class<?> clz = result.getClass();
|
|
Gson gson = ApiGsonHelper.getBuilder().create();
|
|
|
|
if (result instanceof ResponseObject) {
|
|
return clz.getName() + token + ((ResponseObject) result).getObjectName() + token + gson.toJson(result);
|
|
} else {
|
|
return clz.getName() + token + gson.toJson(result);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Object fromSerializedString(String result) {
|
|
try {
|
|
if (result != null && !result.isEmpty()) {
|
|
|
|
String[] serializedParts = result.split(token);
|
|
|
|
if (serializedParts.length < 2) {
|
|
return null;
|
|
}
|
|
String clzName = serializedParts[0];
|
|
String nameField = null;
|
|
String content = null;
|
|
if (serializedParts.length == 2) {
|
|
content = serializedParts[1];
|
|
} else {
|
|
nameField = serializedParts[1];
|
|
int index = result.indexOf(token + nameField + token);
|
|
content = result.substring(index + nameField.length() + 2);
|
|
}
|
|
|
|
Class<?> clz;
|
|
try {
|
|
clz = Class.forName(clzName);
|
|
} catch (ClassNotFoundException e) {
|
|
return null;
|
|
}
|
|
|
|
Gson gson = ApiGsonHelper.getBuilder().create();
|
|
Object obj = gson.fromJson(content, clz);
|
|
if (nameField != null) {
|
|
((ResponseObject) obj).setObjectName(nameField);
|
|
}
|
|
return obj;
|
|
}
|
|
return null;
|
|
} catch (RuntimeException e) {
|
|
s_logger.error("Caught runtime exception when doing GSON deserialization on: " + result);
|
|
throw e;
|
|
}
|
|
}
|
|
}
|