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:org.talend.mdm.query.BasicConditionProcessor.java

License:Open Source License

public Condition process(JsonObject condition, MetadataRepository repository) {
    JsonArray conditionElement = condition.get(getConditionElement()).getAsJsonArray(); //$NON-NLS-1
    TypedExpression expression = null;//from w  ww.  j av a2s .  c o m
    String value = null;
    TypedExpression valueExpression = null;
    for (int i = 0; i < conditionElement.size(); i++) {
        JsonObject element = conditionElement.get(i).getAsJsonObject();
        if (element.has("value")) { //$NON-NLS-1
            JsonElement valueElement = element.get("value");
            if (valueElement.isJsonPrimitive()) {
                value = element.getAsJsonPrimitive("value").getAsString(); //$NON-NLS-1
            } else if (valueElement.isJsonObject()) {
                valueExpression = Deserializer.getTypedExpression(valueElement.getAsJsonObject())
                        .process(valueElement.getAsJsonObject(), repository);
            } else {
                throw new IllegalArgumentException("Value '" + valueElement + "' is not supported.");
            }
        } else {
            expression = Deserializer.getTypedExpression(element.getAsJsonObject())
                    .process(element.getAsJsonObject(), repository);
        }
    }
    if (expression == null || (value == null && valueExpression == null)) {
        throw new IllegalArgumentException("Missing expression and/or value.");
    }
    if (value != null) {
        return buildCondition(expression, value);
    } else { // valueExpression != null
        return buildCondition(expression, valueExpression);
    }
}

From source file:org.talend.mdm.query.IndexProcessor.java

License:Open Source License

@Override
public TypedExpression process(JsonObject element, MetadataRepository repository) {
    JsonArray index = element.get("index").getAsJsonArray(); //$NON-NLS-1$
    TypedExpression expression = null;//  w w  w  .j  av  a2s. c o  m
    int indexValue = -1;
    for (int j = 0; j < index.size(); j++) {
        JsonElement jsonElement = index.get(j);
        if (jsonElement.isJsonObject()) {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            expression = Deserializer.getTypedExpression(jsonObject).process(jsonObject, repository);
        } else if (jsonElement.isJsonPrimitive()) {
            indexValue = jsonElement.getAsInt();
        }
    }
    if (expression == null) {
        throw new IllegalArgumentException("Malformed query (expected expression in '" + index + "'");
    }
    if (indexValue < 0) {
        throw new IllegalArgumentException("Malformed query (expected 'index' in '" + index + "'");
    }
    if (!(expression instanceof Field)) {
        throw new IllegalArgumentException("Malformed query (expected a field in expression)");
    }
    return index(((Field) expression).getFieldMetadata(), indexValue);
}

From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java

License:Apache License

private static TokenizerFactory buildTokenizerFactory(JsonElement map, String analyzerName) throws IOException {
    if (!(map instanceof JsonObject)) {
        throw new IllegalArgumentException("Expecting a map with \"factory\" string and "
                + "\"params\" map in tokenizer factory;" + " not: " + map.toString() + " in " + analyzerName);
    }//from  ww  w .ja v  a2  s . c  o  m
    JsonElement factoryEl = ((JsonObject) map).get(FACTORY);
    if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
        throw new IllegalArgumentException(
                "Expecting value for factory in char filter factory builder in:" + analyzerName);
    }
    String factoryName = factoryEl.getAsString();
    factoryName = factoryName.startsWith("oala.")
            ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.")
            : factoryName;

    JsonElement paramsEl = ((JsonObject) map).get(PARAMS);
    Map<String, String> params = mapify(paramsEl);
    String spiName = "";
    for (String s : TokenizerFactory.availableTokenizers()) {
        Class clazz = TokenizerFactory.lookupClass(s);
        if (clazz.getName().equals(factoryName)) {
            spiName = s;
            break;
        }
    }
    try {
        TokenizerFactory tokenizerFactory = TokenizerFactory.forName(spiName, params);
        if (tokenizerFactory instanceof ResourceLoaderAware) {
            ((ResourceLoaderAware) tokenizerFactory)
                    .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
        }

        return tokenizerFactory;
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("While working on " + analyzerName, e);
    }
}

From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java

License:Apache License

private static CharFilterFactory[] buildCharFilters(JsonElement el, String analyzerName) throws IOException {
    if (el == null || el.isJsonNull()) {
        return null;
    }// w  ww . ja  v  a 2  s .co m
    if (!el.isJsonArray()) {
        throw new IllegalArgumentException(
                "Expecting array for charfilters, but got:" + el.toString() + " for " + analyzerName);
    }
    JsonArray jsonArray = (JsonArray) el;
    List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>();
    for (JsonElement filterMap : jsonArray) {
        if (!(filterMap instanceof JsonObject)) {
            throw new IllegalArgumentException(
                    "Expecting a map with \"factory\" string and \"params\" map in char filter factory;"
                            + " not: " + filterMap.toString() + " in " + analyzerName);
        }
        JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY);
        if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
            throw new IllegalArgumentException(
                    "Expecting value for factory in char filter factory builder in:" + analyzerName);
        }
        String factoryName = factoryEl.getAsString();
        factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis.");

        JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS);
        Map<String, String> params = mapify(paramsEl);
        for (Map.Entry<String, String> e : params.entrySet()) {
            System.out.println("PARAM: " + e.getKey() + " : " + e.getValue());
        }
        String spiName = "";
        for (String s : CharFilterFactory.availableCharFilters()) {
            Class clazz = CharFilterFactory.lookupClass(s);
            if (clazz.getName().equals(factoryName)) {
                spiName = s;
                break;
            }
        }
        try {
            CharFilterFactory charFilterFactory = CharFilterFactory.forName(spiName, params);
            if (charFilterFactory instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware) charFilterFactory)
                        .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
            }
            ret.add(charFilterFactory);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("While trying to load " + analyzerName + ": " + e.getMessage(),
                    e);
        }
    }
    if (ret.size() == 0) {
        return new CharFilterFactory[0];
    }
    return ret.toArray(new CharFilterFactory[ret.size()]);
}

From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java

License:Apache License

private static TokenFilterFactory[] buildTokenFilterFactories(JsonElement el, String analyzerName)
        throws IOException {
    if (el == null || el.isJsonNull()) {
        return null;
    }/*from w  ww. j  a  v a 2s .  c om*/
    if (!el.isJsonArray()) {
        throw new IllegalArgumentException(
                "Expecting array for tokenfilters, but got:" + el.toString() + " in " + analyzerName);
    }
    JsonArray jsonArray = (JsonArray) el;
    List<TokenFilterFactory> ret = new LinkedList<>();
    for (JsonElement filterMap : jsonArray) {
        if (!(filterMap instanceof JsonObject)) {
            throw new IllegalArgumentException(
                    "Expecting a map with \"factory\" string and \"params\" map in token filter factory;"
                            + " not: " + filterMap.toString() + " in " + analyzerName);
        }
        JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY);
        if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
            throw new IllegalArgumentException(
                    "Expecting value for factory in token filter factory builder in " + analyzerName);
        }
        String factoryName = factoryEl.getAsString();
        factoryName = factoryName.startsWith("oala.")
                ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.")
                : factoryName;

        JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS);
        Map<String, String> params = mapify(paramsEl);
        String spiName = "";
        for (String s : TokenFilterFactory.availableTokenFilters()) {
            Class clazz = TokenFilterFactory.lookupClass(s);
            if (clazz.getName().equals(factoryName)) {
                spiName = s;
                break;
            }
        }

        try {
            TokenFilterFactory tokenFilterFactory = TokenFilterFactory.forName(spiName, params);
            if (tokenFilterFactory instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware) tokenFilterFactory)
                        .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
            }
            ret.add(tokenFilterFactory);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("While loading " + analyzerName, e);
        }
    }
    if (ret.size() == 0) {
        return new TokenFilterFactory[0];
    }
    return ret.toArray(new TokenFilterFactory[ret.size()]);
}

From source file:org.tallison.gramreaper.ingest.schema.FieldMapper.java

License:Apache License

public static FieldMapper load(JsonElement el) {
    if (el == null) {
        throw new IllegalArgumentException(NAME + " must not be empty");
    }//www. j  ava 2  s .  c om
    JsonObject root = el.getAsJsonObject();

    if (root.size() == 0) {
        return new FieldMapper();
    }
    //build ignore case element
    JsonElement ignoreCaseElement = root.get(IGNORE_CASE_KEY);
    if (ignoreCaseElement == null || !ignoreCaseElement.isJsonPrimitive()) {
        throw new IllegalArgumentException(
                "ignore case element in field mapper must not be null and must be a primitive: "
                        + ((ignoreCaseElement == null) ? "" : ignoreCaseElement.toString()));
    }
    String ignoreCaseString = ((JsonPrimitive) ignoreCaseElement).getAsString().toLowerCase();
    FieldMapper mapper = new FieldMapper();
    if ("true".equals(ignoreCaseString)) {
        mapper.setIgnoreCase(true);
    } else if ("false".equals(ignoreCaseString)) {
        mapper.setIgnoreCase(false);
    } else {
        throw new IllegalArgumentException(IGNORE_CASE_KEY + " must have a value of \"true\" or \"false\"");
    }

    JsonArray mappings = root.getAsJsonArray("mappings");
    for (JsonElement mappingElement : mappings) {
        JsonObject mappingObj = mappingElement.getAsJsonObject();
        String from = mappingObj.getAsJsonPrimitive("f").getAsString();
        IndivFieldMapper indivFieldMapper = buildMapper(mappingObj);
        mapper.add(from, indivFieldMapper);
    }
    return mapper;
}

From source file:org.tallison.schema.FieldMapper.java

License:Apache License

public static FieldMapper load(JsonElement el) {
    if (el == null) {
        throw new IllegalArgumentException(NAME + " must not be empty");
    }/*from   w  w w.  j  a  v a 2s  . c om*/
    JsonObject root = el.getAsJsonObject();

    //build ignore case element
    JsonElement ignoreCaseElement = root.get(IGNORE_CASE_KEY);
    if (ignoreCaseElement == null || !ignoreCaseElement.isJsonPrimitive()) {
        throw new IllegalArgumentException("ignore case element must not be null and must be a primitive: "
                + ((ignoreCaseElement == null) ? "" : ignoreCaseElement.toString()));
    }
    String ignoreCaseString = ((JsonPrimitive) ignoreCaseElement).getAsString().toLowerCase();
    FieldMapper mapper = new FieldMapper();
    if ("true".equals(ignoreCaseString)) {
        mapper.setIgnoreCase(true);
    } else if ("false".equals(ignoreCaseString)) {
        mapper.setIgnoreCase(false);
    } else {
        throw new IllegalArgumentException(IGNORE_CASE_KEY + " must have a value of \"true\" or \"false\"");
    }

    JsonArray mappings = root.getAsJsonArray("mappings");
    for (JsonElement mappingElement : mappings) {
        JsonObject mappingObj = mappingElement.getAsJsonObject();
        String from = mappingObj.getAsJsonPrimitive("f").getAsString();
        IndivFieldMapper indivFieldMapper = buildMapper(mappingObj);
        mapper.add(from, indivFieldMapper);
    }
    return mapper;
}

From source file:org.terasology.i18n.gson.I18nMapTypeAdapter.java

License:Apache License

@Override
public I18nMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        return new I18nMap(json.getAsString());
    } else if (json.isJsonObject()) {
        JsonObject obj = json.getAsJsonObject();
        Map<Locale, String> values = Maps.newHashMapWithExpectedSize(obj.entrySet().size());
        for (Map.Entry<String, JsonElement> item : obj.entrySet()) {
            if (item.getValue().isJsonPrimitive()) {
                values.put(Locale.forLanguageTag(item.getKey()), item.getValue().getAsString());
            } else {
                throw new JsonParseException(
                        "Expected locale string pair, found " + item.getKey() + "'" + item.getValue() + "'");
            }/* ww w.j a  va2s  .  c o m*/
        }
        return new I18nMap(values);
    } else {
        throw new JsonParseException("Invalid I18nMap: '" + json + "'");
    }
}

From source file:org.terasology.logic.behavior.core.BehaviorTreeBuilder.java

License:Apache License

@Override
public BehaviorNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    BehaviorNode node;//from  www. j a  va 2 s. c  o m
    if (json.isJsonPrimitive()) {
        node = getPrimitiveNode(json, context);
    } else {
        node = getCompositeNode(json, context);
    }
    node = createNode(node);
    return node;
}

From source file:org.terasology.persistence.typeHandling.gson.GsonPersistedDataArray.java

License:Apache License

public boolean isNumberArray() {
    for (JsonElement element : array) {
        if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isNumber()) {
            return false;
        }//from   w w  w  .j a  v  a2  s  . c o  m
    }
    return true;
}