Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.voxelplugineering.voxelsniper.service.persistence.JsonDataSource.java

License:Open Source License

/**
 * Recursively converts the given {@link JsonElement} to a
 * {@link DataContainer}.//  w ww.  j a  va 2s  .  c om
 * 
 * @param rootelement The element to convert
 * @return The new {@link DataContainer}
 */
public DataContainer toContainer(JsonElement rootelement) {
    DataContainer container = new MemoryContainer("");

    if (rootelement.isJsonObject()) {
        JsonObject root = rootelement.getAsJsonObject();
        for (Entry<String, JsonElement> entry : root.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();
            if (element.isJsonPrimitive()) {
                JsonPrimitive primitive = element.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    container.writeBoolean(key, primitive.getAsBoolean());
                } else if (primitive.isString()) {
                    container.writeString(key, primitive.getAsString());
                } else if (primitive.isNumber()) {
                    container.writeNumber(key, primitive.getAsNumber());
                }
            } else if (element.isJsonObject()) {
                container.writeContainer(key, toContainer(element));
            }
        }
    }

    return container;

}

From source file:com.voxelplugineering.voxelsniper.service.persistence.JsonDataSourceReader.java

License:Open Source License

/**
 * Recursively converts the given {@link JsonElement} to a {@link DataContainer}.
 * // w  w  w  . ja  va 2 s.com
 * @param rootelement The element to convert
 * @return The new {@link DataContainer}
 */
public DataContainer toContainer(JsonElement rootelement) {
    DataContainer container = new MemoryContainer("");

    if (rootelement.isJsonObject()) {
        JsonObject root = rootelement.getAsJsonObject();
        for (Entry<String, JsonElement> entry : root.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();
            if (element.isJsonPrimitive()) {
                JsonPrimitive primitive = element.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    container.setBoolean(key, primitive.getAsBoolean());
                } else if (primitive.isString()) {
                    container.setString(key, primitive.getAsString());
                } else if (primitive.isNumber()) {
                    container.setNumber(key, primitive.getAsNumber());
                }
            } else if (element.isJsonObject()) {
                container.setContainer(key, toContainer(element));
            }
        }
    }

    return container;

}

From source file:com.wialon.core.Session.java

License:Apache License

/**
 * Initialize Wialon session//from ww  w.  j a  va  2  s. c  om
 * @param baseUrl
 */
public boolean initSession(String baseUrl) {
    this.baseUrl = baseUrl;
    this.renderer = new Renderer();
    this.messagesLoader = new MessagesLoader();
    if (httpClient == null)
        httpClient = RemoteHttpClient.getInstance();
    if (jsonParser == null)
        jsonParser = new JsonParser();
    if (gson == null)
        gson = new GsonBuilder().registerTypeAdapter(String.class, new JsonDeserializer<String>() {
            @Override
            public String deserialize(JsonElement jsonElement, Type type,
                    JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                return jsonElement.isJsonPrimitive() ? jsonElement.getAsString() : jsonElement.toString();
            }
        }).create();
    return initialized = true;
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Parses a {@link JsonObject{ into a MapRecord. A duplicate key in the JsonObject will
 * overwrite the previous key./*from w ww.ja va 2 s  .  co m*/
 * @param map The JsonObject being parsed
 * @param generator Construct to generate a docId for this {@link MapRecord}
 * @return A MapRecord built from the {@link JsonObject}
 */
protected static MapRecord asMapRecord(JsonObject map, DocIdGenerator generator) {
    Map<Column, RecordValue<?>> data = Maps.newHashMap();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Builds a {@link MultimapRecord} from the provided {@link JsonObject} using a docid
 * from the {@link DocIdGenerator}. This method will support lists of values for a key
 * in the {@link JsonObject} but will fail on values which are {@link JsonObject}s and 
 * values which have nested {@link JsonArray}s. 
 * @param map The {@link JsonObject} to build this {@link MultimapRecord} from
 * @param generator {@link DocIdGenerator} to construct a docid for this {@link MultimapRecord}
 * @return A {@link MultimapRecord} built from the provided arguments.
 *//*w ww  .j  a va2s .com*/
protected static MultimapRecord asMultimapRecord(JsonObject map, DocIdGenerator generator) {
    Multimap<Column, RecordValue<?>> data = HashMultimap.create();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else if (value.isJsonArray()) {

            // Multimaps should handle the multiple values, not fail
            JsonArray values = value.getAsJsonArray();
            for (JsonElement element : values) {
                if (element.isJsonNull()) {
                    data.put(key, null);
                } else if (element.isJsonPrimitive()) {

                    JsonPrimitive primitive = (JsonPrimitive) element;

                    // Numbers
                    if (primitive.isNumber()) {
                        NumberRecordValue<?> v;

                        double d = primitive.getAsDouble();
                        if ((int) d == d) {
                            v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                        } else if ((long) d == d) {
                            v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                        } else {
                            v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                        }

                        data.put(key, v);

                    } else if (primitive.isString()) {
                        // String
                        data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

                    } else if (primitive.isBoolean()) {
                        // Boolean
                        data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

                    } else if (primitive.isJsonNull()) {
                        // Is this redundant?
                        data.put(key, null);
                    } else {
                        throw new RuntimeException("Unhandled Json primitive: " + primitive);
                    }
                } else {
                    throw new RuntimeException("Expected a Json primitive");
                }
            }

        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MultimapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);

}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean jsonCompare(JsonElement element1, JsonElement element2) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element1.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element1.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element1.isJsonObject()) {
        //System.out.println("Element is object");
        Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element1.getAsJsonObject().entrySet()
                .iterator();//from   w w w  . java 2 s . c  o  m
        while (CrunchifyIterator.hasNext()) {
            Map.Entry<String, JsonElement> aux = CrunchifyIterator.next();
            //System.out.println("Field:" + aux.getKey());
            if (findElement(element2, aux.getKey()) == false) {
                //System.out.println("NOT FOUND Field:" + aux.getKey());
                return false;
            } else if (findElement(element2, aux.getKey()) == true) {
                //System.out.println("FOUND Field:" + aux.getKey());
                if (jsonCompare(aux.getValue(), element2) == false) {
                    return false;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element1.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element1.getAsJsonArray()) {
            if (jsonCompare(jsonElement, element2) == false) {
                return false;
            }
        }
    }
    return true;
}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean findElement(JsonElement element, String field) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element.isJsonObject()) {
        //System.out.println("Element is object");

        if (element.getAsJsonObject().has(field)) {
            //System.out.println("Object found --> " + element.getAsJsonObject().get(field).toString());
            return true;
        } else {/*from   w w  w.  j a va2 s .co m*/
            Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element.getAsJsonObject().entrySet()
                    .iterator();
            while (CrunchifyIterator.hasNext()) {
                if (findElement(CrunchifyIterator.next().getValue(), field) == true) {
                    return true;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element.getAsJsonArray()) {
            if (findElement(jsonElement, field) == true) {
                return true;
            }
        }
    }
    return false;
}

From source file:DAO.ValidationDAO.ValidationOperationSource.java

public boolean findElementAndValue(JsonElement element, String field, String value) {

    //-------------------Caso o Json seja NULO------------------------------
    if (element.isJsonNull()) {
        //System.out.println("Element is null");

        //---------------Caso o Json seja uma primitiva-------------------------
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Element is primitive");

        //----------------Caso o Json seja um Objeto----------------------------
    } else if (element.isJsonObject()) {
        //System.out.println("Element is object");

        if (element.getAsJsonObject().has(field)
                && element.getAsJsonObject().get(field).toString().contains(value)) {
            //System.out.println("Object found --> " + element.getAsJsonObject().get(field).toString());
            return true;
        } else {//from w w  w  .  ja  v  a 2  s .c  om
            Iterator<Map.Entry<String, JsonElement>> CrunchifyIterator = element.getAsJsonObject().entrySet()
                    .iterator();
            while (CrunchifyIterator.hasNext()) {
                if (findElementAndValue(CrunchifyIterator.next().getValue(), field, value) == true) {
                    return true;
                }
            }
        }
        //------------Caso o json seja um array---------------------------------
    } else if (element.isJsonArray()) {
        //System.out.println("Element is array");
        for (JsonElement jsonElement : element.getAsJsonArray()) {
            if (findElementAndValue(jsonElement, field, value) == true) {
                return true;
            }
        }
    }
    return false;
}

From source file:Days.Day12.java

public int parseElement(JsonElement element) {
    int totaal = 0;
    if (element.isJsonArray()) {
        totaal += parseArray(element.getAsJsonArray());
    } else if (element.isJsonObject()) {
        totaal += parseObject(element.getAsJsonObject());
    } else if (element.isJsonPrimitive()) {
        totaal += parsePrimitive(element.getAsJsonPrimitive());
    }//from  www .j a  v  a  2s.c  o  m
    return totaal;
}

From source file:de.azapps.mirakel.model.list.meta.SetDeserializer.java

License:Open Source License

@Override
public T deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
    if (json.isJsonObject()) {
        List<Integer> content = null;
        Boolean negated = null;// initialize with stuff to mute the
        // compiler
        for (final Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {

            if (entry.getValue().isJsonArray() && "content".equals(entry.getKey())) {
                content = new ArrayList<>(entry.getValue().getAsJsonArray().size());
                for (final JsonElement el : ((JsonArray) entry.getValue())) {
                    if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
                        content.add(el.getAsInt());
                    }//w  w w . j a v  a2  s .  c o m
                }
            } else if (entry.getValue().isJsonPrimitive()
                    && ("isNegated".equals(entry.getKey()) || "isSet".equals(entry.getKey()))) {
                negated = entry.getValue().getAsBoolean();
            } else {
                throw new JsonParseException("unknown format");
            }
        }
        if ((content != null) && (negated != null)) {
            try {
                return (T) clazz.getConstructor(boolean.class, List.class).newInstance(negated, content);
            } catch (IllegalAccessException | InstantiationException | NoSuchMethodException
                    | InvocationTargetException e) {
                throw new JsonParseException("Could not create new SetProperty", e);
            }

        }
    }
    throw new JsonParseException("unknown format");
}