当前位置: 首页>>代码示例>>Java>>正文


Java JSON.serialize方法代码示例

本文整理汇总了Java中com.mongodb.util.JSON.serialize方法的典型用法代码示例。如果您正苦于以下问题:Java JSON.serialize方法的具体用法?Java JSON.serialize怎么用?Java JSON.serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.mongodb.util.JSON的用法示例。


在下文中一共展示了JSON.serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getGardenLocationsAsJson

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 * Takes `uploadID` and returns all bed names as a json format string
 * @param uploadID - the year that the data was uploaded
 * @return String representation of json with all bed names
 */
public String getGardenLocationsAsJson(String uploadID){
    AggregateIterable<Document> documents
            = plantCollection.aggregate(
            Arrays.asList(
                    Aggregates.match(eq("uploadID", uploadID)), //!! Order is important here
                    Aggregates.group("$gardenLocation"),
                    Aggregates.sort(Sorts.ascending("_id"))
            ));

    List<Document> listDoc = new ArrayList<>();
    for (Document doc : documents) {
        listDoc.add(doc);
    }
    listDoc.sort(new BedComparator());

    return JSON.serialize(listDoc);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-dorfner-v2,代码行数:23,代码来源:PlantController.java

示例2: incrementMetadata

import com.mongodb.util.JSON; //导入方法依赖的package包/类
@Test
public void incrementMetadata() {
//This method is called from inside of getPlantByPlantId();
    boolean myPlant = plantController.
            incrementMetadata("58d1c36efb0cac4e15afd278", "pageViews");
    assertFalse(myPlant);
    boolean myPlant2 = plantController.incrementMetadata("16001.0","pageViews");
    assertTrue(myPlant2);


//This is necessary to test the data separately from getPlantByPlantId();
    Document searchDocument = new Document();
    searchDocument.append("id", "16001.0");
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase(databaseName);
    MongoCollection<Document> plantCollection = db.getCollection("plants");
    String before = JSON.serialize(plantCollection.find(searchDocument));
    plantController.incrementMetadata("16001.0","pageViews");
    String after = JSON.serialize(plantCollection.find(searchDocument));

    assertFalse(before.equals(after));
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:23,代码来源:UnorganizedTests.java

示例3: listPlants

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 * List all plants within the database, filtered by uploadId, gardenLocation and commonName
 * @param queryParams
 * @param uploadId
 * @return
 */
public String listPlants(Map<String, String[]> queryParams, String uploadId) {

    if (!ExcelParser.isValidUploadId(db, uploadId))
        return "null";

    //Create a filter based on query params
    Document filterDoc = new Document();
    filterDoc.append("uploadId", uploadId);

    if (queryParams.containsKey("gardenLocation")) {
        String location =(queryParams.get("gardenLocation")[0]);
        filterDoc = filterDoc.append("gardenLocation", location);
    }


    if (queryParams.containsKey("commonName")) {
        String commonName =(queryParams.get("commonName")[0]);
        filterDoc = filterDoc.append("commonName", commonName);
    }

    FindIterable<Document> matchingPlants = plantCollection.find(filterDoc);
    matchingPlants.sort(Sorts.ascending("commonName", "cultivar"));

    return JSON.serialize(matchingPlants);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:32,代码来源:PlantController.java

示例4: listPlants

import com.mongodb.util.JSON; //导入方法依赖的package包/类
public String listPlants(Map<String, String[]> queryParams, String uploadId) {
    Document filterDoc = new Document();
    filterDoc.append("uploadId", uploadId);

    if (queryParams.containsKey("gardenLocation")) {
        String location = (queryParams.get("gardenLocation")[0]);
        filterDoc = filterDoc.append("gardenLocation", location);
    }


    if (queryParams.containsKey("commonName")) {
        String commonName = (queryParams.get("commonName")[0]);
        filterDoc = filterDoc.append("commonName", commonName);
    }

    FindIterable<Document> matchingPlants = plantCollection.find(filterDoc);


    return JSON.serialize(matchingPlants);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:21,代码来源:PlantController.java

示例5: listPlants

import com.mongodb.util.JSON; //导入方法依赖的package包/类
public String listPlants(Map<String, String[]> queryParams, String uploadID) {
    Document filterDoc = new Document();
    filterDoc.append("uploadID", uploadID);

    if (queryParams.containsKey("gardenLocation")) {
        String location =(queryParams.get("gardenLocation")[0]);
        filterDoc = filterDoc.append("gardenLocation", location);
    }


    if (queryParams.containsKey("commonName")) {
        String commonName =(queryParams.get("commonName")[0]);
        filterDoc = filterDoc.append("commonName", commonName);
    }

    FindIterable<Document> matchingPlants = plantCollection.find(filterDoc);
    List<Document> sortedPlants = new ArrayList<>();
    for (Document doc : matchingPlants) {
        sortedPlants.add(doc);
    }
    sortedPlants.sort(new PlantComparator());
    return JSON.serialize(sortedPlants);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-dorfner-v2,代码行数:24,代码来源:PlantController.java

示例6: listFlowers

import com.mongodb.util.JSON; //导入方法依赖的package包/类
public String listFlowers(Map<String, String[]> queryParams) {
    Document filterDoc = new Document();

    if (queryParams.containsKey("cultivar")) {
        String targetCultivar = queryParams.get("cultivar")[0];
        filterDoc = filterDoc.append("cultivar", targetCultivar);
    }

    if (queryParams.containsKey("source")) {
        String targetSource = queryParams.get("source")[0];
        filterDoc = filterDoc.append("source", targetSource);
    }

    if (queryParams.containsKey("gardenLocation")) {
        String targetLocation = queryParams.get("gardenLocation")[0];
        filterDoc = filterDoc.append("gardenLocation", targetLocation);
    }

    if (queryParams.containsKey("year")) {
        int targetYear = Integer.parseInt(queryParams.get("year")[0]);
        filterDoc = filterDoc.append("year", targetYear);
    }

    FindIterable<Document> matchingFlowers = flowerCollection.find(filterDoc);

    return JSON.serialize(matchingFlowers);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-2-spraguesanborn,代码行数:28,代码来源:FlowerController.java

示例7: getAll

import com.mongodb.util.JSON; //导入方法依赖的package包/类
@Override
public List<T> getAll() {
    T resultElement;
    List<T> result = new ArrayList<>();
    try {
        FindIterable documents = this.collection.find();
        for (Object document : documents) {
            String documentToJson = JSON.serialize(document);
            resultElement = this.jsonConverter.fromJson(documentToJson, this.entryType);
            result.add(resultElement);
        }
    } catch (RuntimeException e) {
        LOGGER.error(e);
    }

    return result;
}
 
开发者ID:rndsolutions,项目名称:hawkcd,代码行数:18,代码来源:MongoDbRepository.java

示例8: createObject

import com.mongodb.util.JSON; //导入方法依赖的package包/类
protected T createObject(final DBObject dbObject) {
  if (null == dbObject) {
    return null;
  }
  String jsonString = JSON.serialize(dbObject);
  try {
    return objectReader.readValue(jsonString);
  } catch (IOException e) {
    LOGGER.error(
        String.format(
            "Error while retrieving object of type [%s] from [%s].",
            clazz.getName(),
            jsonString),
        e);
    throw new IllegalArgumentException(e);
  }
}
 
开发者ID:melphi,项目名称:onplan,代码行数:18,代码来源:AbstractMongoDbDao.java

示例9: mapJsonToInstance

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 * Maps the given raw JSON string onto the provided class instance.
 *
 * @param jsonString    raw JSON string
 * @param type          object class
 * @param objectMapper  custom object mapper
 * @return a list of instances of the provided class
 * @throws ResourceMappingException
 */
private List<T> mapJsonToInstance(String jsonString, Class<T> type, ObjectMapper objectMapper)
    throws ResourceMappingException
{
    List<T> list = new ArrayList<>();
    ObjectMapper mapper = objectMapper;

    if (mapper == null)
    {
        mapper = new ObjectMapper();
    }

    try {
        for (DBObject dbObject: Transformer.convertToDbObject(jsonString))
        {
            String toMap = JSON.serialize(dbObject);

            // map json string onto the given class type
            list.add(mapper.readValue(toMap, type));
        }
    } catch (Exception e) {
        throw new ResourceMappingException(e.getMessage());
    }

    return list;
}
 
开发者ID:genome-nexus,项目名称:genome-nexus,代码行数:35,代码来源:ExternalResourceTransformer.java

示例10: getPlantFeedbackByPlantIdJSON

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 *
 * @param plantID The plant to get feedback of
 * @param uploadID Dataset to find the plant
 *
 * @return JSON for the number of comments, likes, and dislikes
 * Of the form:
 * {
 *     commentCount: number
 *     likeCount: number
 *     dislikeCount: number
 * }
 */

public String getPlantFeedbackByPlantIdJSON(String plantID, String uploadID) {
    if (!ExcelParser.isValidUploadId(db, uploadID))
        return "null";
    Document out = new Document();

    //Get feedback then package it in a JSON(BSON) Document
    long[] metadataCount = getPlantFeedbackByPlantId(plantID, uploadID);

    out.put("likeCount", metadataCount[0]);
    out.put("dislikeCount", metadataCount[1]);
    out.put("commentCount", metadataCount[2]);
    return JSON.serialize(out);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:28,代码来源:PlantController.java

示例11: getCommonNamesJSON

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 * Get a json containing a list of commonNames sorted by common name
 * @param uploadID
 * @return
 */
public String getCommonNamesJSON(String uploadID){
    if (!ExcelParser.isValidUploadId(db, uploadID))
        return "null";

    AggregateIterable<Document> documents
            = plantCollection.aggregate(
            Arrays.asList(
                    Aggregates.match(eq("uploadId", uploadID)), //!! Order is important here
                    Aggregates.group("$commonName"),
                    Aggregates.sort(Sorts.ascending("commonName"))
            ));
    return JSON.serialize(documents);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:19,代码来源:PlantController.java

示例12: getFeedbackForPlantByPlantID

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
 * @param plantID  The plant to get feedback of
 * @param uploadID Dataset to find the plant
 * @return JSON for the number of comments, likes, and dislikes
 * Of the form:All
 * {
 * commentCount: number
 * likeCount: number
 * dislikeCount: number
 * }
 */

public String getFeedbackForPlantByPlantID(String plantID, String uploadID) {
    Document out = new Document();

    Document filter = new Document();
    filter.put("commentOnPlant", plantID);
    filter.put("uploadId", uploadID);
    long comments = commentCollection.count(filter);
    long likes = 0;
    long dislikes = 0;


    //Get a plant by plantID
    FindIterable doc = plantCollection.find(new Document().append("id", plantID).append("uploadId", uploadID));

    Iterator iterator = doc.iterator();
    if (iterator.hasNext()) {
        Document result = (Document) iterator.next();

        //Get metadata.rating array
        List<Document> ratings = (List<Document>) ((Document) result.get("metadata")).get("ratings");

        //Loop through all of the entries within the array, counting like=true(like) and like=false(dislike)
        for (Document rating : ratings) {
            if (rating.get("like").equals(true))
                likes++;
            else if (rating.get("like").equals(false))
                dislikes++;
        }
    }


    out.put("commentCount", comments);
    out.put("likeCount", likes);
    out.put("dislikeCount", dislikes);
    return JSON.serialize(out);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:49,代码来源:PlantController.java

示例13: getGardenLocationsAsJson

import com.mongodb.util.JSON; //导入方法依赖的package包/类
public String getGardenLocationsAsJson(String uploadID) {
    AggregateIterable<Document> documents
            = plantCollection.aggregate(
            Arrays.asList(
                    Aggregates.match(eq("uploadId", uploadID)), //!! Order is important here
                    Aggregates.group("$gardenLocation"),
                    Aggregates.sort(Sorts.ascending("_id"))
            ));
    return JSON.serialize(documents);
}
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:11,代码来源:PlantController.java

示例14: listUploadIds

import com.mongodb.util.JSON; //导入方法依赖的package包/类
/**
     *
     * @return a sorted JSON array of all the distinct uploadIds in the DB
     */
    public String listUploadIds() {
        AggregateIterable<Document> documents
                = plantCollection.aggregate(
                Arrays.asList(
                        Aggregates.group("$uploadId"),
                        Aggregates.sort(Sorts.ascending("_id"))
                ));
        List<String> lst = new LinkedList<>();
        for(Document d: documents) {
            lst.add(d.getString("_id"));
        }
        return JSON.serialize(lst);
//        return JSON.serialize(plantCollection.distinct("uploadId","".getClass()));
    }
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:19,代码来源:PlantController.java

示例15: testSetLiveUploadID

import com.mongodb.util.JSON; //导入方法依赖的package包/类
@Test
public  void testSetLiveUploadID() {
     parser.setLiveUploadId("JAPSER");


     Document searchDocument = new Document();
     searchDocument.append("liveUploadId", "JAPSER");
     MongoClient mongoClient = new MongoClient();
     MongoDatabase test = mongoClient.getDatabase("test");
     MongoCollection configCollection = test.getCollection("config");
     String uploadID = JSON.serialize(configCollection.find(searchDocument));
     assertEquals(80,uploadID.length());
 }
 
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-3-sixguysburgers-fries,代码行数:14,代码来源:TestExcelParser.java


注:本文中的com.mongodb.util.JSON.serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。