001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 *
021 */
022package org.nuxeo.ecm.platform.pictures.tiles.serializer;
023
024import net.sf.json.JSONObject;
025
026import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
027
028/**
029 * JSON serializer for PictureTiles structure
030 *
031 * @author tiry
032 */
033public class JSONPictureTilesSerializer implements PictureTilesSerializer {
034
035    public String serialize(PictureTiles tiles) {
036
037        JSONObject mainMap = new JSONObject();
038        JSONObject tileInfo = new JSONObject();
039        JSONObject orgImgInfo = new JSONObject();
040        JSONObject srcImgInfo = new JSONObject();
041        JSONObject otherInfo = new JSONObject();
042
043        // tileInfo
044        tileInfo.put("maxtiles", tiles.getMaxTiles());
045        tileInfo.put("xtiles", tiles.getXTiles());
046        tileInfo.put("ytiles", tiles.getYTiles());
047        tileInfo.put("tileWidth", tiles.getTilesWidth());
048        tileInfo.put("tileHeight", tiles.getTilesHeight());
049        tileInfo.put("zoom", tiles.getZoomfactor());
050        mainMap.put("tileInfo", tileInfo);
051
052        // orginial Image info
053        orgImgInfo.put("format", tiles.getOriginalImageInfo().getFormat());
054        orgImgInfo.put("width", tiles.getOriginalImageInfo().getWidth());
055        orgImgInfo.put("height", tiles.getOriginalImageInfo().getHeight());
056        mainMap.put("originalImage", orgImgInfo);
057
058        // src Image info
059        srcImgInfo.put("format", tiles.getSourceImageInfo().getFormat());
060        srcImgInfo.put("width", tiles.getSourceImageInfo().getWidth());
061        srcImgInfo.put("height", tiles.getSourceImageInfo().getHeight());
062        mainMap.put("srcImage", srcImgInfo);
063
064        // misc tiler info
065        for (String k : tiles.getInfo().keySet()) {
066            otherInfo.put(k, tiles.getInfo().get(k));
067        }
068        mainMap.put("additionalInfo", otherInfo);
069
070        return mainMap.toString(1);
071    }
072
073}