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 java.util.LinkedHashMap;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
029
030import com.fasterxml.jackson.core.JsonProcessingException;
031import com.fasterxml.jackson.databind.ObjectMapper;
032
033/**
034 * JSON serializer for PictureTiles structure
035 *
036 * @author tiry
037 */
038public class JSONPictureTilesSerializer implements PictureTilesSerializer {
039
040    @Override
041    public String serialize(PictureTiles tiles) {
042
043        Map<String, Object> mainMap = new LinkedHashMap<>();
044
045        // tileInfo
046        Map<String, Object> tileInfo = new LinkedHashMap<>();
047        tileInfo.put("maxtiles", tiles.getMaxTiles());
048        tileInfo.put("xtiles", tiles.getXTiles());
049        tileInfo.put("ytiles", tiles.getYTiles());
050        tileInfo.put("tileWidth", tiles.getTilesWidth());
051        tileInfo.put("tileHeight", tiles.getTilesHeight());
052        tileInfo.put("zoom", tiles.getZoomfactor());
053        mainMap.put("tileInfo", tileInfo);
054
055        // orginial Image info
056        Map<String, Object> orgImgInfo = new LinkedHashMap<>();
057        orgImgInfo.put("format", tiles.getOriginalImageInfo().getFormat());
058        orgImgInfo.put("width", tiles.getOriginalImageInfo().getWidth());
059        orgImgInfo.put("height", tiles.getOriginalImageInfo().getHeight());
060        mainMap.put("originalImage", orgImgInfo);
061
062        // src Image info
063        Map<String, Object> srcImgInfo = new LinkedHashMap<>();
064        srcImgInfo.put("format", tiles.getSourceImageInfo().getFormat());
065        srcImgInfo.put("width", tiles.getSourceImageInfo().getWidth());
066        srcImgInfo.put("height", tiles.getSourceImageInfo().getHeight());
067        mainMap.put("srcImage", srcImgInfo);
068
069        // misc tiler info
070        Map<String, Object> otherInfo = new LinkedHashMap<>();
071        for (String k : tiles.getInfo().keySet()) {
072            otherInfo.put(k, tiles.getInfo().get(k));
073        }
074        mainMap.put("additionalInfo", otherInfo);
075
076        ObjectMapper mapper = new ObjectMapper();
077        try {
078            return mapper.writeValueAsString(mainMap);
079        } catch (JsonProcessingException e) {
080            throw new NuxeoException("Unable to serialize tile", e);
081        }
082    }
083
084}