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

com.qiniu.service.line.JsonObjParser Maven / Gradle / Ivy

There is a newer version: 8.4.8
Show newest version
package com.qiniu.service.line;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.qiniu.service.interfaces.ILineParser;

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

public class JsonObjParser implements ILineParser {

    private Map indexMap;
    private boolean forceParse;

    public JsonObjParser(Map indexMap, boolean forceParse) throws IOException {
        if (indexMap == null || indexMap.size() == 0) throw new IOException("there are no indexMap be set.");
        this.indexMap = indexMap;
        this.forceParse = forceParse;
    }

    public Map getItemMap(JsonObject json) throws IOException {
        Map itemMap = new HashMap<>();
        String mapKey;
        for (String key : json.keySet()) {
            mapKey = indexMap.get(key);
            if (mapKey != null) {
                if (json.get(key) instanceof JsonNull) itemMap.put(mapKey, null);
                else itemMap.put(mapKey, json.get(key).getAsString());
            }
        }
        if (!forceParse && itemMap.size() < indexMap.size())
            throw new IOException("no enough indexes in line. The parameter indexes may have incorrect order or name.");
        return itemMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy