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

net.cnri.util.JsonToStreamObjectConverter Maven / Gradle / Ivy

Go to download

CNRI Handle.net Software (Permission granted for redistribution by Giridhar Manepalli at CNRI)

There is a newer version: 9.2.0.v20190814
Show newest version
/**********************************************************************\
 © COPYRIGHT 2019 Corporation for National Research Initiatives (CNRI);
                        All rights reserved.

        The HANDLE.NET software is made available subject to the
      Handle.Net Public License Agreement, which may be obtained at
         http://hdl.handle.net/20.1000/112 or hdl:20.1000/112
\**********************************************************************/

package net.cnri.util;

import java.math.BigDecimal;
import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

public class JsonToStreamObjectConverter {

    public static StreamObject toStreamObject(JsonElement jsonElement) {
        StreamObject result = null;
        if (jsonElement.isJsonObject()) {
            result = toStreamTable((JsonObject) jsonElement);
        } else {
            result = toStreamVector((JsonArray) jsonElement);
        }
        return result;
    }

    private static StreamTable toStreamTable(JsonObject jsonObject) {
        StreamTable streamTable = new StreamTable();
        for (Entry properties : jsonObject.entrySet()) {
            String key = properties.getKey();
            JsonElement value = properties.getValue();
            if (value.isJsonObject() || value.isJsonArray()) {
                StreamObject childObject = toStreamObject(value);
                streamTable.put(key, childObject);
            } else if (value.isJsonPrimitive()) {
                JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive();
                if (jsonPrimitive.isString()) {
                    String stringValue = value.getAsJsonPrimitive().getAsString();
                    streamTable.put(key, stringValue);
                } else if (jsonPrimitive.isBoolean()) {
                    boolean booleanValue = jsonPrimitive.getAsBoolean();
                    streamTable.put(key, booleanValue);
                } else if (jsonPrimitive.isNumber()) {
                    BigDecimal number = jsonPrimitive.getAsBigDecimal();
                    String numberAsString = number.toPlainString();
                    streamTable.put(key, numberAsString);
                } else if (jsonPrimitive.isJsonNull()) {
                    streamTable.put(key, "null");
                }
            }
        }
        return streamTable;
    }

    private static StreamVector toStreamVector(JsonArray jsonArray) {
        StreamVector streamVector = new StreamVector();
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement item = jsonArray.get(i);
            if (item.isJsonObject() || item.isJsonArray()) {
                StreamObject childObject = toStreamObject(item);
                streamVector.add(childObject);
            } else if (item.isJsonPrimitive()) {
                JsonPrimitive jsonPrimitive = item.getAsJsonPrimitive();
                if (jsonPrimitive.isString()) {
                    String stringValue = item.getAsJsonPrimitive().getAsString();
                    streamVector.add(stringValue);
                } else if (jsonPrimitive.isBoolean()) {
                    boolean booleanValue = jsonPrimitive.getAsBoolean();
                    streamVector.add(String.valueOf(booleanValue));
                } else if (jsonPrimitive.isNumber()) {
                    BigDecimal number = jsonPrimitive.getAsBigDecimal();
                    String numberAsString = number.toPlainString();
                    streamVector.add(numberAsString);
                } else if (jsonPrimitive.isJsonNull()) {
                    streamVector.add("null");
                }
            }
        }
        return streamVector;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy