Ejemplo n.º 1
0
 /**
  * Fully reads an embedded document, reusing this parser
  *
  * @return the parsed document
  * @throws IOException if the document could not be read
  */
 protected Map<String, Object> readDocument() throws IOException {
   ObjectCodec codec = getCodec();
   if (codec == null) {
     throw new IllegalStateException(
         "Could not parse embedded document " + "because BSON parser has no codec");
   }
   _currToken = handleNewDocument(false);
   return codec.readValue(this, new TypeReference<Map<String, Object>>() {});
 }
 /**
  * Method for writing given Java object (POJO) as Json. Exactly how the object gets written
  * depends on object in question (ad on codec, its configuration); for most beans it will result
  * in Json object, but for others Json array, or String or numeric value (and for nulls, Json null
  * literal. <b>NOTE</b>: generator must have its <b>object codec</b> set to non-null value; for
  * generators created by a mapping factory this is the case, for others not.
  */
 @Override
 public void writeObject(Object pojo) throws IOException, JsonProcessingException {
   if (pojo == null) {
     writeNull();
   } else {
     objectCodec.writeValue(this, pojo);
   }
 }
Ejemplo n.º 3
0
    @Override
    public Volumes deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {

      List<Volume> volumes = new ArrayList<Volume>();
      ObjectCodec oc = jsonParser.getCodec();
      JsonNode node = oc.readTree(jsonParser);
      for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {

        Map.Entry<String, JsonNode> field = it.next();
        if (!field.getValue().equals(NullNode.getInstance())) {
          String path = field.getKey();
          Volume volume = new Volume(path);
          volumes.add(volume);
        }
      }
      return new Volumes(volumes.toArray(new Volume[0]));
    }
 private static <T> T toValue(ObjectCodec codec, JsonNode node, Class<T> valueType)
     throws JsonProcessingException {
   if (node.isPojo()) {
     Object pojo = ((POJONode) node).getPojo();
     if (valueType.isInstance(pojo)) {
       return valueType.cast(pojo);
     }
   }
   return codec.treeToValue(node, valueType);
 }
  @Override
  public CLAClassifier deserialize(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {

    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);

    CLAClassifier retVal = new CLAClassifier();
    retVal.alpha = node.get("alpha").asDouble();
    retVal.actValueAlpha = node.get("actValueAlpha").asDouble();
    retVal.learnIteration = node.get("learnIteration").asInt();
    retVal.recordNumMinusLearnIteration = node.get("recordNumMinusLearnIteration").asInt();
    retVal.maxBucketIdx = node.get("maxBucketIdx").asInt();

    String[] steps = node.get("steps").asText().split(",");
    TIntList t = new TIntArrayList();
    for (String step : steps) {
      t.add(Integer.parseInt(step));
    }
    retVal.steps = t;

    String[] tupleStrs = node.get("patternNZHistory").asText().split(";");
    Deque<Tuple> patterns = new Deque<Tuple>(tupleStrs.length);
    for (String tupleStr : tupleStrs) {
      String[] tupleParts = tupleStr.split("-");
      int iteration = Integer.parseInt(tupleParts[0]);
      String pattern = tupleParts[1].substring(1, tupleParts[1].indexOf("]")).trim();
      String[] indexes = pattern.split(",");
      int[] indices = new int[indexes.length];
      for (int i = 0; i < indices.length; i++) {
        indices[i] = Integer.parseInt(indexes[i].trim());
      }
      Tuple tup = new Tuple(iteration, indices);
      patterns.append(tup);
    }
    retVal.patternNZHistory = patterns;

    Map<Tuple, BitHistory> bitHistoryMap = new HashMap<Tuple, BitHistory>();
    String[] bithists = node.get("activeBitHistory").asText().split(";");
    for (String bh : bithists) {
      String[] parts = bh.split("-");

      String[] left = parts[0].split(",");
      Tuple iteration =
          new Tuple(Integer.parseInt(left[0].trim()), Integer.parseInt(left[1].trim()));

      BitHistory bitHistory = new BitHistory();
      String[] right = parts[1].split("=");
      bitHistory.id = right[0].trim();

      TDoubleList dubs = new TDoubleArrayList();
      String[] stats = right[1].substring(1, right[1].indexOf("}")).trim().split(",");
      for (int i = 0; i < stats.length; i++) {
        dubs.add(Double.parseDouble(stats[i].trim()));
      }
      bitHistory.stats = dubs;

      bitHistory.lastTotalUpdate = Integer.parseInt(right[2].trim());

      bitHistoryMap.put(iteration, bitHistory);
    }
    retVal.activeBitHistory = bitHistoryMap;

    ArrayNode jn = (ArrayNode) node.get("actualValues");
    List<Object> l = new ArrayList<Object>();
    for (int i = 0; i < jn.size(); i++) {
      JsonNode n = jn.get(i);
      try {
        double d = Double.parseDouble(n.asText().trim());
        l.add(d);
      } catch (Exception e) {
        l.add(n.asText().trim());
      }
    }
    retVal.actualValues = l;

    // Go back and set the classifier on the BitHistory objects
    for (Tuple tuple : bitHistoryMap.keySet()) {
      bitHistoryMap.get(tuple).classifier = retVal;
    }

    return retVal;
  }