コード例 #1
0
 @org.testng.annotations.Test
 public void testJSONEncoding() throws ParseException {
   String json =
       "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
   BasicDBObject a = (BasicDBObject) JSON.parse(json);
   assert (a.get("str").equals("asdfasd"));
   assert (a.get("int").equals(5));
   assert (a.get("long").equals(123123123123L));
   assert (a.get("float").equals(0.4d));
   assert (a.get("bool").equals(false));
   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
   format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
   assert (a.get("date").equals(format.parse("2011-05-18T18:56:00Z")));
   Pattern pat = (Pattern) a.get("pat");
   Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
   assert (pat.pattern().equals(pat2.pattern()));
   assert (pat.flags() == (pat2.flags()));
   ObjectId oid = (ObjectId) a.get("oid");
   assert (oid.equals(new ObjectId("4d83ab3ea39562db9c1ae2ae")));
   DBRef ref = (DBRef) a.get("ref");
   assert (ref.equals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af"))));
   assert (a.get("code").equals(new Code("asdfdsa")));
   assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject())));
   assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10)));
   assert (a.get("uuid").equals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5")));
   String json2 = JSON.serialize(a);
   BasicDBObject b = (BasicDBObject) JSON.parse(json2);
   a.equals(b);
   assert (a.equals(b));
 }
コード例 #2
0
  public Object objectDone() {
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(_lastName, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        o = format.parse((String) b.get("$date"), new ParsePosition(0));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      }
    }

    return o;
  }
コード例 #3
0
  public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    // override the object if it's a special type
    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(name, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
        format.setCalendar(calendar);
        String txtdate = (String) b.get("$date");
        o = format.parse(txtdate, new ParsePosition(0));
        if (o == null) {
          // try older format with no ms
          format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
          format.setCalendar(calendar);
          o = format.parse(txtdate, new ParsePosition(0));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ts")) {
        Long ts = ((Number) b.get("$ts")).longValue();
        Long inc = ((Number) b.get("$inc")).longValue();
        o = new BSONTimestamp(ts.intValue(), inc.intValue());
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
          o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
          o = new Code((String) b.get("$code"));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ref")) {
        o = new DBRef(null, (String) b.get("$ref"), b.get("$id"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$minKey")) {
        o = new MinKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      }
    }
    return o;
  }