diff --git a/bson/src/main/org/bson/AbstractBsonWriter.java b/bson/src/main/org/bson/AbstractBsonWriter.java index b256c9b5545..8ef3f8dba99 100644 --- a/bson/src/main/org/bson/AbstractBsonWriter.java +++ b/bson/src/main/org/bson/AbstractBsonWriter.java @@ -321,7 +321,7 @@ public void writeStartArray(final String name) { @Override public void writeStartArray() { - checkPreconditions("writeStartArray", State.VALUE); + checkPreconditions("writeStartArray", State.INITIAL, State.VALUE, State.SCOPE_DOCUMENT, State.DONE); if (context != null && context.name != null) { fieldNameValidatorStack.push(fieldNameValidatorStack.peek().getValidatorForField(getName())); diff --git a/bson/src/main/org/bson/BsonArray.java b/bson/src/main/org/bson/BsonArray.java index 876858b01b0..be8b555a71e 100644 --- a/bson/src/main/org/bson/BsonArray.java +++ b/bson/src/main/org/bson/BsonArray.java @@ -20,6 +20,7 @@ import org.bson.codecs.DecoderContext; import org.bson.json.JsonReader; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -234,11 +235,34 @@ public int hashCode() { return values.hashCode(); } + /** + * Gets a JSON representation of this array using the {@link org.bson.json.JsonMode#RELAXED} output mode, and otherwise the default + * settings of {@link JsonWriterSettings.Builder}. + * + * @return a JSON representation of this array + * @see #toJson(JsonWriterSettings) + * @see JsonWriterSettings + */ + public String toJson() { + return toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build()); + } + + /** + * Gets a JSON representation of this array using the given + * {@code JsonWriterSettings}. + * + * @param settings the JSON writer settings + * @return a JSON representation of this array + */ + public String toJson(final JsonWriterSettings settings) { + StringWriter writer = new StringWriter(); + new BsonArrayCodec().encode(new JsonWriterExt(writer, settings), this, EncoderContext.builder().build()); + return writer.toString(); + } + @Override public String toString() { - return "BsonArray{" - + "values=" + getValues() - + '}'; + return this.toJson(); } @Override