Skip to content

Commit

Permalink
iterators for JSONArray types
Browse files Browse the repository at this point in the history
  • Loading branch information
benfry committed Sep 28, 2023
1 parent 8fb8937 commit 1849ab7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
106 changes: 106 additions & 0 deletions core/src/processing/data/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;

Expand Down Expand Up @@ -561,6 +562,111 @@ public JSONObject getJSONObject(int index, JSONObject defaultValue) {
}


public Iterable<Boolean> booleanValues() {
return () -> new Iterator<>() {
int index = -1;

public Boolean next() {
return getBoolean(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<Integer> intValues() {
return () -> new Iterator<>() {
int index = -1;

public Integer next() {
return getInt(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<Long> longValues() {
return () -> new Iterator<>() {
int index = -1;

public Long next() {
return getLong(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<Float> floatValues() {
return () -> new Iterator<>() {
int index = -1;

public Float next() {
return getFloat(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<Double> doubleValues() {
return () -> new Iterator<>() {
int index = -1;

public Double next() {
return getDouble(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<JSONArray> arrayValues() {
return () -> new Iterator<>() {
int index = -1;

public JSONArray next() {
return getJSONArray(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


public Iterable<JSONObject> objectValues() {
return () -> new Iterator<>() {
int index = -1;

public JSONObject next() {
return getJSONObject(++index);
}

public boolean hasNext() {
return index+1 < size();
}
};
}


/** Use toStringArray() instead. */
@Deprecated
public String[] getStringArray() {
Expand Down
4 changes: 4 additions & 0 deletions core/todo.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1294 (4.3.1)
X difference in text position with processing 4.3, JAVA2D vs P2D
X https://github.com/processing/processing4/issues/768
X add iterators to JSONArray


contribs
Expand All @@ -13,6 +14,9 @@ X https://github.com/processing/processing4/pull/776
X Fix `PShape.getSpecular()`, `getEmissive()`, and `getShininess()` from @hx2A
X https://github.com/processing/processing4/issues/781
X https://github.com/processing/processing4/pull/782
_ Fix hash of keyEvent being added to pressedKeys
_ https://github.com/processing/processing4/issues/779
_ https://github.com/processing/processing4/pull/786


_ JNA version of getting resolution
Expand Down

0 comments on commit 1849ab7

Please sign in to comment.