All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.asprise.imaging.core.util.JsonUtils Maven / Gradle / Ivy

Go to download

Acquire images from and have total control on almost all kinds of scanners on both Windows 32bit and 64bit.

There is a newer version: 12.0.20.3
Show newest version
/**********************************************************************************************
 *
 * Asprise Scanning and Imaging API - http://asprise.com/document-scanner-image-pdf/java-scanning-api-overview.html
 * Copyright (C) 1998-2018. Asprise Inc. 
 *
 * This file is licensed under the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * You should have received a copy of the GNU Affero General Public License.  If not, please
 * visit .
 *
 **********************************************************************************************/
package com.asprise.imaging.core.util;

import com.asprise.imaging.core.scan.twain.TwainException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.jr.ob.JSON;

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


public class JsonUtils {

    
    public static Map parseJson(String s) throws IOException {
        JsonParser parser = new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS).createParser(s);
        Map json = JSON.std.mapFrom(parser);
        return json;
    }

    
    public static Object[] parseJsonArray(String s) throws IOException {
        JsonParser parser = new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS).createParser(s);
        Object[] json = JSON.std.arrayFrom(parser);
        return json;
    }

    
    public static List parseJsonList(String s) throws IOException {
        JsonParser parser = new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS).createParser(s);
        List json = JSON.std.listFrom(parser);
        return json;
    }

    
    public static String jsonSerialize(Object jsonObject, boolean pretty) {
        try {
            return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT, pretty).with(JSON.Feature.WRITE_NULL_PROPERTIES).asString(jsonObject);
        } catch (Throwable e) {
            throw new TwainException("Failed to serialize JSON", e);
        }
    }

    public static String attrValue(Map json, String key, String defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        Object object = json.get(key);
        return object == null ? defaultValue : String.valueOf(object);
    }

    public static Boolean attrValueAsBoolean(Map json, String key, Boolean defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        return toBoolean(json.get(key), defaultValue);
    }

    public static Integer attrValueAsInt(Map json, String key, Integer defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        return toInteger(json.get(key), defaultValue);
    }

    public static Long attrValueAsLong(Map json, String key, Long defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        return toLong(json.get(key), defaultValue);
    }

    public static Double attrValueAsDouble(Map json, String key, Double defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        return toDouble(json.get(key), defaultValue);
    }

    public static String attrValueAsString(Map json, String key, String defaultValue) {
        if(json == null || key == null) {
            return defaultValue;
        }
        return json.containsKey(key) ? (json.get(key) == null ? null : json.get(key).toString()) : defaultValue;
    }

    
    public static Integer toInteger(Object object, Integer defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        if (object instanceof Number) {
            return ((Number) object).intValue();
        }

        try {
            Double value = Double.valueOf(object.toString().trim());
            return value.intValue();
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    
    public static Long toLong(Object object, Long defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        if (object instanceof Number) {
            return ((Number) object).longValue();
        }

        try {
            Double value = Double.valueOf(object.toString().trim());
            return value.longValue();
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    
    public static Double toDouble(Object object, Double defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        if (object instanceof Number) {
            return ((Number) object).doubleValue();
        }

        try {
            Double value = Double.valueOf(object.toString().trim());
            return value;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    
    public static Number toNumber(Object object, Number defaultValue) {
        if(object == null) {
            return defaultValue;
        }
        if(object instanceof Number) {
            return (Number) object;
        }

        String s = object.toString();
        s = s.replaceAll("\\.0*$", ""); 
        try {
            if(s.contains(".")) {
                return Double.parseDouble(s);
            } else {
                return Integer.parseInt(s);
            }
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    
    public static Boolean toBoolean(Object object, Boolean defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        if (object instanceof Boolean) {
            return ((Boolean) object).booleanValue();
        }

        try {
            Boolean value = Boolean.valueOf(object.toString().trim());
            return value;
        } catch (Throwable e) {
            return defaultValue;
        }
    }
}