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.kernel.json.JSONDeserializer;
018    import com.liferay.portal.kernel.json.JSONDeserializerTransformer;
019    
020    import jodd.json.JsonParser;
021    import jodd.json.ValueConverter;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class JSONDeserializerImpl<T> implements JSONDeserializer<T> {
027    
028            public JSONDeserializerImpl() {
029                    _jsonDeserializer = new PortalJsonParser();
030            }
031    
032            @Override
033            public T deserialize(String input) {
034                    return _jsonDeserializer.parse(input);
035            }
036    
037            @Override
038            public T deserialize(String input, Class<T> targetType) {
039                    return _jsonDeserializer.parse(input, targetType);
040            }
041    
042            @Override
043            public <K, V> JSONDeserializer<T> transform(
044                    JSONDeserializerTransformer<K, V> jsonDeserializerTransformer,
045                    String field) {
046    
047                    ValueConverter<K, V> valueConverter =
048                            new JoddJsonDeserializerTransformer<>(jsonDeserializerTransformer);
049    
050                    _jsonDeserializer.use(field, valueConverter);
051    
052                    return this;
053            }
054    
055            @Override
056            public JSONDeserializer<T> use(String path, Class<?> clazz) {
057                    _jsonDeserializer.map(path, clazz);
058    
059                    return this;
060            }
061    
062            private final JsonParser _jsonDeserializer;
063    
064    }