Skip to content

Commit

Permalink
Minor tweaks to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 15, 2025
1 parent f7eede3 commit b11e4f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,11 @@ private static void generateArray(ThreadContext context, Session session, RubyAr

buffer.write('[');
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
boolean firstItem = true;

int length = object.getLength();
for (int i = 0, t = length; i < t; i++) {
for (int i = 0; i < length; i++) {
IRubyObject element = object.eltInternal(i);
if (firstItem) {
firstItem = false;
} else {
if (i > 0) {
buffer.write(',');
if (!arrayNLEmpty) {
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
Expand All @@ -407,10 +404,10 @@ private static void generateArray(ThreadContext context, Session session, RubyAr
generateFor(context, session, element, buffer);
}

state.decreaseDepth();
int oldDepth = state.decreaseDepth();
if (!arrayNLEmpty) {
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
Utils.repeatWrite(buffer, indentUnit, state.getDepth());
Utils.repeatWrite(buffer, indentUnit, oldDepth);
}

buffer.write((byte) ']');
Expand Down Expand Up @@ -451,19 +448,19 @@ private static void generateHash(ThreadContext context, Session session, RubyHas
final ByteList spaceBefore = state.getSpaceBefore();
final ByteList space = state.getSpace();

buffer.write((byte)'{');
buffer.write('{');
buffer.write(objectNLBytes);

boolean firstPair = true;
for (RubyHash.RubyHashEntry entry : (Set<RubyHash.RubyHashEntry>) object.directEntrySet()) {
processEntry(context, session, buffer, entry, firstPair, objectNl, indent, spaceBefore, space);
firstPair = false;
}
state.decreaseDepth();
int oldDepth = state.decreaseDepth();
if (!firstPair && !objectNl.isEmpty()) {
buffer.write(objectNLBytes);
}
buffer.write(Utils.repeat(state.getIndent(), state.getDepth()));
Utils.repeatWrite(buffer, state.getIndent(), oldDepth);
buffer.write((byte)'}');
}

Expand Down
4 changes: 2 additions & 2 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ public int increaseDepth(ThreadContext context) {
return depth;
}

public void decreaseDepth() {
--depth;
public int decreaseDepth() {
return --depth;
}

/**
Expand Down

0 comments on commit b11e4f2

Please sign in to comment.