Skip to content

Commit

Permalink
style: source reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
flsobral committed Jul 19, 2021
1 parent e787022 commit 0f3876d
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions TotalCrossSDK/src/main/java/totalcross/json/JSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,49 @@
import java.util.Map;

/**
The JSONFactory class helps converting json objects into Java objects, using reflection.
Some examples:
<pre>
class Car
{
private int id;
private String description;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
</pre>
You can retrieve a new Car object using:
<pre>
Car cc = JSONFactory.parse("{\"carro\":{\"id\":-1,\"descricao\":\"GOL\"}}", Carro.class);
</pre>
You may also retrieve a list or an array. See the JSONSample in the TotalCrossAPI.
* The JSONFactory class helps converting json objects into Java objects, using
* reflection.
*
* Some examples:
*
* <pre>
* class Car {
* private int id;
* private String description;
*
* public int getId() {
* return id;
* }
*
* public void setId(int id) {
* this.id = id;
* }
*
* public String getDescription() {
* return description;
* }
*
* public void setDescription(String description) {
* this.description = description;
* }
* }
* </pre>
*
* You can retrieve a new Car object using:
*
* <pre>
* Car cc = JSONFactory.parse("{\"carro\":{\"id\":-1,\"descricao\":\"GOL\"}}", Carro.class);
* </pre>
*
* You may also retrieve a list or an array. See the JSONSample in the
* TotalCrossAPI.
*/
public class JSONFactory {
private static Map<Class<?>, Map<String, Method>> classes = new HashMap<>();

public static <T> List<T> asList(String json, Class<T> classOfT) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {
public static <T> List<T> asList(String json, Class<T> classOfT)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {
List<T> list = new ArrayList<T>();
try {
JSONArray jsonArray = new JSONArray(json);
Expand Down Expand Up @@ -93,13 +95,15 @@ public static <T> T parse(String json, Class<T> classOfT) throws InstantiationEx
return parse(new JSONObject(json), classOfT);
}

public static <T> T parse(JSONArray jsonArray, Class<T> classOfT) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {
return parse(null, jsonArray, classOfT);
public static <T> T parse(JSONArray jsonArray, Class<T> classOfT)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {
return parse(null, jsonArray, classOfT);
}

private static <T> T parse(Object outerObject, JSONArray jsonArray, Class<T> classOfT) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {

private static <T> T parse(Object outerObject, JSONArray jsonArray, Class<T> classOfT)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
JSONException, ArrayIndexOutOfBoundsException, NoSuchMethodException, SecurityException {
if (classOfT.isArray()) {
T array;
try {
Expand All @@ -115,14 +119,16 @@ private static <T> T parse(Object outerObject, JSONArray jsonArray, Class<T> cla
}
return parse(outerObject, jsonArray, classOfT);
}

public static <T> T parse(JSONObject jsonObject, Class<T> classOfT) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONException, NoSuchMethodException, SecurityException {
return parse(null, jsonObject, classOfT);

public static <T> T parse(JSONObject jsonObject, Class<T> classOfT)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
JSONException, NoSuchMethodException, SecurityException {
return parse(null, jsonObject, classOfT);
}

private static <T> T parse(Object outerObject, JSONObject jsonObject, Class<T> classOfT) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONException, NoSuchMethodException, SecurityException {
private static <T> T parse(Object outerObject, JSONObject jsonObject, Class<T> classOfT)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
JSONException, NoSuchMethodException, SecurityException {
if (classOfT.isArray()) {
throw new IllegalArgumentException();
}
Expand Down

0 comments on commit 0f3876d

Please sign in to comment.