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.lanternpowered.server.script.function.condition.json.ConditionTypeAdapterFactory.java

License:MIT License

@Override
protected Condition deserialize(TypeToken<Condition> type, JsonElement element, Gson gson) {
    final Class<? super Condition> raw = type.getRawType();
    if (AndCondition.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(AndCondition.class)).fromJsonTree(element);
    } else if (OrCondition.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(OrCondition.class)).fromJsonTree(element);
    } else if (raw == Condition.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonArray()) {
            return gson.getAdapter(AndCondition.class).fromJsonTree(element);
        } else if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, Condition.class).get();
            }/*from   w w  w .j av a 2  s .c om*/
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.DoubleValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected DoubleValueProvider deserialize(TypeToken<DoubleValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super DoubleValueProvider> raw = type.getRawType();
    if (FloatValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Constant.class))
                .fromJsonTree(element);//from   w  w  w  .  ja  va2  s .co m
    } else if (FloatValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Range.class))
                .fromJsonTree(element);
    } else if (raw == DoubleValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, DoubleValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.FloatValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected FloatValueProvider deserialize(TypeToken<FloatValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super FloatValueProvider> raw = type.getRawType();
    if (FloatValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Constant.class))
                .fromJsonTree(element);//w  w  w . ja v a  2  s . c  o m
    } else if (FloatValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Range.class))
                .fromJsonTree(element);
    } else if (raw == FloatValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, FloatValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.IntValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected IntValueProvider deserialize(TypeToken<IntValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super IntValueProvider> raw = type.getRawType();
    if (IntValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class))
                .fromJsonTree(element);//  www.j a  va2s . c  om
    } else if (IntValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Range.class)).fromJsonTree(element);
    } else if (raw == IntValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, IntValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.text.gson.JsonTextLiteralSerializer.java

License:MIT License

@Override
public LiteralText deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        return Text.of(json.getAsString());
    }/* w  w w  . j av a 2  s . c  om*/
    JsonObject json0 = json.getAsJsonObject();
    LiteralText.Builder builder = Text.builder(json0.get(TEXT).getAsString());
    deserialize(json0, builder, context);
    return builder.build();
}

From source file:org.lanternpowered.server.text.gson.JsonTextSerializer.java

License:MIT License

@Override
public Text deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        return context.deserialize(json, LiteralText.class);
    }//www.  j a  v  a2 s  .  c o m
    if (json.isJsonArray()) {
        final Text.Builder builder = Text.builder();
        builder.append(context.<Text[]>deserialize(json, Text[].class));
        return builder.build();
    }
    final JsonObject obj = json.getAsJsonObject();
    if (obj.has(TEXT)) {
        return context.deserialize(json, LiteralText.class);
    } else if (obj.has(TRANSLATABLE)) {
        return context.deserialize(json, TranslatableText.class);
    } else if (obj.has(SCORE_VALUE)) {
        return context.deserialize(json, ScoreText.class);
    } else if (obj.has(SELECTOR)) {
        return context.deserialize(json, SelectorText.class);
    } else {
        throw new JsonParseException("Unknown text format: " + json.toString());
    }
}

From source file:org.locationtech.geogig.spring.service.LegacyBatchObjectsService.java

License:Open Source License

public BatchObjects batchObjects(RepositoryProvider provider, String repoName, InputStream request) {
    final BatchObjects batchObjects = new BatchObjects();
    final Reader body = new InputStreamReader(request);
    final JsonParser parser = new JsonParser();
    final JsonElement messageJson = parser.parse(body);
    final ArrayList<ObjectId> want = new ArrayList<>();
    final ArrayList<ObjectId> have = new ArrayList<>();

    if (messageJson.isJsonObject()) {
        final JsonObject message = messageJson.getAsJsonObject();
        final JsonArray wantArray;
        final JsonArray haveArray;
        if (message.has("want") && message.get("want").isJsonArray()) {
            wantArray = message.get("want").getAsJsonArray();
        } else {//from w  w  w.  j  a v a 2 s .  c  o m
            wantArray = new JsonArray();
        }
        if (message.has("have") && message.get("have").isJsonArray()) {
            haveArray = message.get("have").getAsJsonArray();
        } else {
            haveArray = new JsonArray();
        }
        for (final JsonElement e : wantArray) {
            if (e.isJsonPrimitive()) {
                want.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString()));
            }
        }
        for (final JsonElement e : haveArray) {
            if (e.isJsonPrimitive()) {
                have.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString()));
            }
        }
    }

    final Repository repository = getRepository(provider, repoName);
    final Deduplicator deduplicator = DeduplicationService.create();
    BinaryPackedObjects packer = new BinaryPackedObjects(repository.objectDatabase());

    batchObjects.setDeduplicator(deduplicator).setHave(have).setPacker(packer).setWant(want);
    return batchObjects;
}

From source file:org.locationtech.geogig.spring.service.LegacyFilteredChangesService.java

License:Open Source License

public FilteredChanges filterChanges(RepositoryProvider provider, String repoName, InputStream request) {
    final FilteredChanges filteredChanges = new FilteredChanges();
    try {/* www. ja v a  2  s.c  o  m*/
        final Reader body = new InputStreamReader(request);
        final JsonParser parser = new JsonParser();
        final JsonElement messageJson = parser.parse(body);

        final HashSet<ObjectId> tracked = new HashSet<>();

        RepositoryFilter filter = new RepositoryFilter();

        ObjectId commitId = ObjectId.NULL;

        if (messageJson.isJsonObject()) {
            final JsonObject message = messageJson.getAsJsonObject();
            final JsonArray trackedArray;
            if (message.has("tracked") && message.get("tracked").isJsonArray()) {
                trackedArray = message.get("tracked").getAsJsonArray();
            } else {
                trackedArray = new JsonArray();
            }
            if (message.has("commitId") && message.get("commitId").isJsonPrimitive()) {
                commitId = ObjectId.valueOf(message.get("commitId").getAsJsonPrimitive().getAsString());
            } else {
                commitId = ObjectId.NULL;
            }
            for (final JsonElement e : trackedArray) {
                if (e.isJsonPrimitive()) {
                    tracked.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString()));
                }
            }

            if (message.has("filter") && message.get("filter").isJsonArray()) {
                JsonArray filterArray = message.get("filter").getAsJsonArray();
                for (final JsonElement e : filterArray) {
                    if (e.isJsonObject()) {
                        JsonObject filterObject = e.getAsJsonObject();
                        String featureType = null;
                        String filterType = null;
                        String filterText = null;
                        if (filterObject.has("featurepath")
                                && filterObject.get("featurepath").isJsonPrimitive()) {
                            featureType = filterObject.get("featurepath").getAsJsonPrimitive().getAsString();
                        }
                        if (filterObject.has("type") && filterObject.get("type").isJsonPrimitive()) {
                            filterType = filterObject.get("type").getAsJsonPrimitive().getAsString();
                        }
                        if (filterObject.has("filter") && filterObject.get("filter").isJsonPrimitive()) {
                            filterText = filterObject.get("filter").getAsJsonPrimitive().getAsString();
                        }
                        if (featureType != null && filterType != null && filterText != null) {
                            filter.addFilter(featureType, filterType, filterText);
                        }
                    }
                }

            }
        }

        final Repository repository = getRepository(provider, repoName);

        RevCommit commit = repository.getCommit(commitId);

        ObjectId parent = ObjectId.NULL;
        if (commit.getParentIds().size() > 0) {
            parent = commit.getParentIds().get(0);
        }

        AutoCloseableIterator<DiffEntry> changes = repository.command(DiffOp.class)
                .setNewVersion(commit.getId()).setOldVersion(parent).setReportTrees(true).call();
        FilteredDiffIterator filteredDiffIterator = new FilteredDiffIterator(changes, repository, filter) {
            @Override
            protected boolean trackingObject(ObjectId objectId) {
                return tracked.contains(objectId);
            }
        };

        filteredChanges.setChanges(filteredDiffIterator).setPacker(new BinaryPackedChanges(repository));

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return filteredChanges;
}

From source file:org.matrix.androidsdk.rest.model.bingrules.BingRule.java

License:Apache License

/**
 * Search a JsonPrimitive from its value.
 * @param value the jsonPrimitive value.
 * @return the json primitive. null if not found.
 *///from  ww w  .  jav a 2  s  . c  om
private JsonPrimitive jsonPrimitive(String value) {
    JsonPrimitive jsonPrimitive = null;

    if (null != actions) {
        for (JsonElement json : actions) {
            if (json.isJsonPrimitive()) {
                JsonPrimitive primitive = json.getAsJsonPrimitive();

                try {
                    if (TextUtils.equals(primitive.getAsString(), value)) {
                        jsonPrimitive = primitive;
                        break;
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG, "## jsonPrimitive() : " + e.getMessage());
                }
            }
        }
    }
    return jsonPrimitive;
}

From source file:org.matrix.androidsdk.util.BingRulesManager.java

License:Apache License

/**
 * Returns the first notifiable bing rule which fulfills its condition with this event.
 * @param event the event//from   w w w  . j a  v  a 2s.com
 * @return the first matched bing rule, null if none
 */
public BingRule fulfilledBingRule(Event event) {
    // sanity check
    if (null == event) {
        return null;
    }

    if (!isReady) {
        return null;
    }

    // do not trigger notification for oneself messages
    if ((null != event.userId) && (event.userId.equals(mMyUserId))) {
        return null;
    }

    if (mRules != null) {
        // Go down the rule list until we find a match
        for (BingRule bingRule : mRules) {
            if (eventMatchesConditions(event, bingRule.conditions)) {
                for (JsonElement action : bingRule.actions) {
                    if (action.isJsonPrimitive()) {
                        String actionString = action.getAsString();

                        if (BingRule.ACTION_NOTIFY.equals(actionString)
                                || BingRule.ACTION_COALESCE.equals(actionString)) {
                            return bingRule;
                        } else if (BingRule.ACTION_DONT_NOTIFY.equals(actionString)) {
                            return null;
                        }
                    }
                    // FIXME: Support other actions
                }
                // No supported actions were found, just bing
                return mDefaultBingRule;
            }
        }
    }
    // The default is to bing
    return mDefaultBingRule;
}