Example usage for com.google.gson JsonObject size

List of usage examples for com.google.gson JsonObject size

Introduction

In this page you can find the example usage for com.google.gson JsonObject size.

Prototype

public int size() 

Source Link

Document

Returns the number of key/value pairs in the object.

Usage

From source file:org.wso2.siddhi.extension.input.mapper.json.JsonSourceMapper.java

License:Open Source License

private Event[] convertToEventArrayForDefaultMapping(Object eventObject) {
    Gson gson = new Gson();
    JsonObject[] eventObjects = gson.fromJson(eventObject.toString(), JsonObject[].class);
    Event[] events = new Event[eventObjects.length];
    int index = 0;
    JsonObject eventObj = null;
    for (JsonObject jsonEvent : eventObjects) {
        if (jsonEvent.has(DEFAULT_JSON_EVENT_IDENTIFIER)) {
            eventObj = jsonEvent.get(DEFAULT_JSON_EVENT_IDENTIFIER).getAsJsonObject();
            if (failOnMissingAttribute && eventObj.size() < streamAttributes.size()) {
                log.error("Json message " + eventObj.toString() + " contains missing attributes. "
                        + "Hence dropping the message.");
                continue;
            }//from  ww w  .  j  av a 2s  . c  o m
        } else {
            log.error("Default json message " + eventObj.toString()
                    + " in the array does not have the valid event identifier \"event\". "
                    + "Hence dropping the message.");
            continue;
        }
        Event event = new Event(streamAttributes.size());
        Object[] data = event.getData();

        int position = 0;
        for (Attribute attribute : streamAttributes) {
            String attributeName = attribute.getName();
            Attribute.Type type = attribute.getType();
            Object attributeValue = eventObj.get(attributeName);
            if (attributeValue == null) {
                data[position++] = null;
            } else {
                data[position++] = attributeConverter.getPropertyValue(eventObj.get(attributeName).toString(),
                        type);
            }
        }
        events[index++] = event;
    }
    return Arrays.copyOfRange(events, 0, index);
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

private static NVBase<?> guessNVBaseArray(JsonArray ja) {
    NVBase<?> ret = null;/*from   w  ww.  j ava2  s  .  com*/

    GNVType guess = null;
    for (int i = 0; i < ja.size(); i++) {
        JsonElement je = ja.get(i);

        if (je.isJsonObject()) {
            // could an NVEntity or NVPairList or NVGenericMap
            // nvpair
            JsonObject jo = je.getAsJsonObject();
            if (jo.size() == 1) {

                if (ret == null) {
                    return new NVPairList(null, new ArrayList<NVPair>());
                }
            }

            if (jo.size() > 1) {
                return new NVGenericMapList();

            }
        } else if (je.isJsonPrimitive()) {
            if (je.getAsJsonPrimitive().isString()) {
                // must be fixed
                //break;
                return new NVStringList();
            }

            GNVType gnv = GNVType.toGNVType(je.getAsNumber());
            if (gnv != null) {
                if (guess == null) {
                    guess = gnv;
                } else {
                    switch (gnv) {

                    case NVDOUBLE:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG || guess == GNVType.NVFLOAT) {
                            guess = gnv;
                        }
                        break;
                    case NVFLOAT:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG) {
                            guess = gnv;
                        }
                        break;
                    case NVINT:
                        break;
                    case NVLONG:
                        if (guess == GNVType.NVINT) {
                            guess = gnv;
                        }
                        break;
                    default:
                        break;

                    }
                }
            }
        }
    }

    if (ret == null && guess != null) {
        switch (guess) {

        case NVDOUBLE:
            ret = new NVDoubleList(null, new ArrayList<Double>());
            break;
        case NVFLOAT:
            ret = new NVFloatList(null, new ArrayList<Float>());
            break;
        case NVINT:
            ret = new NVIntList(null, new ArrayList<Integer>());
            break;
        case NVLONG:
            ret = new NVLongList(null, new ArrayList<Long>());
            break;
        default:
            break;

        }
    }

    return ret;
}