Java 类org.bson.codecs.BsonArrayCodec 实例源码

项目:baleen    文件:SharedFongoResource.java   
@Override
protected boolean doInitialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    // Work whether it's a list of DB Objects or a single
    if ("{}".equals(fongoData) || "[]".equals(fongoData) || Strings.isNullOrEmpty(fongoData)) {
        return true;
    }

    if (fongoData.trim().startsWith("[")) {
        CodecRegistry codecRegistry = CodecRegistries.fromProviders(Arrays.asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider()));
        JsonReader reader = new JsonReader(fongoData);
        BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);

        BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build());

        for(BsonValue doc : docArray.getValues()){
            fongo.getDatabase(BALEEN).getCollection(fongoCollection).insertOne(Document.parse(doc.asDocument().toJson()));
        }       
    } else if (fongoData.trim().startsWith("{")) {
        Document data = Document.parse(fongoData);
        fongo.getDatabase(BALEEN).getCollection(fongoCollection).insertOne(data);
    } else {
        getMonitor().error("Unsupported type");
        throw new ResourceInitializationException();
    }

    return true;
}
项目:json-parse-helper    文件:JsonParseHelper.java   
private static BsonArray parseBsonArray(final String json) {
    return new BsonArrayCodec(CodecRegistries.fromProviders(new BsonValueCodecProvider())).decode(new JsonReader(json), DecoderContext.builder().build());
}