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