Skip to content

Commit

Permalink
Small refactorings to BsonParser with more meaningful method names
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Oct 16, 2019
1 parent ef44e5e commit 3f240d1
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BsonParser extends ParserBase implements Wrapper<BsonReader> {

private final AbstractBsonReader reader;

private final ParseContext context;
private ParseContext context;
/**
* The ObjectCodec used to parse the Bson object(s)
*/
Expand All @@ -69,10 +69,17 @@ private String valueAsString() {
return Objects.toString(value);
}

private Object setValue(Object value) {
private void setValue(Object value) {
this.value = value;
skipValue = false;
return value;
}

/**
* Means value was read from {@link BsonReader} this flag is used
* on next read token iteration to checker wherever to call {@link BsonReader#skipValue()}.
*/
private boolean hasValue() {
return !skipValue;
}

/**
Expand All @@ -89,6 +96,7 @@ protected void _closeInput() throws IOException {
if (isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) {
reader.close();
}
context = null;
_closed = true;
}

Expand Down Expand Up @@ -134,7 +142,7 @@ public Number getNumberValue() throws JsonParseException {
throw new JsonParseException(this, String.format("Can't convert %s (bson:%s) to %s", currentToken(), type(), Number.class.getName()));
}

if (context.skipValue) {
if (!context.hasValue()) {
// lazily read the value
readValue();
}
Expand Down Expand Up @@ -298,8 +306,8 @@ public JsonToken nextToken() throws JsonParseException {

private JsonToken next() throws JsonParseException {

if (context.skipValue && state() == AbstractBsonReader.State.VALUE) {
// means the value was not read before and can be skipped
if (!context.hasValue() && state() == AbstractBsonReader.State.VALUE) {
// means the value was not parsed before and can be skipped
reader.skipValue();
}

Expand Down Expand Up @@ -388,22 +396,21 @@ public String getText() throws JsonParseException {
return token.asString();
}

if (!context.skipValue) {
return Objects.toString(context.value);
if (context.hasValue()) {
return context.valueAsString();
}

readValue();

return context.valueAsString();
}

@Override
public byte[] getBinaryValue(Base64Variant variant) throws IOException {
if (type() != BsonType.BINARY) {
throw new JsonParseException(this, String.format("Expected %s got %s", BsonType.BINARY, type()));
throw new JsonParseException(this, String.format("Can't read binary data. Expected type %s got %s", BsonType.BINARY, type()));
}

if (context.value == null) {
if (!context.hasValue()) {
readValue();
}

Expand Down

0 comments on commit 3f240d1

Please sign in to comment.