001/*
002 * Copyright (c) 2011-2014 GoPivotal, Inc. All Rights Reserved.
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 */
016package io.stallion.utils.json;
017
018import com.fasterxml.jackson.annotation.JsonView;
019import com.fasterxml.jackson.core.JsonFactory;
020import com.fasterxml.jackson.core.JsonGenerator;
021import com.fasterxml.jackson.core.JsonProcessingException;
022import com.fasterxml.jackson.core.Version;
023import com.fasterxml.jackson.core.type.TypeReference;
024import com.fasterxml.jackson.databind.*;
025import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
026import com.fasterxml.jackson.databind.module.SimpleModule;
027import com.fasterxml.jackson.databind.ser.std.RawSerializer;
028import io.stallion.exceptions.*;
029import io.stallion.utils.DateUtils;
030import jdk.nashorn.api.scripting.JSObject;
031import jdk.nashorn.internal.runtime.ScriptObject;
032import org.parboiled.common.FileUtils;
033
034
035import java.io.IOException;
036import java.util.ArrayList;
037import java.util.List;
038import java.util.Map;
039
040public class JSON {
041
042    private static final ObjectMapper mapper;
043
044
045    static {
046
047        mapper = new ObjectMapper();
048        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
049        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
050        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
051        mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
052        mapper.findAndRegisterModules();
053
054        SimpleModule mod = new SimpleModule("nashornConverter", Version.unknownVersion());
055
056        mod.addSerializer(JSObject.class, new JSObjectSerializer());
057        mod.addSerializer(ScriptObject.class, new ScriptObjectSerializer());
058        mod.addDeserializer(JSObject.class, new JSObjectDeserializer());
059
060
061        //SimpleModule mod2 = new SimpleModule("rawJsonConverter", Version.unknownVersion());
062        //mod2.addSerializer(RawJson.class, new RawJsonSerializer());
063
064        mapper.registerModules(mod);
065
066    }
067
068    public static Map<String, Object> parseMap(String json) {
069        return parse(json, new TypeReference<Map<String, Object>>(){});
070    }
071
072    public static <T> T parse(String json, TypeReference<T> typeReference) throws io.stallion.exceptions.JsonMappingException  {
073        try {
074            return mapper.readValue(json, typeReference);
075        } catch (IOException e) {
076            throw new io.stallion.exceptions.JsonMappingException("Exception parsing json", e);
077        }
078    }
079
080    /*
081    public static Object parse(String json, Class cls) throws IOException {
082        return mapper.readValue(json, cls);
083    }
084    */
085
086    public static <T> T parse(String json, Class<T> cls) throws io.stallion.exceptions.JsonMappingException {
087        try {
088
089            return mapper.readValue(json, cls);
090        } catch (IOException e) {
091            throw new io.stallion.exceptions.JsonMappingException("Exception parsing: json" , e);
092        }
093    }
094
095    public static JSObject parse(String json) throws io.stallion.exceptions.JsonMappingException {
096        try {
097            return mapper.readValue(json, JSObject.class);
098        } catch (IOException e) {
099            throw new io.stallion.exceptions.JsonMappingException("Exception parsing json", e);
100        }
101    }
102
103    public static List parseList(String json) throws io.stallion.exceptions.JsonMappingException {
104        try {
105            return mapper.readValue(json, ArrayList.class);
106        } catch (IOException e) {
107            throw new io.stallion.exceptions.JsonMappingException("Exception parsing json", e);
108        }
109    }
110
111
112    public static String stringify(Object obj) throws JsonWriteException {
113        try {
114            return mapper.writeValueAsString(obj);
115        } catch (Exception e) {
116            throw new JsonWriteException(e);
117        }
118    }
119
120    public static String stringify(Object obj, Class view) throws JsonProcessingException {
121        return stringify(obj, view, false);
122    }
123
124    public static String stringify(Object obj, Class view, Boolean excludeByDefault) throws JsonProcessingException {
125        ObjectMapper objectMapper = mapper;
126        if (excludeByDefault) {
127            objectMapper = mapper.copy();
128            objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
129        }
130        ObjectWriter writer = objectMapper.writerWithView(view);
131        return writer.writeValueAsString(obj);
132    }
133
134}