當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。