001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 *
019 */
020package org.nuxeo.ecm.platform.pictures.tiles.serializer;
021
022import net.sf.json.JSONObject;
023
024import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
025
026/**
027 * JSON serializer for PictureTiles structure
028 *
029 * @author tiry
030 */
031public class JSONPictureTilesSerializer implements PictureTilesSerializer {
032
033    public String serialize(PictureTiles tiles) {
034
035        JSONObject mainMap = new JSONObject();
036        JSONObject tileInfo = new JSONObject();
037        JSONObject orgImgInfo = new JSONObject();
038        JSONObject srcImgInfo = new JSONObject();
039        JSONObject otherInfo = new JSONObject();
040
041        // tileInfo
042        tileInfo.put("maxtiles", tiles.getMaxTiles());
043        tileInfo.put("xtiles", tiles.getXTiles());
044        tileInfo.put("ytiles", tiles.getYTiles());
045        tileInfo.put("tileWidth", tiles.getTilesWidth());
046        tileInfo.put("tileHeight", tiles.getTilesHeight());
047        tileInfo.put("zoom", tiles.getZoomfactor());
048        mainMap.put("tileInfo", tileInfo);
049
050        // orginial Image info
051        orgImgInfo.put("format", tiles.getOriginalImageInfo().getFormat());
052        orgImgInfo.put("width", tiles.getOriginalImageInfo().getWidth());
053        orgImgInfo.put("height", tiles.getOriginalImageInfo().getHeight());
054        mainMap.put("originalImage", orgImgInfo);
055
056        // src Image info
057        srcImgInfo.put("format", tiles.getSourceImageInfo().getFormat());
058        srcImgInfo.put("width", tiles.getSourceImageInfo().getWidth());
059        srcImgInfo.put("height", tiles.getSourceImageInfo().getHeight());
060        mainMap.put("srcImage", srcImgInfo);
061
062        // misc tiler info
063        for (String k : tiles.getInfo().keySet()) {
064            otherInfo.put(k, tiles.getInfo().get(k));
065        }
066        mainMap.put("additionalInfo", otherInfo);
067
068        return mainMap.toString(1);
069    }
070
071}