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

com.alibaba.fastjson2.function.impl.ToDouble Maven / Gradle / Ivy

Go to download

Fastjson is a JSON processor (JSON parser + JSON generator) written in Java

There is a newer version: 2.0.53.android8
Show newest version
package com.alibaba.fastjson2.function.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;

import java.util.List;
import java.util.function.Function;

public class ToDouble
        implements Function {
    final Double defaultValue;

    public ToDouble(Double defaultValue) {
        this.defaultValue = defaultValue;
    }

    @Override
    public Object apply(Object o) {
        if (o == null) {
            return defaultValue;
        }

        if (o instanceof Boolean) {
            return (Boolean) o ? 1D : 0D;
        }

        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        }

        if (o instanceof String) {
            String str = (String) o;
            if (str.isEmpty()) {
                return defaultValue;
            }

            return Double.parseDouble(str);
        }

        if (o instanceof List) {
            List list = (List) o;
            JSONArray array = new JSONArray(list.size());
            for (int i = 0; i < list.size(); i++) {
                Object item = list.get(i);
                array.add(apply(item));
            }
            return array;
        }

        throw new JSONException("can not cast to Double " + o.getClass());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy