Example usage for com.google.gson JsonNull INSTANCE

List of usage examples for com.google.gson JsonNull INSTANCE

Introduction

In this page you can find the example usage for com.google.gson JsonNull INSTANCE.

Prototype

JsonNull INSTANCE

To view the source code for com.google.gson JsonNull INSTANCE.

Click Source Link

Document

singleton for JsonNull

Usage

From source file:org.fenixedu.cms.ui.AdminPostsService.java

License:Open Source License

public JsonObject serializePost(Post post) {
    JsonObject postJson = new JsonObject();
    JsonArray categoriesJson = new JsonArray();
    JsonArray filesJson = new JsonArray();
    if (canDoThis(post.getSite(), Permission.LIST_CATEGORIES, Permission.EDIT_CATEGORY)) {
        boolean canUsePrivileged = canDoThis(post.getSite(), Permission.USE_PRIVILEGED_CATEGORY);
        post.getSite().getCategoriesSet().stream().filter(cat -> canUsePrivileged || !cat.getPrivileged())
                .sorted(Category.CATEGORY_NAME_COMPARATOR).map(category -> serializeCategory(category, post))
                .forEach(categoriesJson::add);
    }//from  w ww . j a va2s.  c o m
    post.getFilesSorted().stream().map(this::serializePostFile).forEach(filesJson::add);
    postJson.addProperty("slug", post.getSlug());
    postJson.add("name", ofNullable(post.getName()).map(LocalizedString::json).orElseGet(JsonObject::new));
    postJson.add("body", ofNullable(post.getBody()).map(LocalizedString::json).orElseGet(JsonObject::new));
    postJson.add("excerpt",
            ofNullable(post.getExcerpt()).map(LocalizedString::json).orElseGet(JsonObject::new));

    if (PermissionEvaluation.canDoThis(post.getSite(), Permission.SEE_METADATA)) {
        postJson.add("metadata",
                ofNullable(post.getMetadata()).map(PostMetadata::json).orElseGet(JsonObject::new));
    } else {
        postJson.add("metadata", JsonNull.INSTANCE);
    }
    postJson.add("categories", categoriesJson);
    postJson.add("files", filesJson);
    postJson.addProperty("address", post.getAddress());

    if (canPublish(post)) {
        postJson.addProperty("active", post.getActive());
        postJson.addProperty("canViewGroup", post.getCanViewGroup().getExpression());
        postJson.add("createdBy", UserResource.getBuilder().view(post.getCreatedBy()));
        postJson.addProperty("publicationBegin",
                ofNullable(post.getPublicationBegin()).map(DateTime::toString).orElse(null));
        postJson.addProperty("publicationEnd",
                ofNullable(post.getPublicationEnd()).map(DateTime::toString).orElse(null));
    }

    return postJson;
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.ArrayElementJsonAccessor.java

License:LGPL

private static void fillTo(JsonArray array, int index) {
    for (int i = array.size(); i <= index; ++i) {
        array.add(JsonNull.INSTANCE);
    }/*  ww  w  .jav a  2s .c om*/
}

From source file:org.hibernate.search.backend.elasticsearch.impl.ElasticsearchIndexWorkVisitor.java

License:LGPL

private JsonObject getOrCreateDocumentTree(JsonObject source, String path) {
    if (path == null) {
        return source;
    }//from  w  w w  . ja  va2s.  c  om

    // embedded property Create JSON hierarchy as needed
    String[] parts = DOT.split(path);
    JsonObject parent = source;

    for (int i = 0; i < parts.length; i++) {
        Matcher nameAndIndex = NAME_AND_INDEX.matcher(parts[i]);
        nameAndIndex.matches();

        String name = nameAndIndex.group(1);
        String idx = nameAndIndex.group(3);
        Integer index = null;

        if (idx != null) {
            index = Integer.valueOf(idx);
            JsonArray array = parent.getAsJsonArray(name);
            if (array == null) {
                array = new JsonArray();
                parent.add(name, array);
            }

            JsonObject newParent = index < array.size() ? array.get(index).getAsJsonObject() : null;
            if (newParent == null) {
                newParent = new JsonObject();

                if (index >= array.size()) {
                    for (int j = array.size(); j <= index; j++) {
                        array.add(JsonNull.INSTANCE);
                    }
                }
                array.set(index, newParent);
            }

            parent = newParent;
        } else {
            JsonObject newParent = parent.getAsJsonObject(name);
            if (newParent == null) {
                newParent = new JsonObject();
                parent.add(name, newParent);
            }
            parent = newParent;
        }
    }

    return parent;
}

From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.GeoPointFieldCodec.java

License:LGPL

@Override
public JsonElement encode(GeoPoint value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }/* w ww  .ja  v a  2s.c  om*/
    JsonObject result = new JsonObject();
    LATITUDE_ACCESSOR.set(result, value.getLatitude());
    LONGITUDE_ACCESSOR.set(result, value.getLongitude());
    return result;
}

From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.IntegerFieldCodec.java

License:LGPL

@Override
public JsonElement encode(Integer value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }/*from  w  ww . j a va2s  .c o m*/
    return new JsonPrimitive(value);
}

From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.JsonStringFieldCodec.java

License:LGPL

@Override
public JsonElement encode(String value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }//from   w w w.j ava 2 s .  c  o  m
    return gson.fromJson(value, JsonElement.class);
}

From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.LocalDateFieldCodec.java

License:LGPL

@Override
public JsonElement encode(LocalDate value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }/* w ww. jav a2 s.  c  o  m*/
    return new JsonPrimitive(delegate.format(value));
}

From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.StringFieldCodec.java

License:LGPL

@Override
public JsonElement encode(String value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }//from  w  w w.  j  a  v  a2s . c  o  m
    return new JsonPrimitive(value);
}

From source file:org.jboss.weld.logging.Json.java

License:Apache License

static JsonElement wrapPrimitive(Object value) {
    if (value == null) {
        return JsonNull.INSTANCE;
    }//ww  w . j  av  a2  s .c  o m
    if (value instanceof Number) {
        return new JsonPrimitive((Number) value);
    }
    if (value instanceof Boolean) {
        return new JsonPrimitive((Boolean) value);
    }
    return new JsonPrimitive(value.toString());
}

From source file:org.jboss.weld.logging.LogMessageIndexDiff.java

License:Apache License

/**
 * Generates the JSON diff for the specified index files.
 *
 * @param indexFiles//ww w .  j  a va  2  s.co m
 * @param outputFile
 * @param detectCollisionsOnly
 * @return
 */
public JsonObject generate(List<File> indexFiles, boolean detectCollisionsOnly) {

    if (indexFiles.size() < 2) {
        throw new IllegalStateException("More than one index file must be specified: " + indexFiles);
    }

    // First parse the index files
    List<JsonObject> indexes = parseIndexFiles(indexFiles);

    // Build indexes metadata and check compared versions
    JsonArray indexesMeta = new JsonArray();
    List<String> indexesIds = new ArrayList<String>();
    Set<JsonElement> versions = new HashSet<JsonElement>();
    for (Iterator<JsonObject> iterator = indexes.iterator(); iterator.hasNext();) {
        JsonObject index = iterator.next();
        JsonElement version = index.get(VERSION);
        versions.add(version);
        String indexId = version.getAsString() + index.get(ARTIFACT).getAsString();
        if (indexesIds.contains(indexId)) {
            throw new IllegalStateException(
                    "Unable to compare index files with the same composite identifier (version and artifact id): "
                            + indexId);
        }
        indexesIds.add(indexId);
        JsonObject indexMeta = new JsonObject();
        indexMeta.add(VERSION, version);
        indexMeta.add(ARTIFACT, index.get(ARTIFACT));
        indexMeta.add(TOTAL, index.get(TOTAL));
        indexMeta.add(FILE_PATH, index.get(FILE_PATH));
        indexesMeta.add(indexMeta);
    }

    // Now let's find the differences
    // Note that messages don't need to have the ID specified (0) or may inherit the ID from another message with the same name (-1)
    JsonArray differences = findDifferences(versions.size(), detectCollisionsOnly, buildDataMap(indexes));

    JsonObject diff = new JsonObject();
    diff.add(INDEXES, indexesMeta);
    diff.add(DETECT_COLLISIONS_ONLY, Json.wrapPrimitive(detectCollisionsOnly));
    diff.add(TOTAL, Json.wrapPrimitive(differences.size()));
    diff.add(DIFFERENCES, differences.size() > 0 ? differences : JsonNull.INSTANCE);
    return diff;
}