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;
016    
017    import com.liferay.portal.json.transformer.FileJSONTransformer;
018    import com.liferay.portal.json.transformer.JSONArrayJSONTransformer;
019    import com.liferay.portal.json.transformer.JSONObjectJSONTransformer;
020    import com.liferay.portal.json.transformer.JSONSerializableJSONTransformer;
021    import com.liferay.portal.json.transformer.RepositoryModelJSONTransformer;
022    import com.liferay.portal.json.transformer.UserJSONTransformer;
023    import com.liferay.portal.kernel.json.JSON;
024    import com.liferay.portal.kernel.json.JSONArray;
025    import com.liferay.portal.kernel.json.JSONObject;
026    import com.liferay.portal.kernel.json.JSONSerializable;
027    import com.liferay.portal.kernel.json.JSONTransformer;
028    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
029    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
030    import com.liferay.portal.kernel.portlet.PortletDisplayModel;
031    import com.liferay.portal.kernel.repository.model.RepositoryModel;
032    import com.liferay.portal.model.User;
033    import com.liferay.portlet.expando.model.ExpandoBridge;
034    
035    import java.io.File;
036    import java.io.InputStream;
037    import java.io.OutputStream;
038    
039    import javax.portlet.PortletURL;
040    
041    import jodd.introspector.CachingIntrospector;
042    import jodd.introspector.JoddIntrospector;
043    
044    import jodd.json.JoddJson;
045    import jodd.json.TypeJsonSerializerMap;
046    
047    /**
048     * @author Igor Spasic
049     */
050    public class JSONInit {
051    
052            public static synchronized void init() {
053                    try {
054                            if (_initalized) {
055                                    return;
056                            }
057    
058                            _registerDefaultTransformers();
059    
060                            _initalized = true;
061                    }
062                    catch (Exception e) {
063                            throw new RuntimeException(e);
064                    }
065            }
066    
067            private static void _registerDefaultTransformers() throws Exception {
068                    JoddIntrospector.introspector = new CachingIntrospector(
069                            true, true, true, new String[] {"_"});
070    
071                    JoddJson.jsonAnnotation = JSON.class;
072    
073                    JoddJson.excludedTypes = new Class[] {
074                            ExpandoBridge.class, InputStream.class, LiferayPortletRequest.class,
075                            LiferayPortletResponse.class, OutputStream.class,
076                            PortletDisplayModel.class, PortletURL.class
077                    };
078    
079                    JoddJson.excludedTypeNames = new String[] {
080                            "javax.*"
081                    };
082    
083                    TypeJsonSerializerMap typeSerializerMap = JoddJson.defaultSerializers;
084    
085                    Class<?>[][] classesArray = new Class<?>[][] {
086                            new Class[] {File.class, FileJSONTransformer.class},
087                            new Class[] {JSONArray.class, JSONArrayJSONTransformer.class},
088                            new Class[] {JSONObject.class, JSONObjectJSONTransformer.class},
089                            new Class[] {
090                                    JSONSerializable.class, JSONSerializableJSONTransformer.class
091                            },
092                            new Class[] {
093                                    RepositoryModel.class, RepositoryModelJSONTransformer.class
094                            },
095                            new Class[] {User.class, UserJSONTransformer.class}
096                    };
097    
098                    for (Class<?>[] classes : classesArray) {
099                            typeSerializerMap.register(
100                                    classes[0],
101                                    new JoddJsonTransformer(
102                                            (JSONTransformer)classes[1].newInstance()));
103                    }
104            }
105    
106            private static boolean _initalized = false;
107    
108    }