Java 类com.mongodb.util.JSONSerializers 实例源码

项目:lumongo    文件:IndexesResource.java   
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

    try {
        Lumongo.GetIndexesResponse getIndexesResponse = indexManager.getIndexes(Lumongo.GetIndexesRequest.newBuilder().build());

        Document mongoDocument = new org.bson.Document();
        mongoDocument.put("indexes", getIndexesResponse.getIndexNameList());
        String docString = JSONSerializers.getStrict().serialize(mongoDocument);

        if (pretty) {
            docString = JsonWriter.formatJson(docString);
        }

        return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

    }
    catch (Exception e) {
        return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get index names: " + e.getMessage()).build();
    }

}
项目:lumongo    文件:FieldsResource.java   
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.INDEX) final String indexName,
        @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

    if (indexName != null) {

        Lumongo.GetFieldNamesRequest fieldNamesRequest = Lumongo.GetFieldNamesRequest.newBuilder().setIndexName(indexName).build();

        Lumongo.GetFieldNamesResponse fieldNamesResponse;

        try {
            fieldNamesResponse = indexManager.getFieldNames(fieldNamesRequest);

            Document mongoDocument = new Document();
            mongoDocument.put("index", indexName);
            mongoDocument.put("fields", fieldNamesResponse.getFieldNameList());

            String docString = JSONSerializers.getStrict().serialize(mongoDocument);

            if (pretty) {
                docString = JsonWriter.formatJson(docString);
            }

            return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

        }
        catch (Exception e) {
            return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to fetch fields for index <" + indexName + ">: " + e.getMessage())
                    .build();
        }
    }
    else {
        return Response.status(LumongoConstants.INTERNAL_ERROR).entity("No index defined").build();
    }

}
项目:lumongo    文件:StatsResource.java   
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

    try {

        Document mongoDocument = new Document();

        mongoDocument.put("indexBlockSize", indexManager.getClusterConfig().getIndexBlockSize());
        mongoDocument.put("maxIndexBlockCount", indexManager.getClusterConfig().getMaxIndexBlocks());
        mongoDocument.put("currentIndexBlockCount", MongoFile.getCacheSize());

        Runtime runtime = Runtime.getRuntime();

        mongoDocument.put("jvmUsedMemoryMB", (runtime.totalMemory() - runtime.freeMemory()) / MB);
        mongoDocument.put("jvmFreeMemoryMB", runtime.freeMemory() / MB);
        mongoDocument.put("jvmTotalMemoryMB", runtime.totalMemory() / MB);
        mongoDocument.put("jvmMaxMemoryMB", runtime.maxMemory() / MB);

        String docString = JSONSerializers.getStrict().serialize(mongoDocument);

        if (pretty) {
            docString = JsonWriter.formatJson(docString);
        }

        return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

    }
    catch (Exception e) {
        return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get cluster membership: " + e.getMessage()).build();
    }

}
项目:mongodb-aggregate-query-support    文件:AbstractAggregateQueryProvider.java   
private static String serialize(Object object) {
  StringBuilder buf = new StringBuilder();
  JSONSerializers.getStrict().serialize(object, buf);
  return buf.toString();
}
项目:lumongo    文件:MembersResource.java   
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

    try {
        Lumongo.GetMembersResponse getMembersResponse = indexManager.getMembers(Lumongo.GetMembersRequest.newBuilder().build());

        org.bson.Document mongoDocument = new org.bson.Document();

        List<Document> memberObjList = new ArrayList<>();
        for (Lumongo.LMMember lmMember : getMembersResponse.getMemberList()) {
            Document memberObj = new Document();
            memberObj.put("serverAddress", lmMember.getServerAddress());
            memberObj.put("hazelcastPort", lmMember.getHazelcastPort());
            memberObj.put("internalPort", lmMember.getInternalPort());
            memberObj.put("externalPort", lmMember.getExternalPort());

            Document indexMappingObj = new Document();
            for (Lumongo.IndexMapping indexMapping : getMembersResponse.getIndexMappingList()) {

                TreeSet<Integer> segments = new TreeSet<>();
                for (Lumongo.SegmentMapping segmentMapping : indexMapping.getSegmentMappingList()) {
                    if (segmentMapping.getMember().equals(lmMember)) {
                        segments.add(segmentMapping.getSegmentNumber());
                    }
                }

                indexMappingObj.put(indexMapping.getIndexName(), segments);
            }
            memberObj.put("indexMapping", indexMappingObj);


            memberObjList.add(memberObj);

        }

        mongoDocument.put("members", memberObjList);

        String docString = JSONSerializers.getStrict().serialize(mongoDocument);

        if (pretty) {
            docString = JsonWriter.formatJson(docString);
        }

        return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

    }
    catch (Exception e) {
        return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get cluster membership: " + e.getMessage()).build();
    }

}
项目:lumongo    文件:FetchResource.java   
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.ID) final String uniqueId,
        @QueryParam(LumongoConstants.INDEX) final String indexName, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

    Lumongo.FetchRequest.Builder fetchRequest = Lumongo.FetchRequest.newBuilder();
    fetchRequest.setIndexName(indexName);
    fetchRequest.setUniqueId(uniqueId);

    Lumongo.FetchResponse fetchResponse;

    try {
        fetchResponse = indexManager.fetch(fetchRequest.build());

        if (fetchResponse.hasResultDocument()) {
            Document document = ResultHelper.getDocumentFromResultDocument(fetchResponse.getResultDocument());
            String docString;
            if (pretty) {
                docString = JSONSerializers.getLegacy().serialize(document);
            }
            else {
                docString = JSONSerializers.getStrict().serialize(document);
            }

            if (pretty) {
                docString = JsonWriter.formatJson(docString);
            }

            return Response.status(LumongoConstants.SUCCESS).entity(docString).build();
        }
        else {
            return Response.status(LumongoConstants.NOT_FOUND).entity("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">").build();
        }

    }
    catch (Exception e) {
        return Response.status(LumongoConstants.INTERNAL_ERROR)
                .entity("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">: " + e.getMessage()).build();
    }

}
项目:HZS.Durian    文件:BasicBSONObject.java   
/** Returns a JSON serialization of this object
 * @return JSON serialization
 */
public String toString(){
    return JSONSerializers.getStrict().serialize(this);
}