Skip to content

Commit 2b36e3e

Browse files
committed
JAVA-1929: Add static parse method to BsonArray
1 parent 560d249 commit 2b36e3e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

bson/src/main/org/bson/BsonArray.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.bson;
1818

19+
import org.bson.codecs.BsonArrayCodec;
20+
import org.bson.codecs.DecoderContext;
21+
import org.bson.json.JsonReader;
22+
1923
import java.util.ArrayList;
2024
import java.util.Collection;
2125
import java.util.Collections;
@@ -48,6 +52,20 @@ public BsonArray() {
4852
values = new ArrayList<BsonValue>();
4953
}
5054

55+
/**
56+
* Parses a string in MongoDB Extended JSON format to a {@code BsonArray}
57+
*
58+
* @param json the JSON string
59+
* @return a corresponding {@code BsonArray} object
60+
* @see org.bson.json.JsonReader
61+
* @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON
62+
*
63+
* @since 3.4
64+
*/
65+
public static BsonArray parse(final String json) {
66+
return new BsonArrayCodec().decode(new JsonReader(json), DecoderContext.builder().build());
67+
}
68+
5169
/**
5270
* Gets the values in this array as a list of {@code BsonValue} objects.
5371
*

bson/src/main/org/bson/codecs/BsonArrayCodec.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,28 @@
2727
import java.util.List;
2828

2929
import static org.bson.assertions.Assertions.notNull;
30+
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
3031

3132
/**
3233
* A codec for BsonArray instances.
3334
*
3435
* @since 3.0
3536
*/
3637
public class BsonArrayCodec implements Codec<BsonArray> {
38+
39+
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(new BsonValueCodecProvider());
40+
3741
private final CodecRegistry codecRegistry;
3842

43+
/**
44+
* Creates a new instance with a default codec registry that uses the {@link BsonValueCodecProvider}.
45+
*
46+
* @since 3.4
47+
*/
48+
public BsonArrayCodec() {
49+
this(DEFAULT_REGISTRY);
50+
}
51+
3952
/**
4053
* Construct an instance with the given registry
4154
*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2016 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.bson
19+
20+
import spock.lang.Specification
21+
22+
class BsonArraySpecification extends Specification {
23+
24+
def 'should be array type'() {
25+
expect:
26+
new BsonArray().getBsonType() == BsonType.ARRAY
27+
}
28+
29+
def 'should construct empty array'() {
30+
when:
31+
def array = new BsonArray()
32+
33+
then:
34+
array.isEmpty()
35+
array.size() == 0
36+
array.getValues().isEmpty()
37+
}
38+
39+
def 'should construct from a list'() {
40+
given:
41+
def list = [BsonBoolean.TRUE, BsonBoolean.FALSE]
42+
43+
when:
44+
def array = new BsonArray(list)
45+
46+
then:
47+
!array.isEmpty()
48+
array.size() == 2
49+
array.getValues() == list
50+
}
51+
52+
def 'should parse json'() {
53+
expect:
54+
BsonArray.parse('[1, true]') == new BsonArray([new BsonInt32(1), BsonBoolean.TRUE])
55+
}
56+
}

0 commit comments

Comments
 (0)