blob: 9858aefb4131a2b58814a3bbb84011d3df94944b [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with 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.
*/
package org.apache.s4.comm.topology;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
* Utility to serialize/deserialize data in ZK. <br/>
* Using Json format and Gson library. TODO: Explore other libraries like jackson much richer features. Gson needs
* no-arg constructor to work with without additional work
*/
public class ZNRecordSerializer implements ZkSerializer {
@Override
public byte[] serialize(Object data) throws ZkMarshallingError {
if (data != null) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(data);
if (json != null) {
return json.getBytes();
}
}
return new byte[0];
}
@Override
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
ZNRecord record = null;
if (bytes != null) {
Gson gson = new Gson();
record = gson.fromJson(new String(bytes), ZNRecord.class);
}
return record;
}
}