org.reactivesource.util.JsonParserUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.reactivesource.util.JsonParserUtils.java

Source

/*******************************************************************************
 * Copyright (c) 2013-2014 eBay Software Foundation
 *
 * See the file license.txt for copying permission.
 ******************************************************************************/

package org.reactivesource.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.reactivesource.util.Assert.notNull;

@SuppressWarnings("unchecked")
public class JsonParserUtils {

    public static Map<String, Object> jsonStringToMap(String jsonString) {
        notNull(jsonString, "jsonString can not be null");
        return jsonObjectToMap(new JSONObject(jsonString));
    }

    public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) {
        notNull(jsonObject, "jsonString can not be null");
        try {
            return new ObjectMapper().readValue(jsonObject.toString(), HashMap.class);
        } catch (IOException e) {
            throw new JSONException("Could not map row entity:" + jsonObject.toString());
        }
    }

}