Java 类org.bson.json.JsonParseException 实例源码

项目:restheart    文件:JsonUtils.java   
/**
 * @param json
 * @return either a BsonDocument or a BsonArray from the json string
 * @throws JsonParseException
 */
public static BsonValue parse(String json)
        throws JsonParseException {
    if (json == null) {
        return null;
    }

    String trimmed = json.trim();

    if (trimmed.startsWith("{")) {
        try {
            return BsonDocument.parse(json);
        } catch (BsonInvalidOperationException ex) {
            // this can happen parsing a bson type, e.g.
            // {"$oid": "xxxxxxxx" }
            // the string starts with { but is not a document
            return getBsonValue(json);
        }
    } else if (trimmed.startsWith("[")) {
        return BSON_ARRAY_CODEC.decode(
                new JsonReader(json),
                DecoderContext.builder().build());
    } else {
        return getBsonValue(json);
    }
}
项目:restheart    文件:RequestContext.java   
/**
 *
 * @return the $and composed filter qparam values
 */
public BsonDocument getFiltersDocument() throws JsonParseException {
    final BsonDocument filterQuery = new BsonDocument();

    if (filter != null) {
        if (filter.size() > 1) {
            BsonArray _filters = new BsonArray();

            filter.stream().forEach((String f) -> {
                _filters.add(BsonDocument.parse(f));
            });

            filterQuery.put("$and", _filters);
        } else if (filter.size() == 1) {
            filterQuery.putAll(BsonDocument.parse(filter.getFirst()));  // this can throw JsonParseException for invalid filter parameters
        } else {
            return filterQuery;
        }
    }

    return filterQuery;
}
项目:microcks    文件:DynamicMockRestController.java   
@RequestMapping(value = "/{service}/{version}/{resource}", method = RequestMethod.POST)
public ResponseEntity<String> createResource(
      @PathVariable("service") String serviceName,
      @PathVariable("version") String version,
      @PathVariable("resource") String resource,
      @RequestParam(value="delay", required=false) Long delay,
      @RequestBody(required=true) String body,
      HttpServletRequest request
) {
   log.debug("Creating a new resource '{}' for service '{}-{}'", resource, serviceName, version);
   long startTime = System.currentTimeMillis();

   MockContext mockContext = getMockContext(serviceName, version, "POST /" + resource);
   if (mockContext != null) {
      Document document = null;
      GenericResource genericResource = null;

      try {
         // Try parsing body payload that should be json.
         document = Document.parse(body);
         // Now create a generic resource.
         genericResource = new GenericResource();
         genericResource.setServiceId(mockContext.service.getId());
         genericResource.setPayload(document);

         genericResource = genericResourceRepository.save(genericResource);
      } catch (JsonParseException jpe) {
         // Return a 422 code : unprocessable entity.
         return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
      }

      // Append id and wait if specified before returning.
      document.append(ID_FIELD, genericResource.getId());
      waitForDelay(startTime, delay, mockContext);
      return new ResponseEntity<>(document.toJson(), HttpStatus.CREATED);
   }
   // Return a 400 code : bad request.
   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
项目:restheart    文件:CollectionDAO.java   
/**
 * Returs the FindIterable<BsonDocument> of the collection applying sorting,
 * filtering and projection.
 *
 * @param sortBy the sort expression to use for sorting (prepend field name
 * with - for descending sorting)
 * @param filters the filters to apply.
 * @param keys the keys to return (projection)
 * @return
 * @throws JsonParseException
 */
FindIterable<BsonDocument> getFindIterable(
        final MongoCollection<BsonDocument> coll,
        final BsonDocument sortBy,
        final BsonDocument filters,
        final BsonDocument keys) throws JsonParseException {

    return coll.find(filters)
            .projection(keys)
            .sort(sortBy)
            .batchSize(BATCH_SIZE)
            .maxTime(Bootstrapper.getConfiguration().getQueryTimeLimit(), TimeUnit.MILLISECONDS);
}
项目:restheart    文件:CsvLoaderHandler.java   
private BsonValue getBsonValue(String raw) {
    try {
        return JsonUtils.parse(raw);
    } catch (JsonParseException jpe) {
        return new BsonString(raw);
    }
}
项目:restheart    文件:RequestContext.java   
public BsonDocument getSortByDocument() throws JsonParseException {
    BsonDocument sort = new BsonDocument();

    if (sortBy == null) {
        sort.put("_id", new BsonInt32(-1));
    } else {
        sortBy.stream().forEach((s) -> {

            String _s = s.trim(); // the + sign is decoded into a space, in case remove it

            // manage the case where sort_by is a json object
            try {
                BsonDocument _sort = BsonDocument.parse(_s);

                sort.putAll(_sort);
            } catch (JsonParseException e) {
                // sort_by is just a string, i.e. a property name
                if (_s.startsWith("-")) {
                    sort.put(_s.substring(1), new BsonInt32(-1));
                } else if (_s.startsWith("+")) {
                    sort.put(_s.substring(1), new BsonInt32(11));
                } else {
                    sort.put(_s, new BsonInt32(1));
                }
            }
        });
    }

    return sort;
}
项目:restheart    文件:RequestContext.java   
public BsonDocument getProjectionDocument() throws JsonParseException {
    final BsonDocument projection = new BsonDocument();

    if (keys == null || keys.isEmpty()) {
        return null;
    } else {
        keys.stream().forEach((String f) -> {
            projection.putAll(BsonDocument.parse(f));  // this can throw JsonParseException for invalid keys parameters
        });
    }

    return projection;
}
项目:microcks    文件:DynamicMockRestController.java   
@RequestMapping(value = "/{service}/{version}/{resource}/{resourceId}", method = RequestMethod.PUT)
public ResponseEntity<String> updateResource(
      @PathVariable("service") String serviceName,
      @PathVariable("version") String version,
      @PathVariable("resource") String resource,
      @PathVariable("resourceId") String resourceId,
      @RequestParam(value="delay", required=false) Long delay,
      @RequestBody(required=true) String body,
      HttpServletRequest request
) {
   log.debug("Update resource '{}:{}' for service '{}-{}'", resource, resourceId, serviceName, version);
   long startTime = System.currentTimeMillis();

   MockContext mockContext = getMockContext(serviceName, version, "PUT /" + resource + "/:id");
   if (mockContext != null) {
      // Get the requested generic resource.
      GenericResource genericResource = genericResourceRepository.findOne(resourceId);
      if (genericResource != null) {
         Document document = null;

         try {
            // Try parsing body payload that should be json.
            document = Document.parse(body);
            document.remove(ID_FIELD);

            // Now update the generic resource payload.
            genericResource.setPayload(document);

            genericResourceRepository.save(genericResource);
         } catch (JsonParseException jpe) {
            // Return a 422 code : unprocessable entity.
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
         }
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return the updated resource as well as a 200 code.
         return new ResponseEntity<>(transformToResourceJSON(genericResource), HttpStatus.OK);

      } else {
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return a 404 code : not found.
         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }
   }

   // Return a 400 code : bad request.
   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}