001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.json.transformer;
016    
017    import com.liferay.portal.json.JoddJSONContext;
018    import com.liferay.portal.kernel.json.JSONContext;
019    import com.liferay.portal.kernel.json.JSONTransformer;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    import java.util.TreeMap;
024    
025    import jodd.json.impl.MapJsonSerializer;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class SortedHashMapJSONTransformer
031            extends MapJsonSerializer implements JSONTransformer {
032    
033            @Override
034            public void transform(JSONContext jsonContext, Object map) {
035                    if (map instanceof HashMap) {
036                            TreeMap<Object, Object> treeMap = new TreeMap<>();
037    
038                            treeMap.putAll((HashMap<Object, Object>)map);
039    
040                            map = treeMap;
041                    }
042    
043                    JoddJSONContext joddJSONContext = (JoddJSONContext)jsonContext;
044    
045                    super.serialize(joddJSONContext.getImplementation(), (Map<?, ?>)map);
046            }
047    
048    }