Skip to content

made JSONArray Iterable #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions JSONString.java

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 8 additions & 2 deletions JSONArray.java → src/org/json/JSONArray.java
Original file line number Diff line number Diff line change
@@ -75,9 +75,10 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2014-05-03
* @author Haringat
* @version 2015-06-04
*/
public class JSONArray {
public class JSONArray implements Iterable<Object>{

/**
* The arrayList where the JSONArray's properties are kept.
@@ -974,4 +975,9 @@ Writer write(Writer writer, int indentFactor, int indent)
throw new JSONException(e);
}
}

@Override
public Iterator<Object> iterator() {
return myArrayList.iterator();
}
}
9 changes: 8 additions & 1 deletion JSONException.java → src/org/json/JSONException.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
* The JSONException is thrown by the JSON.org classes when things are amiss.
*
* @author JSON.org
* @version 2014-05-03
* @author Haringat
* @version 2014-06-04
*/
public class JSONException extends RuntimeException {
private static final long serialVersionUID = 0;
@@ -19,6 +20,12 @@ public class JSONException extends RuntimeException {
public JSONException(String message) {
super(message);
}


public JSONException(String message, Throwable cause){
super(message, cause);
this.cause = cause;
}

/**
* Constructs a new JSONException with the specified cause.
File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions src/org/json/JSONString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.json;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
* method so that a class can change the behavior of
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>,
* and <code>JSONWriter.value(</code>Object<code>)</code>. The
* <code>toJSONString</code> method will be used instead of the default behavior
* of using the Object's <code>toString()</code> method and quoting the result.
*/
public interface JSONString {
/**
* The <code>toJSONString</code> method allows a class to produce its own JSON
* serialization.<br>
*
* The default implementation will look for fields in the class and will check
* if they extend <code>JSONString</code>. If they do the result of
* <code>&lt;field&gt.toJSONString()</code> is taken as value. Otherwise it will
* just use <code>&lt;field&gt.toString()</code>.
*
* @return A strictly syntactically correct JSON text or null if an exception
* occurred
* @throws JSONException if a field's type or value could not be retrieved
*/
public default String toJSONString() throws JSONException {
String ret = "{\n";
for(Field f: this.getClass().getFields()){
Object o = null;
try {
if(Modifier.isStatic(f.getModifiers())){
o = f.get(null);
} else {
o = f.get(this);
}
} catch( IllegalAccessException | IllegalArgumentException e){
throw new JSONException("could not create a JSON representation"
+ "for " + this.getClass().getName(), e);
}
ret += "\"" + f.getName().replace("\"", "\\\"") + "\":";
if(f.getType().isAssignableFrom(JSONString.class)){
ret += ((JSONString) o).toJSONString();
} else {
ret += "\"" + o.toString().replace("\"", "\\\"") + "\"";
}
ret += "\n";
}
ret += "}\n";
return ret;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.